2011-08-29 58 views
0

我在創建新的xlsx文件時遇到了XSSFWorkbook apis問題。XSSFWorkbook問題 - 創建新文件

場景:我的gui菜單中有一個菜單項「New File」,它從流中創建新的xlsx文件。第一次當我點擊菜單項「New File」時,出現新的對話框,我給出新的xlsx文件的名稱,並創建新的文件。但是當我第二次點擊這個菜單項「新文件」時,新的xlsx不會被創建。

//Code snippet 

File newOpenXLSFile; 
public XSSFWorkbook newPtrIrWorkBook; 

newPtrIrStream = this.getClass().getResourceAsStream ("/org/ama/defect/prevention/templates/MainTemplate.xlsx"); 


private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
    logger.debug("You choose to create new PTR/IR file"); 
    int returnVal = jFileChooser4.showDialog(this, "New PTR/IR Data File"); 

    if (returnVal == JFileChooser.APPROVE_OPTION) { 

     newOpenXLSFile = jFileChooser4.getSelectedFile(); 
     logger.debug("file path " + newOpenXLSFile); 
     try { 
      logger.debug("For second time, I am stopped here:"); 
      //newPtrIrWorkBook = new HSSFWorkbook(newPtrIrPFS, true); //copying extract into Excel file  
      newPtrIrWorkBook = new XSSFWorkbook(newPtrIrStream); 
      logger.debug("New File..." + newOpenXLSFile.getPath()); 
      FileOutputStream out = new FileOutputStream(newOpenXLSFile); 
      newPtrIrWorkBook.write(out); 
      out.close(); 
     } catch (Exception e) { 
      e.getMessage(); 
     } 
    } else { 
     logger.debug("New file dialogue cancelled by user."); 
    } 

} 

對於第二次,我想它會阻止這裏的代碼語句:

logger.debug("For second time, I am stopped here:"); 
//newPtrIrWorkBook = new HSSFWorkbook(newPtrIrPFS, true);//copying extract into 
//Excel file  
---> newPtrIrWorkBook = new XSSFWorkbook(newPtrIrStream); <--- 

調試日誌:

2011-08-18 13:04:37,602 [AWT-EventQueue-0] DEBUG org.ama.defect.prevention.tool.gui.GUI.class - You choose to create new PTR/IR file 
2011-08-18 13:04:45,586 [AWT-EventQueue-0] DEBUG org.ama.defect.prevention.tool.gui.GUI.class - file path C:\Documents and Settings\rmehta\Desktop\Try\FirstFile.xlsx 
2011-08-18 13:04:45,586 [AWT-EventQueue-0] DEBUG org.ama.defect.prevention.tool.gui.GUI.class - For second time, I am stopped here: 
2011-08-18 13:04:46,351 [AWT-EventQueue-0] DEBUG org.ama.defect.prevention.tool.gui.GUI.class - New File...C:\Documents and Settings\rmehta\Desktop\Try\FirstFile.xlsx 

2011-08-18 13:04:52,898 [AWT-EventQueue-0] DEBUG org.ama.defect.prevention.tool.gui.GUI.class - You choose to create new PTR/IR file 
2011-08-18 13:04:57,116 [AWT-EventQueue-0] DEBUG org.ama.defect.prevention.tool.gui.GUI.class - file path C:\Documents and Settings\rmehta\Desktop\Try\SecondFile.xlsx 
2011-08-18 13:04:57,116 [AWT-EventQueue-0] DEBUG org.ama.defect.prevention.tool.gui.GUI.class - For second time, I am stopped here: 

能否請你幫我解決這個問題?但是,HSSFWorkbook(對於xls文件)沒有問題。

java.io.IOException: Stream closed 

看來問題是::

非常感謝,

拉胡爾

+0

你有沒有嘗試在調試器中運行它,所以你可以檢查確定事件鎖定的位置? – Gagravarr

回答

0

的問題是使用調試器識別的流只能讀一次,讀第二次嘗試將導致這個IOException。

所以要解決這個問題,我把

newPtrIrStream = this.getClass() 
    .getResourceAsStream("/org/ama/defect/prevention/templates/MainTemplate.xlsx"); 

jMenuItem1ActionPerformed和問題修復。