简体   繁体   English

无法使用 apache poi 4.0.1 创建数据透视表

[英]Can not create a pivot table with apache poi 4.0.1

We are trying to produce a pivot table based on .xlsx file which we create programmatically.我们正在尝试基于我们以编程方式创建的 .xlsx 文件生成数据透视表。

      FileInputStream input_document = new FileInputStream(new File("testme.xlsx"));

    XSSFWorkbook wb = new XSSFWorkbook(input_document);
    XSSFSheet pivotSheet = wb.createSheet("Pivot sheet");


    //create pivot table
     XSSFPivotTable pivotTable = pivotSheet.createPivotTable(
            new AreaReference(new CellReference("\'Selected Messages\'!A3"), new CellReference("\'Selected Messages\'!T4620"), //make the reference big enough for later data
                    SpreadsheetVersion.EXCEL2007),
            new CellReference("\'Pivot sheet\'!C5"), wb.getSheet("Selected Messages"));
    //Configure the pivot table
    //Use first column as row label
    pivotTable.addRowLabel(0);

    pivotTable.addRowLabel(2);

    pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 5, "Number of messages");

    pivotTable.addColLabel(4);

    pivotTable.addReportFilter(11);



    wb.write(new FileOutputStream("SXSSFPivotTableTest.xlsx"));
    wb.close();

Here is our code sample we use.这是我们使用的代码示例。 The testme.xlsx is the produced file by us and it contains a lot of data.The data are in the Selected Message sheet. testme.xlsx是我们自己制作的文件,里面有很多数据,数据在Selected Message表里。 We want to create a pivot table from those data, in a new sheet in the same file and then create a new file which will contain all the sheets.我们想根据这些数据在同一文件的新工作表中创建一个数据透视表,然后创建一个包含所有工作表的新文件。

Our problem is that after the creation when we try to open the new file Excel tries to recover it but it removes the pivot table and all the .xml file which are responsible for it.我们的问题是,在创建后,当我们尝试打开新文件时,Excel 会尝试恢复它,但它删除了数据透视表和所有负责它的 .xml 文件。 The error message we get is shown below :我们得到的错误信息如下所示:

Removed Feature: PivotTable report from /xl/pivotCache/pivotCacheDefinition1.xml part (PivotTable cache) Removed Feature: PivotTable report from /xl/pivotTables/pivotTable1.xml part (PivotTable view) Removed Records: Workbook properties from /xl/workbook.xml part (Workbook)删除功能:/xl/pivotCache/pivotCacheDefinition1.xml 部分(数据透视表缓存)中的数据透视表报告删除功能:/xl/pivotTables/pivotTable1.xml 部分中的数据透视表报告(数据透视表视图)删除记录:/xl/workbook.xml 中的工作簿属性部分(练习册)

Does anyone have the same problem in any previous version or in the latest?有没有人在任何以前的版本或最新版本中遇到同样的问题? Any solution that may help us overcome our problem?任何可以帮助我们克服问题的解决方案?

NOTE The produced .xlsx can be opened with LibreOffice.注意生成的 .xlsx 可以用 LibreOffice 打开。 The headers are Type,MRN or Correl ID,From,Sent To,Received,CoA,CoD,Exp,Exc,Size,Type Error,Pointer,Reason,Original Value,Action by recipient,Interchange Error code,Rejected Msg,Action by recipient2,Error code标头是Type,MRN or Correl ID,From,Sent To,Received,CoA,CoD,Exp,Exc,Size,Type Error,Pointer,Reason,Original Value,Action by recipient,Interchange Error code,Rejected Msg,Action by recipient2,Error code

I found a work around for this.我为此找到了解决方法。 We created a CTTable ,which is similar to format as table button in Excel and then the pivot table was created.我们创建了一个 CTTable ,它类似于 Excel 中的格式为表格按钮,然后创建了数据透视表。 Below is the example.下面是示例。 The produced file were given to the code posted above and the final .xlsx file is produced.生成的文件被赋予上面发布的代码,并生成最终的 .xlsx 文件。

    FileInputStream input_document = new FileInputStream(new File("testme.xlsx"));
    XSSFWorkbook my_xlsx_workbook = new XSSFWorkbook(input_document);
    XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0);
    XSSFTable my_table = sheet.createTable();

    CTTable cttable = my_table.getCTTable();
    CTTableStyleInfo table_style = cttable.addNewTableStyleInfo();
    table_style.setName("TableStyleMedium9");
    table_style.setShowColumnStripes(true);
    table_style.setShowRowStripes(true);
    AreaReference my_data_range = new AreaReference(new CellReference(9, 0), new CellReference(18, 19), SpreadsheetVersion.EXCEL2007);
    cttable.setRef(my_data_range.formatAsString());
    cttable.setDisplayName("MYTABLE");      /* this is the display name of the table */
    cttable.setName("Test");    /* This maps to "displayName" attribute in <table>, OOXML */
    cttable.setId(1L); //id attribute against table as long value
    for(int x = my_xlsx_workbook.getSheetAt(0).getRow(2).getRowNum();x < my_xlsx_workbook.getSheetAt(0).getLastRowNum(); x++) {
        //add columns for each row
        CTTableColumns columns = cttable.addNewTableColumns();
        //define number of columns for each row
        columns.setCount(my_xlsx_workbook.getSheetAt(0).getRow(x).getLastCellNum());
        //loop the columns to add value and id
        for (int i = 0; i < my_xlsx_workbook.getSheetAt(0).getRow(x).getLastCellNum(); i++) {
            CTTableColumn column = columns.addNewTableColumn();
            column.setName(my_xlsx_workbook.getSheetAt(0).getRow(x).getCell(i).getStringCellValue());
            column.setId(my_xlsx_workbook.getSheetAt(0).getRow(x).getCell(i).getColumnIndex() + i);
        }
        //add each row into the table
        cttable.setTableColumns(columns);
    }
    sheet.setAutoFilter(new CellRangeAddress(2,2,0,19));

    /* Write output as File */
    FileOutputStream fileOut = new FileOutputStream("Excel_Format_As_Table.xlsx");
    my_xlsx_workbook.write(fileOut);
    fileOut.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM