【问题标题】:Generate Excel Report in Android with Sqlite Database使用 Sqlite 数据库在 Android 中生成 Excel 报告
【发布时间】:2012-05-14 07:21:41
【问题描述】:

我对 Android 有了新的认识。我使用ListView 进行了报告,该报告分为三列,分别名为DateTimeOnOffStatusAlarmImage

它工作正常并且看起来足够好,但现在我想将此表数据导出为 Excel 格式。有没有可能,怎么做?

提前致谢 Om Parkash Kaushik

【问题讨论】:

标签: java android eclipse sqlite


【解决方案1】:

首先查看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"));
}

然后你通过设置它们的样式和值来继续这些行。

希望对您有所帮助,如果您觉得有帮助,请记得接受。

// 雅各布

【讨论】:

  • 在我的应用程序中运行此代码时出现“java.lang.NoClassDefFoundError: org.apache.poi.hssf.usermodel.HSSFWorkbook”错误。您能指导我如何从我的代码。我在我的项目中添加了 poi-3.9-20121203.jar 文件。
猜你喜欢
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多