JXLS + SXSSF - 如何在不包含模板和实际生成的报告的情况下生成Excel文件?

时间:2016-04-07 18:54:03

标签: java streaming apache-poi jxls

我正在尝试使用JXLS的PoiTransformer来生成一个非常庞大的报告;问题是,到目前为止我发现的所有示例都生成了一个包含两个工作表的excel文件,一个包含原始的,未修改的模板,另一个包含实际结果。有没有办法省略第一张纸,只保留结果?

这是我目前的代码:

        PoiContext context = new PoiContext();
        context.putVar("params", this.montaParametros(rel, filtro, opcoes, dados));
        context.putVar("dados", this.converteDados(rel, dados));

        Workbook template = WorkbookFactory.create(resource.getInputStream());
        PoiTransformer transformer = PoiTransformer.createSxssfTransformer(template, 5, false);

        AreaBuilder areaBuilder = new XlsCommentAreaBuilder(transformer);
        List<Area> xlsAreaList = areaBuilder.build();

        Area xlsArea = xlsAreaList.get(0);
        // xlsArea.applyAt(new CellRef("Template!A1"), context); // Tried forcing it to use the original sheet as the output sheet, but it does not overwrite things properly
        xlsArea.applyAt(new CellRef("Result!A1"), context); // Writes into a new sheet, but that's not what is desired

        Workbook processado = transformer.getWorkbook();
        processado.write(out);

1 个答案:

答案 0 :(得分:2)

好吧,傻傻的我,似乎我可以在写入磁盘之前删除不需要的纸张;我认为SXSSF会以某种方式阻碍它,但它似乎只是起作用:

    Workbook processado = transformer.getWorkbook();
    processado.removeSheetAt(0);
    processado.write(out);