java.io.EOFException:使用 Apache POI 的 ZLIB 输入流意外结束 [英] java.io.EOFException: Unexpected end of ZLIB input stream using Apache POI

查看:194
本文介绍了java.io.EOFException:使用 Apache POI 的 ZLIB 输入流意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to create Excel Pivot Table using Apache POI.
At the moment, when I try to write data in a workbook workbook.write(fileOut); I get exception

org.apache.poi.ooxml.POIXMLException: java.io.EOFException: 意外ZLIB 输入流结束

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();


}

还有异常的堆栈跟踪:

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

推荐答案

很确定问题是您覆盖了文件.尝试保存到不同路径.如果您仍想覆盖该文件,请保存到其他位置,删除原始文件,然后将您写入的文件重命名到位:

Pretty sure the problem is you overwriting the file. Try to save to a different path. If you still want to overwrite the file, save to something else, delete the original, then rename the file you wrote into place:

try (FileOutputStream fileOut = new FileOutputStream(pathToWorkbook + ".new")) {
    workbook.write(fileOut);
}
Files.delete(Paths.get(pathToWorkbook));
Files.move(Paths.get(pathToWorkbook + ".new"), Paths.get(pathToWorkbook));

这篇关于java.io.EOFException:使用 Apache POI 的 ZLIB 输入流意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆