我正在使用 poi 创建数据透视表并将其保存到另一张表中,但之后我想读取最近创建的数据透视表的数据,但不知何故我无法获得它对于创建数据透视表我正在使用:
File file = new File(filepath);
try (FileInputStream inputStream = new FileInputStream(file);)
{
XSSFWorkbook testData = null;
testData = new XSSFWorkbook(inputStream);
XSSFSheet testDataSheet = testData.getSheet(sheetname);
XSSFSheet sheet2 = testData.createSheet("pivot");
XSSFPivotTable pivotTable = sheet2.createPivotTable(new AreaReference(range, null), new CellReference("A1"), testDataSheet);
pivotTable.addRowLabel(rownum);
pivotTable.addColumnLabel(dataConsolidateFunction, value);
FileOutputStream fileOut = new FileOutputStream(pivotsheetpath);
testData.write(fileOut);
fileOut.close();
inputStream.close();
testData.close();
}
}
从excel中获取数据:
String[][] data = null;
File file = new File(filePath);
try (FileInputStream inputStream = new FileInputStream(file);)
{
int row = 0;
int column = 0;
DataFormatter dataFormatter = new DataFormatter();
XSSFWorkbook testData = null;
String fileExtensionName = FilenameUtils.getExtension(filePath);
if (fileExtensionName.equals("xlsx"))
testData = new XSSFWorkbook(inputStream);
XSSFSheet testDataSheet = testData.getSheet(sheetName);
row = testDataSheet.getLastRowNum() - testDataSheet.getFirstRowNum();
column = testDataSheet.getRow(0).getLastCellNum() - testDataSheet.getRow(0).getFirstCellNum();
data = new String[row + 1][column];
for (Row rows : testDataSheet)
for (Cell cell : rows)
data[rows.getRowNum()][cell.getColumnIndex()] = dataFormatter.formatCellValue(cell);
testData.close();
return data;
}