开发者

XSSFWorkbook Problem- While creating new files

I have a problem with XSSFWorkbook apis while creating new xlsx files.

Scenario: I have a menu item "New File" in my gui's menu which creates new xlsx file from stream. First time when I click the menu item "New File" then file new dialogue box appears and I give the name of new xlsx file and new file gets created. But when I click on this menu item "New File" second time then new xlsx does not get created.

//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 co开发者_开发问答de 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.");
    }

}

For second time, I guess it blocks here on the code statement:

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

Debug logs:

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:

Can you please help me solving this problem? However, it was fine with HSSFWorkbook (for xls file).

Many Thanks,

rahul


The problem was identified using debugger:

java.io.IOException: Stream closed

Seems the problem was: stream can only be read once, a second attempt to read will result in this IOException.

so to fix this problem, I put

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

inside jMenuItem1ActionPerformed and problem fixed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜