我修改了基本的 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/