Apache POI:Excel 数据透视表 - 行标签 [英] Apache POI : Excel Pivot Table - Row Label
问题描述
我需要在 JAVA 中创建一个 Excel 表格,如下所示:
I have a requirement to create a excel sheet in JAVA as below :
我无法创建多列并排的行标签(菜单和子菜单过滤器).
I am not able to create a row label with multiple columns side by side ( menus and submenus filter ).
不是在不同的列中显示子菜单,而是在菜单列下.
Instead of displaying the submenu in different column, its coming under the menu column.
下面是我写的一段代码:
Below is the piece of code which i have written :
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");
但它被错误地显示如下:
But its getting wrongly displayed as below :
有人可以帮我吗?
推荐答案
因为格式是 XML,所以很容易检查需要什么.解压 Zip
xlsx 并查看 /xl/pivotTables/pivotTable1.xml
.
Since the format is XML it is easy to check what is needed. Unpack the Zip
xlsx and look in the /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");
这篇关于Apache POI:Excel 数据透视表 - 行标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!