Apache POI:Excel数据透视表 – 行标签
我有一个要求在JAVA中创build一个Excel表格,如下所示:
我无法创build一个并排多列的行标签(菜单和子菜单filter)。
而不是显示在不同列中的子菜单,而是在菜单栏下。
以下是我写的一段代码:
XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0); AreaReference a=new AreaReference("A1:G7"); CellReference b=new CellReference("I5"); XSSFPivotTable pivotTable = sheet.createPivotTable(a,b); pivotTable.addReportFilter(0); pivotTable.addReportFilter(1); pivotTable.addRowLabel(2); pivotTable.addRowLabel(3); pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 4, "Sum");
但是它的错误显示如下:
有人可以帮帮我吗 ?
由于格式是XML,所以很容易检查需要什么。 解压Zip
xlsx并查看/xl/pivotTables/pivotTable1.xml
。
然后: https : //poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFPivotTable.html#getCTPivotTableDefinition%28%29和http://grepcode.com/file/repo1.maven.org/ maven2 / org.apache.poi / ooxml-schemas / 1.1 / org / openxmlformats / schemas / spreadsheetml / x2006 / main / CTPivotTableDefinition.java 。
AreaReference a=new AreaReference(new CellReference("A1"), new CellReference("E7")); CellReference b=new CellReference("I5"); XSSFPivotTable pivotTable = sheet.createPivotTable(a,b); pivotTable.addReportFilter(0); pivotTable.addReportFilter(1); pivotTable.addRowLabel(2); pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setOutline(false); pivotTable.addRowLabel(3); pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 4, "Sum");