【问题标题】:Apache POI : Excel Pivot Table - Row LabelApache POI:Excel 数据透视表 - 行标签
【发布时间】:2016-04-06 05:22:17
【问题描述】:

我需要在 JAVA 中创建一个 excel 工作表,如下所示:

我无法创建并排多列的行标签(菜单和子菜单过滤器)。

不是在不同的列中显示子菜单,而是在菜单列下。

下面是我写的一段代码:

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

但它的显示错误如下:

有人可以帮帮我吗?

【问题讨论】:

  • 在数据透视表选项下,选择“经典数据透视表布局”
  • 请您详细说明一下好吗?我是 poi API 的新手。
  • 我告诉你如何在 Excel 中更改它。宏记录器显示您想要pivotTable.InGridDropZones = TruepivotTable.RowAxisLayout xlTabularRow
  • 我想用java代码来做。我无法在 Excel 工作表上手动更改它。此工作表已由用户下载。

标签: java excel apache-poi pivot-table xssf


【解决方案1】:

因为格式是 XML,所以很容易检查需要什么。解压Zip xlsx 并查看/xl/pivotTables/pivotTable1.xml

然后:https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFPivotTable.html#getCTPivotTableDefinition%28%29http://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"); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多