簡體   English   中英

無法使用 apache poi 4.0.1 創建數據透視表

[英]Can not create a pivot table with 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 Message表里。 我們想根據這些數據在同一文件的新工作表中創建一個數據透視表,然后創建一個包含所有工作表的新文件。

我們的問題是,在創建后,當我們嘗試打開新文件時,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

我為此找到了解決方法。 我們創建了一個 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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM