当同一个索引用于列和行标签时,Apache POI数据透视表错误

我想创build一个数据透视表来做队列分析

pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1); pivotTable.addRowLabel(1); 

这是给我一个错误,当打开该文件的文件损坏的文件是否仍然打开该文件,当我说是和打开它,结果看起来不错,唯一的问题是错误。

我做了一个解决方法,以重复具有不同名称的列数据

例如:说1列是电子邮件添加了一个副本列36名称重复电子邮件,并做了如下所示,它工作正常

 pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1); pivotTable.addRowLabel(35); 

为什么首先它失败,当我同时给列和行标签为1。

任何帮助是极大的赞赏

如果您使用apache poi设置pivotTable.addRowLabel(1) ,那么apache poi会将pivot字段1设置为axisRow,但是如果您还想要pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1)也需要将其设置为dataField。 所以我们需要纠正这一点。

例:

 import org.apache.poi.xssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.*; import java.io.*; class PivotTableTest5 { private static void setCellData(Sheet sheet) { Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Name"); cell = row.createCell(1); cell.setCellValue("City"); for (int r = 1; r < 15; r++) { row = sheet.createRow(r); cell = row.createCell(0); cell.setCellValue("Name " + ((r-1) % 4 + 1)); cell = row.createCell(1); cell.setCellValue("City " + (int)((new java.util.Random().nextDouble() * 3)+1) ); } } public static void main(String[] args) { try { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); //Create some data to build the pivot table on setCellData(sheet); XSSFPivotTable pivotTable = sheet.createPivotTable( new AreaReference(new CellReference("A1"), new CellReference("B15")), new CellReference("H5")); //Count the second column. This needs to be second column a data field. pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1); //Use second column as row label pivotTable.addRowLabel(1); //Apache poi sets pivot field 1 (second column) only to be axisRow but it needs to be dataField too. pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(1).setDataField(true); FileOutputStream fileOut = new FileOutputStream("PivotTableTest5.xlsx"); wb.write(fileOut); fileOut.close(); wb.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }