gpt4 book ai didi

java.io.EOFException : Unexpected end of ZLIB input stream using Apache POI

转载 作者:行者123 更新时间:2023-12-01 22:16:46 32 4
gpt4 key购买 nike

我尝试使用 Apache POI 创建 Excel 数据透视表。
目前,当我尝试在工作簿中写入数据时 workbook.write(fileOut); 我遇到异常

org.apache.poi.ooxml.POIXMLException: java.io.EOFException: Unexpected end of ZLIB input stream

有该类的代码:

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:

Exception in thread "main" org.apache.poi.ooxml.POIXMLException: java.io.EOFException: Unexpected end of ZLIB input stream
at org.apache.poi.ooxml.POIXMLDocument.getProperties(POIXMLDocument.java:147)
at org.apache.poi.ooxml.POIXMLDocument.write(POIXMLDocument.java:240)
at PivotTable.createPivotTable(PivotTable.java:50)
at Main.main(Main.java:14)
Caused by: java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at org.apache.commons.compress.archivers.zip.InflaterInputStreamWithStatistics.fill(
InflaterInputStreamWithStatistics.java:52)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at org.apache.commons.compress.archivers.zip.InflaterInputStreamWithStatistics.read(
InflaterInputStreamWithStatistics.java:67)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:122)
at org.apache.commons.compress.archivers.zip.InflaterInputStreamWithStatistics.read(
InflaterInputStreamWithStatistics.java:58)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.read(ZipArchiveThresholdInputStream.java:69)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2890)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:674)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:805)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3414)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1272)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1259)
at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
at org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument$Factory.parse(Unknown Source)
at org.apache.poi.ooxml.POIXMLProperties.(POIXMLProperties.java:81)
at org.apache.poi.ooxml.POIXMLDocument.getProperties(POIXMLDocument.java:145)
... 3 more

最佳答案

我遇到了同样的问题,我通过更改行解决了它:

Workbook workbook = new XSSFWorkbook(pathToWorkbook);

至:

Workbook workbook = new XSSFWorkbook(new FileInputStream(pathToWorkbook));

希望对您有帮助! :)

关于java.io.EOFException : Unexpected end of ZLIB input stream using Apache POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52389798/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com