【问题标题】:java.io.EOFException: Unexpected end of ZLIB input stream using Apache POIjava.io.EOFException:使用 Apache POI 的 ZLIB 输入流意外结束
【发布时间】:2018-09-18 15:16:25
【问题描述】:

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

org.apache.poi.ooxml.POIXMLException: java.io.EOFException: Unexpected 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的异常:

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

【问题讨论】:

    标签: java excel exception apache-poi pivot-table


    【解决方案1】:

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

    Workbook workbook = new XSSFWorkbook(pathToWorkbook);
    

    到:

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

    希望对您有所帮助! :)

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我试过了。包含所有工作表的新文件已创建并且可以正常工作,但是当我尝试Files.move()
      • 为了解决这个问题,我刚刚使用这个 stings new File(pathToWorkbook).renameTo(new File("tmpName.xlsx")); Files.delete(Paths.get("tmpName.xlsx")); 重命名了文件
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-20
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多