简体   繁体   English

[apache poi xssf]:在新工作表中创建数据透视表(Java)

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

I've modified the basic example to create a pivot table in a new worksheet. 我已修改基本示例以在新工作表中创建数据透视表。 But upon opening the new xlsx file, I get an error ( Excel found unreadable content in... followed by: 但是在打开新的xlsx文件时,我收到一个错误( Excel found unreadable content in...后跟:

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)

)

Here's the code snippet I modified: 这是我修改的代码段:

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);

I've debugged through the code, and don't see an apparent issue... 我已经通过代码进行了调试,并没有看到明显的问题......

Thoughts on how this can be handled? 关于如何处理这个问题的想法? or if this is a bug with the library? 或者如果这是图书馆的错误?

Thanks 谢谢

EDIT 编辑

The problem is with starting from cell reference A1 ( new CellReference("pivot!$A$1") ) in the above code. 问题是从上面的代码中的单元格引用A1( new CellReference("pivot!$A$1") )开始。 It seems, if we start from A1, there is not enough space left on the sheet to do some other formatting pivot grid does. 看来,如果我们从A1开始,工作表上没有足够的空间来做其他格式化透视网格。 So changing that to A5 does the work. 所以将其改为A5就可以了。 Though I still think POI should explicitly prevent people from doing this by throwing an error 虽然我仍然认为POI应该明确阻止人们通过抛出错误来做到这一点

I had this issue in POI verion 3.14 and 3.16 Beta, but I found when I called the method createPivotTable with the source table reference as the third option, this worked in those versions. 我在POI verion 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);

This ran in POI 3.16 Beta and 3.14. 这在POI 3.16 Beta和3.14中运行。
I also had the same issue with CellReference at A1 and yes, I agree that it should produce some warning/error. 我在A1的CellReference也有同样的问题,是的,我同意它应该产生一些警告/错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM