使用POI将不同的数据类型写入Excel

时间:2013-03-08 15:56:09

标签: java excel apache-poi

我正在尝试将数据从数据库导出到Excel。我将数据导出并存储在ArrayList中(可以更改)。我已经能够将数据导出到excel但是所有的值都被导出为字符串,我需要它们来保存它们的数据类型,即货币/数字。

我正在使用Apache POI,并且很难将字段的数据类型设置为除String之外的任何内容。我错过了什么吗?有人可以告诉我更好的方法吗?对此的任何帮助将不胜感激。

public static void importDataToExcel(String sheetName, ArrayList header, ArrayList data, File xlsFilename, int sheetNumber)
        throws HPSFException, FileNotFoundException, IOException {

    POIFSFileSystem fs = new POIFSFileSystem();
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(xlsFilename));
    HSSFSheet sheet = wb.createSheet(sheetName);

    int rowIdx = 0;
    short cellIdx = 0;

    // Header
    HSSFRow hssfHeader = sheet.createRow(rowIdx);
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    for (Iterator cells = header.iterator(); cells.hasNext();) {
        HSSFCell hssfCell = hssfHeader.createCell(cellIdx++);
        hssfCell.setCellStyle(cellStyle);
        hssfCell.setCellValue((String) cells.next());
    }
    // Data

    rowIdx = 1;
    for (Iterator rows = data.iterator(); rows.hasNext();) {
        ArrayList row = (ArrayList) rows.next();
        HSSFRow hssfRow = (HSSFRow) sheet.createRow(rowIdx++);
        cellIdx = 0;
        for (Iterator cells = row.iterator(); cells.hasNext();) {
            HSSFCell hssfCell = hssfRow.createCell(cellIdx++);
            hssfCell.setCellValue((String) cells.next());
        }
    }
    Logfile.log("sheetNumber = " + sheetNumber);
    wb.setSheetName(sheetNumber, sheetName);
    try {
        FileOutputStream out = new FileOutputStream(xlsFilename);
        wb.write(out);
        out.close();
    } catch (IOException e) {
        throw new HPSFException(e.getMessage());
    }

}

1 个答案:

答案 0 :(得分:1)

您需要在投射前检查细胞值的类别:

public static void importDataToExcel(String sheetName, List<String> headers, List<List<Object>> data, File xlsFilename, int sheetNumber)
        throws HPSFException, FileNotFoundException, IOException {

    POIFSFileSystem fs = new POIFSFileSystem();
    Workbook wb;
    try {
        wb = WorkbookFactory.create(new FileInputStream(xlsFilename));
    } catch (InvalidFormatException ex) {
        throw new IOException("Invalid workbook format");
    }
    Sheet sheet = wb.createSheet(sheetName);

    int rowIdx = 0;
    int cellIdx = 0;

    // Header
    Row hssfHeader = sheet.createRow(rowIdx);
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    for (final String header : headers) {
        Cell hssfCell = hssfHeader.createCell(cellIdx++);
        hssfCell.setCellStyle(cellStyle);
        hssfCell.setCellValue(header);
    }
    // Data

    rowIdx = 1;
    for (final List<Object> row : data) {
        Row hssfRow = sheet.createRow(rowIdx++);
        cellIdx = 0;
        for (Object value : row) {
            Cell hssfCell = hssfRow.createCell(cellIdx++);
            if (value instanceof String) {
                hssfCell.setCellValue((String) value);
            } else if (value instanceof Number) {
                hssfCell.setCellValue(((Number) value).doubleValue());
            } else {
                throw new RuntimeException("Cell value of invalid type " + value);
            }
        }
    }
    wb.setSheetName(sheetNumber, sheetName);
    try {
        FileOutputStream out = new FileOutputStream(xlsFilename);
        wb.write(out);
        out.close();
    } catch (IOException e) {
        throw new HPSFException(e.getMessage());
    }
}

我还添加了泛型 - 这使得代码更具可读性。此外,您需要尽可能避免使用实际的class,并使用interface,例如List而非ArrayListRow而不是HSSFRow