【发布时间】:2012-05-14 07:21:41
【问题描述】:
我对 Android 有了新的认识。我使用ListView 进行了报告,该报告分为三列,分别名为DateTime、OnOffStatus、AlarmImage。
它工作正常并且看起来足够好,但现在我想将此表数据导出为 Excel 格式。有没有可能,怎么做?
提前致谢 Om Parkash Kaushik
【问题讨论】:
标签: java android eclipse sqlite
我对 Android 有了新的认识。我使用ListView 进行了报告,该报告分为三列,分别名为DateTime、OnOffStatus、AlarmImage。
它工作正常并且看起来足够好,但现在我想将此表数据导出为 Excel 格式。有没有可能,怎么做?
提前致谢 Om Parkash Kaushik
【问题讨论】:
标签: java android eclipse sqlite
首先查看http://poi.apache.org/ 和http://poi.apache.org/spreadsheet/index.html 我将这个库用于我所有的 Excel 报告。
从创建工作簿开始:
HSSFWorkbook workbook = new HSSFWorkbook();
Map<String, CellStyle> styles = createStyles(workbook);
HSSFSheet sheet = workbook.createSheet();
设置一些样式:
private static Map<String, CellStyle> createStyles(Workbook wb) {
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style;
Font monthFont = wb.createFont();
monthFont.setFontHeightInPoints((short) 11);
monthFont.setColor(IndexedColors.WHITE.getIndex());
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(monthFont);
style.setWrapText(true);
styles.put("header", style);
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
style.setBorderRight(CellStyle.BORDER_NONE);
style.setBorderLeft(CellStyle.BORDER_NONE);
style.setBorderTop(CellStyle.BORDER_NONE);
style.setBorderBottom(CellStyle.BORDER_NONE);
styles.put("cell", style);
return styles;
}
然后设置标题行:
private static final String[] article_headers = {"header1", "header2"};
// Header row
Row headerRow = sheet.createRow(0);
headerRow.setHeightInPoints(40);
Cell headerCell;
for (int i = 0; i < article_headers.length; i++) {
headerCell = headerRow.createCell(i);
headerCell.setCellValue(article_headers[i]);
headerCell.setCellStyle(styles.get("header"));
}
然后你通过设置它们的样式和值来继续这些行。
希望对您有所帮助,如果您觉得有帮助,请记得接受。
// 雅各布
【讨论】: