java.io.EOFException: Неожиданный конец входного потока ZLIB с помощью Apache POI

Андрей Суручану :

Я пытаюсь создать Excel сводной таблицы с помощью Apache POI.
В тот момент, когда я пытаюсь записи данных в книге 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 исключения:

Исключение в потоке «основного» org.apache.poi.ooxml.POIXMLException: java.io.EOFException: Неожиданный конец входного потока ZLIB
на org.apache.poi.ooxml.POIXMLDocument.getProperties (POIXMLDocument.java:147)
в орг. 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 (Unknown Source)
на org.apache.poi.ooxml.POIXMLProperties. (POIXMLProperties.java:81)
в org.apache.poi.ooxml.POIXMLDocument.getProperties (POIXMLDocument.java:145 ) ... 3 подробнее

rzwitserloot:

Уверен , что проблема вы перезаписать файл. Попробуйте сохранить на другой путь. Если вы все еще хотите , чтобы перезаписать файл, сохранить что - то другое, удалить оригинал, а затем переименовать файл , который вы написали на место:

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

рекомендация

отhttp://43.154.161.224:23101/article/api/json?id=222483&siteId=1