【问题标题】:Is there any way to create a Pivot Table in Excel using Apache POI?有没有办法使用 Apache POI 在 Excel 中创建数据透视表?
【发布时间】:2014-05-27 08:59:30
【问题描述】:

我目前正在研究 Excel 的自动化,并补充说我很好地利用了 Apache POI 库。

由于我的 Excel 工作簿的各个列中存储了太多数据,因此我正在尝试创建一个数据透视表。

有没有办法使用 POI 创建数据透视表?

我的要求是我需要在新的 Excel 工作簿或存储数据的同一个工作簿中创建数据透视表。

【问题讨论】:

  • 嘿,你看到更新了吗?

标签: java excel apache-poi pivot-table


【解决方案1】:

“快速指南”已经过时了。

change log 将此 bugzilla issue 称为已解决。

可以看代码here:

这是一个sn-p:

 public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = (XSSFSheet) wb.createSheet();

        //Create some data to build the pivot table on
        setCellData(sheet);

        XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:D4"), new CellReference("H5"));
        //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);

        FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx");
        wb.write(fileOut);
        fileOut.close();
    }

【讨论】:

  • 能否请您添加 jar 文件列表,因为我找不到 XSSFPivotTable 和 DataConsolidateFunction 所需的文件
  • @ashishbalchandi 你有什么版本的 POI?
  • @ashishbalchandi 尝试升级,并将其添加到类路径/构建路径中
  • 是的,但最新的稳定版本是我已经拥有的 Apache POI 3.10.1。 B/w 你的系统有什么版本??
  • 无论如何谢谢@Pureferret 我已经提出这个问题很久了..我会再次努力的。!并带着问题回来..:P
【解决方案2】:

不,你不能。推荐here

• 图表 您目前无法创建图表。但是,您可以创建 Excel 中的图表,使用 HSSF 修改图表数据值并编写 新的电子表格出来了。这是可能的,因为 POI 试图保持 现有记录尽可能完整。

• 宏 无法创建宏。但是,阅读和重写 包含宏的文件将安全地保留宏。

• 数据透视表 不支持生成数据透视表。它一直 报告包含数据透视表的文件可以被读取和重写 安全。

【讨论】:

  • 这不再正确 - XSSF 现在支持创建数据透视表,正如您链接到当前页面所说的那样
【解决方案3】:

是的,您可以创建。需要依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.3</version>
        </dependency>

输入 Excel 文件

在同一张工作表上创建数据透视表的 Java 代码


import java.io.File;
import java.io.FileOutputStream;
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.*;

public class Test {


    public static void main(String[] args) throws Exception{
        /* Read the input file that contains the data to pivot */
        FileInputStream input_document = new FileInputStream(new File("input-file-path\\Pivot-Cube.xlsx"));
        /* Create a POI XSSFWorkbook Object from the input file */
        XSSFWorkbook my_xlsx_workbook = new XSSFWorkbook(input_document);
        /* Read Data to be Pivoted - we have only one worksheet */
        XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0);
        /* Get the reference for Pivot Data */
        AreaReference a=new AreaReference("A1:C5");
        /* Find out where the Pivot Table needs to be placed */
        CellReference b=new CellReference("I5");
       

        /* Create Pivot Table */
        XSSFPivotTable pivotTable = sheet.createPivotTable(a,b, sheet);
        /* Add filters */
        pivotTable.addReportFilter(0);
        pivotTable.addRowLabel(1);
        pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2);
        /* Write Pivot Table to File */
        FileOutputStream output_file = new FileOutputStream(new File("output-file-path\\POI_XLS_Pivot_Example.xlsx"));
        my_xlsx_workbook.write(output_file);
        input_document.close();
    }


}

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多