Анд*_*ану 5 java excel pivot-table exception apache-poi
我尝试使用 Apache POI 创建 Excel 数据透视表。
目前,当我尝试在工作簿中写入数据时,出现workbook.write(fileOut);异常
org.apache.poi.ooxml.POIXMLException:java.io.EOFException:ZLIB 输入流意外结束
有类的代码:
public class PivotTable {
public static void createPivotTable(String pathToWorkbook, String sheetName) throws IOException {
Workbook workbook = new XSSFWorkbook(pathToWorkbook);
XSSFSheet sheet = (XSSFSheet) workbook.getSheet(sheetName);
int firstRowInd = sheet.getFirstRowNum();
int lastRowInd = sheet.getLastRowNum();
int firstCellInd = sheet.getRow(0).getFirstCellNum();
int lastCellInd = sheet.getRow(0).getLastCellNum() - 1;
//Specifying top left ant the bottom right of the table data
CellReference topLeft = new CellReference(firstRowInd, firstCellInd);
CellReference botRight = new CellReference(lastRowInd, lastCellInd);
//The area of data in table
AreaReference aref = new AreaReference(topLeft, botRight, SpreadsheetVersion.EXCEL2007);
//Position of the pivot table
CellReference pos = new CellReference(firstRowInd + 4, lastCellInd + 1);
//Creating the pivot table
XSSFPivotTable pivotTable = sheet.createPivotTable(aref, pos);
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(2);
pivotTable.addColLabel(3);
FileOutputStream fileOut = new FileOutputStream(pathToWorkbook);
workbook.write(fileOut);
fileOut.close();
}
Run Code Online (Sandbox Code Playgroud)
还有异常的堆栈跟踪:
public class PivotTable {
public static void createPivotTable(String pathToWorkbook, String sheetName) throws IOException {
Workbook workbook = new XSSFWorkbook(pathToWorkbook);
XSSFSheet sheet = (XSSFSheet) workbook.getSheet(sheetName);
int firstRowInd = sheet.getFirstRowNum();
int lastRowInd = sheet.getLastRowNum();
int firstCellInd = sheet.getRow(0).getFirstCellNum();
int lastCellInd = sheet.getRow(0).getLastCellNum() - 1;
//Specifying top left ant the bottom right of the table data
CellReference topLeft = new CellReference(firstRowInd, firstCellInd);
CellReference botRight = new CellReference(lastRowInd, lastCellInd);
//The area of data in table
AreaReference aref = new AreaReference(topLeft, botRight, SpreadsheetVersion.EXCEL2007);
//Position of the pivot table
CellReference pos = new CellReference(firstRowInd + 4, lastCellInd + 1);
//Creating the pivot table
XSSFPivotTable pivotTable = sheet.createPivotTable(aref, pos);
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(2);
pivotTable.addColLabel(3);
FileOutputStream fileOut = new FileOutputStream(pathToWorkbook);
workbook.write(fileOut);
fileOut.close();
}
Run Code Online (Sandbox Code Playgroud)
小智 11
我遇到了同样的问题,我通过更改行解决了它:
Workbook workbook = new XSSFWorkbook(pathToWorkbook);
Run Code Online (Sandbox Code Playgroud)
到:
Workbook workbook = new XSSFWorkbook(new FileInputStream(pathToWorkbook));
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!:)
很确定问题是您覆盖了文件。尝试保存到不同的路径。如果您仍然想覆盖该文件,请保存到其他位置,删除原始文件,然后将您写入的文件重命名到位:
try (FileOutputStream fileOut = new FileOutputStream(pathToWorkbook + ".new")) {
workbook.write(fileOut);
}
Files.delete(Paths.get(pathToWorkbook));
Files.move(Paths.get(pathToWorkbook + ".new"), Paths.get(pathToWorkbook));
Run Code Online (Sandbox Code Playgroud)