有没有办法使用Apache POI在Excel中创建数据透视表?

Sab*_*hSS 9 java excel pivot-table apache-poi

我目前正致力于Excel的自动化,并添加了这样我已经很好地利用了Apache POI库.

由于我在excel工作簿中存储了大量数据,因此我正在尝试创建数据透视表.

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

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

Pur*_*ret 21

"快速指南"已经过时了.

更改日志是指这个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();
    }
Run Code Online (Sandbox Code Playgroud)