2011-08-29 56 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和问题修复。