首页 > 解决方案 > 无法使用 apache poi 4.0.1 创建数据透视表

问题描述

我们正在尝试基于我们以编程方式创建的 .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();

这是我们使用的代码示例。testme.xlsx是我们制作的文件,里面有很多数据。数据在Selected Messagesheet里面。我们想从这些数据创建一个数据透视表,在同一个文件的一个新工作表中,然后创建一个包含所有工作表的新文件。

我们的问题是,在创建后,当我们尝试打开新文件时,Excel 会尝试恢复它,但它会删除数据透视表和所有负责它的 .xml 文件。我们得到的错误信息如下所示:

已删除功能:来自 /xl/pivotCache/pivotCacheDefinition1.xml 部分(数据透视表缓存)的数据透视表报告 已删除功能:来自 /xl/pivotTables/pivotTable1.xml 部分(数据透视表视图)的数据透视表报告 已删除记录:来自 /xl/workbook.xml 的工作簿属性部分(工作簿)

在任何以前的版本或最新版本中是否有人有同样的问题?任何解决方案可以帮助我们克服我们的问题?

注意生成的 .xlsx 可以用 LibreOffice 打开。标题是 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

标签: javaexcelapache-poi

解决方案


我找到了解决这个问题的方法。我们创建了一个 CTTable ,类似于 Excel 中的表格格式按钮,然后创建了数据透视表。下面是示例。将生成的文件提供给上面发布的代码,并生成最终的 .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();
}

推荐阅读