【问题标题】:[apache poi xssf]:creating pivot table in new sheet(Java)[apache poi xssf]:在新工作表中创建数据透视表(Java)
【发布时间】:2025-12-30 23:45:13
【问题描述】:

我修改了基本的example 以在新工作表中创建数据透视表。但是在打开新的 xlsx 文件时,我收到一个错误(Excel found unreadable content in...followed by:

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)

)

这是我修改的代码sn-p:

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.15-beta1 以检查它是否仍然存在?
  • 这听起来像是 POI 中的错误,我认为最好的选择是在bz.apache.org/bugzilla/enter_bug.cgi?product=POI报告错误
  • 我会尝试新版本...虽然犹豫在生产环境中使用它仍然是“测试版”

标签: java apache apache-poi pivot-table xssf


【解决方案1】:

我在 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 也遇到了同样的问题,是的,我同意它应该会产生一些警告/错误。

【讨论】:

  • 如果您能找出应该触发错误的原因,请在Apache POI bugzilla 中打开一个错误。如果您也包含一些代码/逻辑,那就更好了! :)