簡體   English   中英

有沒有辦法使用 Apache POI 在 Excel 中創建數據透視表?

[英]Is there any way to create a Pivot Table in Excel using Apache POI?

我目前正在研究 Excel 的自動化,並補充說我已經很好地利用了 Apache POI 庫。

由於我的 excel 工作簿的各個列中存儲了大量數據,因此我正在嘗試創建一個數據透視表。

有沒有辦法使用 POI 創建數據透視表?

我的要求是我需要在新的 excel 工作簿或存儲數據的同一工作簿中創建數據透視表。

“快速指南”已經過時了。

更改日志將此bugzilla問題視為已解決。

你可以在這里看到代碼:

這是一個片段:

 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();
    }

不,你不能在這里

•圖表您目前無法創建圖表。 但是,您可以在Excel中創建圖表,使用HSSF修改圖表數據值並寫出新的電子表格。 這是可能的,因為POI試圖盡可能保持現有記錄的完整性。

•無法創建宏宏。 但是,讀取和重寫包含宏的文件將安全地保留宏。

•數據透視表不支持生成數據透視表。 據報道,可以安全地讀取和重寫包含數據透視表的文件。

是的,你可以創造。 需要依賴

        <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();
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM