java - [apache poi xssf] :creating pivot table in new sheet(Java)

标签 java apache apache-poi pivot-table xssf

我修改了基本的 example在新工作表中创建数据透视表。但是在打开新的 xlsx 文件时,出现错误(Excel 在...中发现无法读取的内容后跟:

Removed Part: /xl/pivotTables/pivotTable1.xml part with XML error.  (PivotTable view) Load error. Line 2, column 561.
Removed Records: Workbook properties from /xl/workbook.xml part (Workbook)

)

这是我修改的代码片段:

XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("plain");

//Create some data to build the pivot table on
setCellData(sheet);

XSSFSheet sheet2 = wb.createSheet("pivot");

XSSFPivotTable pivotTable = sheet2.createPivotTable(new AreaReference("plain!$A$1:$D$4", null), new CellReference("pivot!$A$1"));
//Configure the pivot table
//Use first column as row label
pivotTable.addRowLabel(0);
//Sum up the second column
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
//Set the third column as filter
pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2);
//Add filter on forth column
pivotTable.addReportFilter(3);

我调试了代码,没有发现明显的问题...

关于如何处理这个问题的想法?或者这是库的错误?

谢谢

编辑

问题是从上面代码中的单元格引用 A1 (new CellReference("pivot!$A$1")) 开始。看起来,如果我们从 A1 开始,工作表上没有足够的空间来做一些其他的格式化枢轴网格。因此,将其更改为 A5 即可。尽管我仍然认为 POI 应该通过抛出错误来明确阻止人们这样做

最佳答案

我在 POI 版本 3.14 和 3.16 Beta 中遇到过这个问题,但我发现当我调用方法 createPivotTable 并将源表引用作为第三个选项时,这在这些版本中有效。

        //Create some data to build the pivot table on
    setCellData(sheet);

    AreaReference source = new AreaReference("A1:D4", SpreadsheetVersion.EXCEL2007);
    XSSFSheet sheet2 = wb.createSheet("pivot");

    CellReference position = new CellReference("A3"); //convertColStringToIndex

    XSSFPivotTable pivotTable = sheet2.createPivotTable(source, position, sheet);

这在 POI 3.16 Beta 和 3.14 中运行。
我在 A1 的 CellReference 也遇到了同样的问题,是的,我同意它应该产生一些警告/错误。

关于java - [apache poi xssf] :creating pivot table in new sheet(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37401570/

相关文章:

apache - 您可以通过Apache下载文件吗?

java - 在使用 JPA 映射类时,为什么有人要在 getter 或 setter 上添加注释?

java - 如何使用代码停止 Apache Felix Webconsole 中的 bundle

macos - `Apache` `localhost/~username/` 不工作

apache - Ubuntu 14.4.01 子域上的 Symfony2 - 缺少 Assets

java - 导出到 Excel JSF 和 PrimeFaces

java - Excel中根据日期对数据进行排序

java - 如何使用 Apache POI 和 Java 添加多个不同的 Excel 列

java - 为什么数组是对象,却不能作为基类?

java - 构造函数必须调用 super() 或 this()