2014-07-17 145 views
3

我正在使用Apache_poi創建一個Excel文件。該文件將在我的Swing應用程序中的按鈕的動作事件上創建。 Excel表得到創建就好,但是,當我打開它,我得到這個錯誤:Apache poi創建錯誤的Excel文件

The file you are trying to open is in different format than specified by the file extension

當我打開文件它是完全空的。

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {           
    try { 
     // String personalclass=jComboBox1.getSelectedItem().toString(); 
     // String ac="academic"; 
     // FileOutputStream output=new FileOutputStream("D:\\"+personalclass+ac+".xls"); 
     FileOutputStream output=new FileOutputStream("D:\\workbook.xls"); 
     Workbook wb=new HSSFWorkbook(); 
     Sheet sheet =wb.createSheet("Class 1"); 
     Cell cell=sheet.createRow(0).createCell(3); 
     cell.setCellValue("Thisisatestofmerging"); 
     //sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2)); 
     output.close(); 
    } 
    catch(Exception e) 
    { 
    } 
} 

回答

5

你忘了打電話給writeWorkbook

關閉您的流之前添加此行:

 wb.write(output); 
+0

謝謝你一百萬次,這個.. @Jonathan –