首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSSFWorkbook问题-创建新文件时

XSSFWorkbook问题-创建新文件时
EN

Stack Overflow用户
提问于 2011-08-29 15:38:00
回答 1查看 1.3K关注 0票数 1

在创建新的XSSFWorkbook文件时,我遇到了xlsx apis的问题。

场景:我在gui的菜单中有一个菜单项"New File“,它可以从流中创建新的xlsx文件。当我第一次点击菜单项“新建文件”时,文件新建对话框出现,我给出了新的xlsx文件的名称,然后新文件就被创建了。但是当我再次点击这个菜单项"New File“时,就不会创建新的xlsx。

代码语言:javascript
复制
//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.");
    }

}

第二次,我猜它在代码语句中阻塞了:

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

调试日志:

代码语言:javascript
复制
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文件)就可以了。

非常感谢,

拉胡尔

EN

回答 1

Stack Overflow用户

发布于 2011-08-31 00:13:08

问题是使用调试器发现的:

代码语言:javascript
复制
java.io.IOException: Stream closed

似乎问题是:流只能读取一次,第二次尝试读取将导致此IOException。

所以为了解决这个问题,我把

代码语言:javascript
复制
newPtrIrStream = this.getClass()
  .getResourceAsStream("/org/ama/defect/prevention/templates/MainTemplate.xlsx");

jMenuItem1ActionPerformed内部并修复了问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7227380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档