我尝试使用Apache POI创建Excel Pivot表。
目前,当我尝试在工作簿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();
}
还有异常的StackTrace:
线程“ main”中的异常org.apache.poi.ooxml.POIXMLException: java.io.EOFException:ZLIB输入流的意外结束
在 org.apache.poi.ooxml.POIXMLDocument.getProperties(POIXMLDocument.java:147)
org.apache.poi.ooxml.POIXMLDocument.write(POIXMLDocument.java:240)
在PivotTable.createPivotTable(PivotTable.java:50)在
Main.main(Main.java:14)原因:java.io.EOFException:意外 ZLIB输入流的结尾
java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
org.apache.commons.compress.archivers.zip.InflaterInputStreamWithStatistics.fill(InflaterInputStreamWithStatistics.java:52)
java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
org.apache.commons.compress.archivers.zip.InflaterInputStreamWithStatistics.read(InflaterInputStreamWithStatistics.java:67)
java.util.zip.InflaterInputStream.read(InflaterInputStream.java:122)
org.apache.commons.compress.archivers.zip.InflaterInputStreamWithStatistics.read(InflaterInputStreamWithStatistics.java:58)
在java.io.FilterInputStream.read(FilterInputStream.java:83)
org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.read(ZipArchiveThresholdInputStream.java:69)
com.sun.org.apache.xerces.internal.impl.XMLEntityManager $ RewindableInputStream.read(XMLEntityManager.java:2890)
com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:674)
com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:805)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:643)
org.apache.xmlbeans.impl.store.Locale $ SaxLoader.load(Locale.java:3414)
org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1272)
org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1259)
org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument $ Factory.parse(未知 来源)
org.apache.poi.ooxml.POIXMLProperties。(POIXMLProperties.java:81)
org.apache.poi.ooxml.POIXMLDocument.getProperties(POIXMLDocument.java:145) ...还有3个
答案 0 :(得分:3)
可以肯定的问题是您覆盖了文件。尝试保存到不同路径。如果仍要覆盖文件,请保存到其他位置,删除原始文件,然后将写入的文件重命名为适当的位置:
try (FileOutputStream fileOut = new FileOutputStream(pathToWorkbook + ".new")) {
workbook.write(fileOut);
}
Files.delete(Paths.get(pathToWorkbook));
Files.move(Paths.get(pathToWorkbook + ".new"), Paths.get(pathToWorkbook));
答案 1 :(得分:3)
我遇到了同样的问题,我通过更改以下行解决了该问题:
Workbook workbook = new XSSFWorkbook(pathToWorkbook);
收件人:
Workbook workbook = new XSSFWorkbook(new FileInputStream(pathToWorkbook));
希望有帮助! :)
答案 2 :(得分:1)
在workbook.write()方法中似乎有一个小错误。我发现2解决方法。 首先是为FileOutputStream指定另一个(虚拟)文件名。当工作簿关闭时,两个文件都将被填充。 其次,也是更好的方法是为workbook.write()方法指定伪OutputStream:
OutputStream dummyOutputStream = new OutputStream() {
@Override
public void write(int b) throws IOException {
}
};
workbook.write(dummyOutputStream);
dummyOutputStream.close();
workbook.close();