我目前正致力于Excel的自动化,并添加了这样我已经很好地利用了Apache POI库。
由于我在excel工作簿的各个列中存储了大量数据,因此我尝试创建数据透视表。
有没有办法使用POI创建数据透视表?
我的要求是我需要在新的Excel工作簿或我存储数据的同一工作簿中创建数据透视表。
答案 0 :(得分:21)
“快速指南”已经过时了。
change log将此bugzilla issue称为已解决。
您可以看到代码here:
这是一个片段:
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();
}
答案 1 :(得分:1)
你不能放弃here
•图表您目前无法创建图表。但是你可以创建 在Excel中的图表,使用HSSF修改图表数据值并写入 新的电子表格。这是可能的,因为POI试图保持 现有记录尽可能完整。
•无法创建宏宏。但是,阅读和重写 包含宏的文件将安全地保留宏。
•数据透视表不支持生成数据透视表。它一直 报告说,可以读取和重写包含数据透视表的文件 安全。