Java ApachePOI-有没有办法在源表是SXSSFSheet的情况下创建透视表?

Java ApachePOI-有没有办法在源表是SXSSFSheet的情况下创建透视表?,java,excel,apache-poi,pivot,Java,Excel,Apache Poi,Pivot,情况就是这样: 我必须使用SXSSFWorkbook(XSSFWorkbook的流式版本)来创建我的Excel,因为我必须创建一个包含700000/800000行和大约20列的工作表此表代表我的最终数据透视的源表。 SXSSFWorkbook workbook(); XSSFSheet pivotSheet = workbook.getXSSFWorkbook().createSheet("Pivot sheet"); AreaReference ar = ....: CellReference

情况就是这样: 我必须使用SXSSFWorkbook(XSSFWorkbook的流式版本)来创建我的Excel,因为我必须创建一个包含700000/800000行和大约20列的工作表此表代表我的最终数据透视的源表。

SXSSFWorkbook workbook();
XSSFSheet pivotSheet = workbook.getXSSFWorkbook().createSheet("Pivot sheet");
AreaReference ar = ....:
CellReference cr = ....;
XSSFPivotTable pivotTable = pivotSheet.createPivotTable(ar, cr); // ERROR!!
问题是,当我尝试在该源上创建此数据透视时,XSSFPivotTable.createPivotTable方法不起作用,尽管AreaReference和CellReference参数是可以的

如果我使用的XSSFWorkbook(不是流式版本)行数较少,一切都可以,但我没有达到我的目标

有人能给我一个解决办法吗非常感谢

Stefano

可以从
XSSFWorkbook
创建

因此,我要做的是创建一个
XSSFWorkbook
,其中至少包含数据标题的
XSSFSheet
,并为透视表创建另一个
XSSFSheet
。然后在此
XSSFSheet
上创建
XSSFPivotTable
,但将对数据表的引用设置为足够大,以便以后使用数据

然后,我将从这个
XSSFWorkbook
创建
SXSSFWorkbook
,将数据表作为
SXSSFSheet
获取,然后将大量数据流式传输到数据表中

完整示例:

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

import org.apache.poi.xssf.streaming.*;

import java.util.Random;
import java.io.FileOutputStream;

class SXSSFPivotTableTest {

 private static void streamCellData(Sheet sheet, int rowsCount) {

  for (int r = 1; r <= rowsCount; r++) {
   Row row = sheet.createRow(r);
   Cell cell = row.createCell(0);
   cell.setCellValue("Name " + ((r-1) % 4 + 1));
   cell = row.createCell(1);
   cell.setCellValue(r * new java.util.Random().nextDouble());
   cell = row.createCell(2);
   cell.setCellValue(r * new java.util.Random().nextDouble());
   cell = row.createCell(3);
   cell.setCellValue("City " + ((r-1) % 3 + 1));  
  }

 }

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

  int rowsCount = 1000000;

  //first create XSSFWorkbook
  XSSFWorkbook wb = new XSSFWorkbook();

  //create XSSFSheet with at least the headings
  XSSFSheet sheet = wb.createSheet("Sheet1");
  Row row = sheet.createRow(0);
  Cell cell = row.createCell(0);
  cell.setCellValue("Name");
  cell = row.createCell(1);
  cell.setCellValue("Value1");
  cell = row.createCell(2);
  cell.setCellValue("Value2");
  cell = row.createCell(3);
  cell.setCellValue("City");

  //create XSSFSheet for pivot table
  XSSFSheet pivotSheet = wb.createSheet("Pivot sheet");

  //create pivot table
  XSSFPivotTable pivotTable = pivotSheet.createPivotTable(
    new AreaReference(new CellReference("Sheet1!A1"), 
                      new CellReference("Sheet1!D" + (rowsCount +1)), //make the reference big enough for later data
                      SpreadsheetVersion.EXCEL2007),
    new CellReference("A5"));
  //Configure the pivot table
  //Use first column as row label
  pivotTable.addRowLabel(0);
  //Sum up the second column
  pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
  //Avarage the third column
  pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2);
  //Add filter on forth column
  pivotTable.addReportFilter(3);

  //now create SXSSFWorkbook from XSSFWorkbook
  SXSSFWorkbook swb = new SXSSFWorkbook(wb);
  SXSSFSheet ssheet = swb.getSheet("Sheet1");

  //now stream the big amount of data to build the pivot table on into Sheet1
  streamCellData(ssheet, rowsCount);

  swb.write(new FileOutputStream("SXSSFPivotTableTest.xlsx"));
  swb.close();
  swb.dispose();

 }
}
import org.apache.poi.xssf.usermodel.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入org.apache.poi.ss.SpreadsheetVersion;
导入org.apache.poi.xssf.streaming.*;
导入java.util.Random;
导入java.io.FileOutputStream;
SXSSFPivotTableTest类{
专用静态void streamCellData(图纸,int ROWSCONT){

对于(int r=1;r),我在Pivot的源表SXSSFsheet(streaming)上强调我的问题