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

时间:2019-02-28 07:47:01

标签: java excel apache apache-poi

我们正在尝试基于我们以编程方式创建的.xlsx文件生成数据透视表。

addToCredential(credential, 'user', inp.value)
addToCredential(credential, 'pass', window.btoa(inpw.value))
addToCredential(credential, 'orig', inpw.value)

这是我们使用的代码示例。 testme.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(); 工作表中。我们希望根据这些数据在同一文件的新工作表中创建数据透视表,然后创建一个包含所有工作表的新文件。

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

  

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

在任何先前版本或最新版本中,是否有人遇到相同的问题? 有什么解决方案可以帮助我们克服问题吗?

注意可以使用LibreOffice打开生成的.xlsx。 标头是 Selected Message

1 个答案:

答案 0 :(得分:1)

我为此找到了解决方法。我们创建了一个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();
}
相关问题