2011-08-29 17 views
0

新しいxlsxファイルの作成中にXSSFWorkbook apisに問題があります。XSSFWorkbook問題 - 新しいファイルの作成中

シナリオ:ストリームから新しいxlsxファイルを作成する私のguiのメニューにメニュー項目 "New File"があります。初めて「新しいファイル」メニュー項目をクリックすると、新しいダイアログボックスが表示され、新しいxlsxファイルの名前を付けて新しいファイルを作成します。しかし、このメニュー項目 "New File"を2回目にクリックすると、新しい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ファイル用)でうまくいきました。

多くのおかげで、

ラーフル

+0

デバッガで実行しようとしましたが、ロックされている場所を確認できましたか? – Gagravarr

答えて

0

が問題は、デバッガを使用して同定された:ストリームは、一度しか読むことができ、読むための第2の試みはになります:

java.io.IOException: Stream closed 

が問題だったようですこのIOException

ので、この問題を解決するために、私はjMenuItem1ActionPerformed内部

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

を入れて、問題を修正。

関連する問題