java.io.EOFException:ApacheのPOIを使用してZLIB入力ストリームの予期しない終了

アンドリューSuruceanu:

私は、Apache POIを使用してExcelピボットテーブルを作成しよう。
私は、ブック内の書き込みデータにしようとすると、現時点では、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();


}

例外のスタックトレースにあります:

スレッド「メイン」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)で
java.io.EOFException:によって引き起こさMain.main(Main.java:14)での予期しない終了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もっと

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=222487&siteId=1