簡體   English   中英

Apache POI - 有沒有辦法計算 pivot 表中字符串的出現次數?

[英]Apache POI - Is there a way to count occurrence of a string in a pivot table?

我正在使用 Apache Poi 的 4.1.2 版,我有這個數據集:

String[] headers = new String[] { "Company", "Status" };

Object[][] sheetData = {
                {"Company 1", "OK"},
                {"Company 1", "NG"},
                {"Company 2", "NG"},
                {"Company 1", "OK"},
                {"Company 3", "OK"},
                {"Company 1", "NG"},
        };

我正在嘗試使用 Apache POI 創建一個 pivot 表,該 POI 對第二列中字符串的出現進行分組和計數。 我試過了:

pivotTable.addRowLabel(0);
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1);

但它仍然以某種方式計算第一列的出現次數。

我正在嘗試創建的 pivot 表:

在此處輸入圖像描述

以及正在生成的 pivot 表:

在此處輸入圖像描述

pivot 表顯示為您嘗試創建的表,將列1 = B (狀態)顯示為列 label,使用DataConsolidateFunction以及列 ZD304BA20E96D87411588EEABAC850E34 使用列。 因此,這里的 pivot 表中的一列有兩個不同的屬性。 這使它變得復雜。

DataConsolidateFunction列 label 已經使用pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1)完成。 這也設置了列的dataField設置。

對於用於標記列apache poi 4.1.2的列 label 提供了方法XSSFPivotTable.addColLabel 但是此方法刪除了dataField設置。 所以我們需要使用低級別ooxml-shemas類來設置它。

命令的順序在這里很重要,因為它們影響同一列。 首先做pivotTable.addColumnLabel然后做pivotTable.addColLabel 否則addColumnLabel將設置dataField設置,但會從該列中刪除axis="axisCol"設置。 但由於 pivot 表中的兩個不同屬性,該列需要兩個設置。

完整示例:

import java.io.FileOutputStream;

import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.usermodel.DataConsolidateFunction;
import org.apache.poi.xssf.usermodel.*;

class CreatePivotTable {

 public static void main(String[] args) throws Exception {

  String[] headers = new String[] { "Company", "Status" };
  Object[][] sheetData = {
   {"Company 1", "OK"},
   {"Company 1", "NG"},
   {"Company 2", "NG"},
   {"Company 1", "OK"},
   {"Company 3", "OK"},
   {"Company 1", "NG"},
  };

  try (XSSFWorkbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("./ExcelResult.xlsx") ) {

   XSSFSheet dataSheet = workbook.createSheet("Data");
   XSSFRow row;
   XSSFCell cell;
   int r = 0;
   row = dataSheet.createRow(r++);
   int c = 0;
   for (String header : headers) {
    cell = row.createCell(c++);
    cell.setCellValue(header);
   }
   for (Object[] dataRow : sheetData) {
    row = dataSheet.createRow(r++);
    c = 0;
    for (Object value : dataRow) {
     cell = row.createCell(c++);
     if (value instanceof String) {
      cell.setCellValue((String)value);
     } //else if...
    }
   } 

   XSSFSheet pivotSheet = workbook.createSheet("Pivot");

   AreaReference areaReference = new AreaReference(
    new CellReference(0, 0),
    new CellReference(sheetData.length, headers.length-1),
    SpreadsheetVersion.EXCEL2007);

   XSSFPivotTable pivotTable = pivotSheet.createPivotTable(areaReference, new CellReference("A4"), dataSheet);

   pivotTable.addRowLabel(0);
   pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1);

   pivotTable.addColLabel(1);
   //Method addColLabel removes the dataField setting. So we need set it new.
   pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(1)
    .setDataField(true);

   workbook.write(fileout);

  }

 }
}

暫無
暫無

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

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