使用Sqlite数据库在Android中生成Excel报告

时间:2012-05-14 07:21:41

标签: java android eclipse sqlite

我在Android上有新手。我使用ListView制作了一份报告,该报告分为三列,分别为DateTimeOnOffStatusAlarmImage

它工作正常,看起来不错,但现在我想将此表格数据导出为Excel格式。是否可能以及如何?

提前谢谢 Om Parkash Kaushik

1 个答案:

答案 0 :(得分: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"));
}

然后通过设置样式和值来继续行。

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

// Jakob