Apache POI工作簿写入尝试期间的POIXMLException

时间:2018-10-03 05:33:08

标签: java nullpointerexception apache-poi nio

下面的代码应该复制excel文件Pristine Copy(.xlsx文件)。副本的名称为copyTest。复制完成后,当我手动打开它时,excel文件具有预期的内容。但是,当我尝试在代码中打开新文件并写入文件时,它将引发错误。

public static void main(String[] args)throws IOException, InvalidFormatException {

    String fileLocation = "C:\\Users\\someUser\\Documents\\Project Docs\\Pristine Copy.xlsx";
    String newFileLocation = "C:\\Users\\someUser\\Documents\\Project Docs\\copyTest.xlsx";

    Path source = Paths.get(fileLocation);
    Path dest = Paths.get(newFileLocation);
    Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);



    Workbook newWorkbook = WorkbookFactory.create(new File(newFileLocation));   
    System.out.println("There are "+newWorkbook.getNumberOfSheets()+" sheets.");

    MongoTest mt = new MongoTest(); 
    Document doc = mt.fetchRecord("HD123456", "SSE");
    Sheet sheet = newWorkbook.getSheet("SSE");
    writeToSheet(sheet, doc);

    FileOutputStream out = new FileOutputStream(newFileLocation);
    newWorkbook.write(out);
    out.close();
    System.out.println("Check");
    newWorkbook.close();

}

堆栈跟踪如下:

Exception in thread "main" org.apache.poi.POIXMLException: java.lang.NullPointerException
    at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:147)
    at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:225)
    at com.cerner.excelTest.ExcelFile.main(ExcelFile.java:63)
Caused by: java.lang.NullPointerException
    at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:211)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at org.apache.poi.util.DocumentHelper.readDocument(DocumentHelper.java:140)
    at org.apache.poi.POIXMLTypeLoader.parse(POIXMLTypeLoader.java:163)
    at org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument$Factory.parse(Unknown Source)
    at org.apache.poi.POIXMLProperties.<init>(POIXMLProperties.java:78)
    at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:145)
    ... 2 more

仅当我尝试打开文件以将工作簿更改写入其中时(最后几行),才会出现问题。其余代码工作正常,没有任何问题。 有什么问题的主意吗? (我正在使用3.17版的Apache POI)

0 个答案:

没有答案