有没有办法使用 Apache POI 在 Excel 中创建数据透视表?
问题描述:
我目前正在研究 Excel 的自动化,并补充说我已经很好地利用了 Apache POI 库.
I am currently working on the automation of Excel, and add such I have made a good use of the Apache POI library.
由于我的 excel 工作簿的各个列中存储了大量数据,因此我正在尝试创建数据透视表.
As I have so much data stored in my excel workbook in various columns, that I'm trying to create a pivot table.
有没有办法使用 POI 创建数据透视表?
我的要求是我需要在新的 excel 工作簿或存储数据的同一工作簿中创建数据透视表.
My requirement is that I need to create the pivot table in a new excel workbook or in the same workbook where I store my data.
答
快速指南"已经过时了.
The 'Quick Guide' is quite out of date.
变更日志指的是这个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();
}