2012-07-05 105 views
3

嘗試寫入.xlsx文件時,JVM崩潰。我正在使用POI(XSSF)。 在代碼中的錯誤位置點是寫method--> workBook.write(fileOutputStream);寫入XLSX文件(POI)時發生JVM崩潰

在控制檯我得到..

A fatal error has been detected by the Java Runtime Environment: 
    SIGBUS (0x7) at pc=0xb68d77f3, pid=14653, tid=1849355120 
    JRE version: 7.0_04-b20 
Java VM: Java HotSpot(TM) Server VM (23.0-b21 mixed mode linux-x86) 
Problematic frame: 
C [libzip.so+0x47f3] newEntry+0x73 
Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again 
If you would like to submit a bug report, please visit: 
    http://bugreport.sun.com/bugreport/crash.jsp 
The crash happened outside the Java Virtual Machine in native code. 
See problematic frame for where to report the bug. 
+0

好像在POI庫中的錯誤,如果錯誤是準確的(使核心轉儲將是有益的)。爲什麼不直接向項目提交錯誤報告?他們會更好地瞭解如何進行調試。 – Voo 2012-07-05 12:28:33

+0

您可以嘗試通過包管理器更新libzip。 – 2012-07-05 12:44:24

+1

嘗試Java 7更新5並確保您的驅動器上有足夠的可用空間。 – 2012-07-05 12:52:31

回答

7

的解決方案,我已經找到了這一點,我一直在尋找了一段時間,是使確定你不打開你的WorkbookFile你用來打開FileOutputStream保存Workbook。相反,請使用FileInputStream打開Workbook

像這樣的事情會工作完美

 File inputFile = new File("Your-Path"); 
     this.inputStream = new FileInputStream(inputFile); 
     this.opc = OPCPackage.open(this.inputStream); 
     this.workbook = WorkbookFactory.create(opc); 

... 

     this.outputStream = new FileOutputStream(inputFile); 
     this.workbook.write(this.outputStream); 

不要忘記關閉每個打開的流和OPCPackage

0

使用OPCPackage沒有解決JVM崩潰問題,但使用WorkbookFactory。如果你看看POI Busy Developers Guide,他們提供了讀寫同一個Excel文件的例子。

File excelFile = new File("workbook.xlsx"); 

InputStream inp = new FileInputStream(excelFile); 
Workbook wb = WorkbookFactory.create(inp); 

FileOutputStream fileOut = new FileOutputStream(excelFile); 
wb.write(fileOut); 
fileOut.close(); 

使用Apache POI 3.13版本,Java的1.8

+0

[如果你有一個文件,不要使用流!它使用更多的內存](http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream),如同您鏈接到的頁面所解釋的那樣... – Gagravarr 2016-02-25 19:10:16

+0

@Gagravarr謝謝您指出。如果我不使用'InputStream',則JVM崩潰。文檔只是讓你意識到它使用更多的內存,而不是它不應該使用。 – Mark 2016-02-26 15:37:42

1

無其他解決方案爲我工作。我只需要只讀訪問我的Excel文件,並設置只讀標誌爲我工作:

Workbook wb = new XSSFWorkbook(OPCPackage.open(file, PackageAccess.READ)); 
Workbook wb = new HSSFWorkbook(new POIFSFileSystem(file, true));