0

我想使用 primefaces 4.0 或 primefaces 扩展导出器在一个 Excel 文件中导出多个数据表,每个数据表都在单独的工作表中。这可以做到吗?

4

1 回答 1

1

使用jxl框架处理 Excel 文件。我认为它仍然是最好的选择之一。

一个例子:

public void createExcelReport(OutputStream stream) {
        WritableWorkbook workbook = null;
        try {
            Workbook template = Workbook.getWorkbook(new File(getClass().getResource("/report/report_template.xls").getFile()));
            workbook = Workbook.createWorkbook(stream, template);
            WritableSheet sheet = workbook.getSheet(0);
            int idx = START_ROW_IDX;
            for (SomePojo pojo : pojoService.getPojos()) {
                writePojoToRow(pojo);       
                        idx++;
            }
            if (idx == START_ROW_IDX) {
                addNoErrorSheet(sheet);
            }
            workbook.write();
        } catch (Exception e) {
            log.error("There was a problem here.", e);
            throw new RuntimeException("Excel file creation did not work because of this Exception: ", e);
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
于 2014-02-28T13:35:41.970 回答