JXLS使用不同大小的列表时,多个工作表失败

时间:2015-12-04 13:13:43

标签: jxls

我在使用多张演示文稿时使用了几乎相同的代码,唯一的区别是我在一张工作表上有4个不同的列表。 (template4)

  • 如果我只为一张纸运行这个例子,那就完美了。
  • 如果删除SUM公式,则可以正常工作。
  • 如果我修改模板并删除同一行上的列表,它可以工作,(template5)
  • 如果我为每个部分使用相同大小的列表,它就可以工作。

here's the templates

这是代码:

public void excelExport() throws IOException {
    LogManager.getLogger("org.jxls.area.XlsArea").setLevel(Level.ERROR);
    try (FileOutputStream os = new FileOutputStream("c:/Temp/xxx.xls")) {
        try (InputStream is =new FileInputStream("C:\\Works\\workspace-intellij\\nostro-vaadin\\src\\main\\resources\\file_templates\\report5_template.xls")) {
            Transformer transformer = TransformerFactory.createTransformer(is, os);
            XlsArea xlsArea = new XlsArea("template!A1:G20", transformer);

            XlsArea corrsArea = new XlsArea("template!A2:G19", transformer);
            EachCommand corrsEach = new EachCommand("val", "data", corrsArea, new ExcelCellRefGenerator());

            XlsArea bookDebitArea = new XlsArea("template!A8:C8", transformer);
            Command bookDebitEach = new EachCommand("bookDebit", "val.dbooks", bookDebitArea);
            corrsArea.addCommand(new AreaRef("template!A8:C8"), bookDebitEach);

            XlsArea bookCreditArea = new XlsArea("template!E8:G8", transformer);
            Command bookCreditEach = new EachCommand("bookCredit", "val.cbooks", bookCreditArea);
            corrsArea.addCommand(new AreaRef("template!E8:G8"), bookCreditEach);

            XlsArea statDebitArea = new XlsArea("template!A12:C12", transformer);
            Command statDebitEach = new EachCommand("statDebit", "val.dstmts", statDebitArea);
            corrsArea.addCommand(new AreaRef("template!A12:C12"), statDebitEach);

            XlsArea statCreditArea = new XlsArea("template!E12:G12", transformer);
            Command statCreditEach = new EachCommand("statCredit", "val.cstmts", statCreditArea);
            corrsArea.addCommand(new AreaRef("template!E12:G12"), statCreditEach);

            xlsArea.addCommand(corrsArea.getAreaRef(), corrsEach);

            Context context = new Context();
            context.putVar("data", getList(10, 50));
            xlsArea.applyAt(new CellRef("Sheet!A1"), context);
            xlsArea.processFormulas();
            transformer.write();
        } catch (Exception e) {
            log.error("", e);
        }
    }
}

public List<ExcelReportSheet> getList(int bank, int trans) {
    List<ExcelReportSheet> sheets = new ArrayList<>();
    for (int i = 0; i < bank; i++) {
        NsCorrespondents corr = new NsCorrespondents();
        corr.setBankcode(i + "bank");
        corr.setBankname(i + "bankname");
        corr.setBic(i + "bankbic");
        corr.setShdBbalance(new BigDecimal("10000"));
        corr.setShdSbalance(new BigDecimal("20000"));
        ExcelReportSheet reportSheet = new ExcelReportSheet(corr);
        Random random = new Random();
        for (int j = 0; j < random.nextInt(trans); j++) {
            reportSheet.getCbooks().add(ExcelReportSheet.ExcelReportTran.of(Date.valueOf(LocalDate.now()), j + "-" + i + "cbref", new BigDecimal(random.nextInt(trans))));
        }
        for (int j = 0; j < random.nextInt(trans); j++) {
            reportSheet.getDbooks().add(ExcelReportSheet.ExcelReportTran.of(Date.valueOf(LocalDate.now()), j + "-" + i + "cbref", new BigDecimal(random.nextInt(trans))));
        }
        for (int j = 0; j < random.nextInt(trans); j++) {
            reportSheet.getCstmts().add(ExcelReportSheet.ExcelReportTran.of(Date.valueOf(LocalDate.now()), j + "-" + i + "cbref", new BigDecimal(random.nextInt(trans))));
        }
        for (int j = 0; j < random.nextInt(trans); j++) {
            reportSheet.getDstmts().add(ExcelReportSheet.ExcelReportTran.of(Date.valueOf(LocalDate.now()), j + "-" + i + "cbref", new BigDecimal(random.nextInt(trans))));
        }

        sheets.add(reportSheet);
    }
    return sheets;
}

这是我得到的错误。我相信它会混合来自不同表格的SUM公式。

任何帮助都将不胜感激。

14:57:20.455 [main] ERROR o.jxls.transform.poi.PoiTransformer - Failed to set formula = SUM(C19,C20,Sheet1!C20,Sheet1!C21,Sheet1!C22,Sheet1!C23,Sheet1!C24,Sheet1!C25,Sheet1!C26,Sheet1!C27,Sheet1!C28,Sheet1!C29,Sheet1!C30,Sheet1!C31,Sheet1!C32,Sheet3!C21,Sheet3!C22,Sheet3!C23,Sheet3!C24,Sheet3!C25,Sheet4!C18,Sheet4!C19,Sheet4!C20,Sheet4!C21,Sheet4!C22,Sheet4!C23,Sheet4!C24,Sheet5!C23,Sheet5!C24,Sheet5!C25,Sheet5!C26,Sheet5!C27,Sheet5!C28,Sheet5!C29,Sheet5!C30,Sheet6!C19,Sheet6!C20,Sheet6!C21,Sheet6!C22,Sheet6!C23,Sheet6!C24,Sheet6!C25,Sheet6!C26,Sheet6!C27,Sheet6!C28,Sheet7!C16,Sheet7!C17,Sheet7!C18,Sheet7!C19,Sheet7!C20,Sheet7!C21,Sheet7!C22,Sheet7!C23,Sheet7!C24,Sheet7!C25,Sheet7!C26,Sheet7!C27,Sheet8!C12,Sheet8!C13,Sheet9!C27,Sheet9!C28,Sheet9!C29,Sheet9!C30,Sheet9!C31,Sheet9!C32,Sheet9!C33,Sheet9!C34,Sheet9!C35,Sheet9!C36,Sheet9!C37,Sheet9!C38,Sheet9!C39,Sheet9!C40) into cell = Sheet0!C16

1 个答案:

答案 0 :(得分:1)

当您拥有复杂模板或具有多个列表的转换时,默认的Jxls公式处理器(名为 FastFormulaProcessor )可能无法正确评估公式。

在这种情况下,您应该使用 StandardFormulaProcessor(),它速度较慢,但​​在所有情况下都可靠运行。您可以通过在 processFormulas()方法之前的某处添加以下代码来配置要使用的公式处理器

xlsArea.setFormulaProcessor(new StandardFormulaProcessor());