2015-01-15 87 views
1

我想写读取和使用下面的代码写入到工作簿:的Apache POI文件中获取损坏,无法写入现有工作簿

public static void main(String args[]) { 
    String absoluteFilePath = System.getProperty("user.dir") + File.separator + "abc.xlsx"; 
    System.out.println("Readin file : " + absoluteFilePath); 

    Workbook workbook = null; 

    try { 
     workbook = WorkbookFactory.create(new File(absoluteFilePath)); 

     //reading and writing on sheets of workbook 


     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      System.out.println("Writing to workbook and Closing the file"); 
      FileOutputStream fileOutputStream = new FileOutputStream(
        new File(absoluteFilePath)); 
      workbook.write(fileOutputStream); 
      fileOutputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

当我运行的第一次的代码,我在workbook.write(fileOutputStream);

Exception in thread "main" org.apache.poi.POIXMLException: java.io.IOException: Can't obtain the input stream from /docProps/app.xml 
    at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:148) 
    at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:199) 
    at NewNewDriver.main(NewNewDriver.java:129) 
Caused by: java.io.IOException: Can't obtain the input stream from /docProps/app.xml 
    at org.apache.poi.openxml4j.opc.PackagePart.getInputStream(PackagePart.java:500) 
    at org.apache.poi.POIXMLProperties.<init>(POIXMLProperties.java:75) 
    at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:146) 
    ... 2 more 

得到这个例外,在此之后,该工作簿被破坏,我减少到0KB和我得到WorkbookFactory.create()此异常:

org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document 
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:167) 
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:117) 
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:225) 
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:164) 
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:145) 
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:105) 
    at NewNewDriver.main(NewNewDriver.java:27) 
Closing the file 
Exception in thread "main" java.lang.NullPointerException 
    at NewNewDriver.main(NewNewDriver.java:129) 

我应该在哪里以及如何使用FileOutputStream,workbook.write(),并且我也应该使用FileInputStream,即使我正在使用WorkbookFactory?

------------编辑----------------------我得到我的代码工作 我用FileInputStream而不是WorkbookFactory创建工作簿并在关闭FileOutputStream后关闭它。这工作。

+0

这是什么版本的Apache POI?如果不是3.11最终版(最新版),升级时会发生什么? – Gagravarr

+0

该版本是最新版本 - 3.11 – tanvi

回答

3

我想我已经发现了你的问题:

workbook = WorkbookFactory.create(new File(absoluteFilePath)); 

.... 

FileOutputStream fileOutputStream = new FileOutputStream(
       new File(absoluteFilePath)); 
workbook.write(fileOutputStream); 

你打开一个文件,然后试图后来覆盖它,同时它还是开放的,在使用中,这是不支持

你需要做的是将更新后的文件写入不同的文件名。如果要替换它,则在关闭原始文件后需要执行第二步。

现在,您已经开始覆盖文件,然后POI尝试将原始文件的某些部分复制到新文件中,并失败,因为它试图读取的原始文件已被删除! (NPOIFSFileSystem和OPCPackage,底层包代码,既支持就地写入和就地更新,但更高级的代码,如HSSFWorkbook/XSSFWorkbook/XWPFDocument只支持写入新文件,并且还没有足够的社区关注在就地编写工作以增加支持)

+0

好的。但是,为什么FileInputStream不会出现类似的问题? – tanvi

+0

因为这样,你可以在内存中缓冲整个文件,关闭底层文件,然后允许你在不再使用原始文件的情况下垃圾回收。 – Gagravarr

+0

明白了。非常感谢 :) – tanvi

0

在哪里,我应该如何使用FileOutputStream中,workbook.write()

不是在finally块。如果您有任何异常情况,您肯定不希望将可能的垃圾写入文件。 workbook甚至可以在finally块中为null

将该代码移动到try块的末尾。

+0

实际上这是一个包含测试案例的文件。如果发生一些未处理的异常,我如何获得“失败”打印? – tanvi

+0

抓住例外并打印“失败”,或拨打任何你必须打电话打印它? – EJP

+0

但是失败的发生是因为我无法打印“失败”到Excel。 – tanvi

相关问题