gpt4 book ai didi

java - XSSFWorkbook 问题 - 创建新文件时

转载 作者:太空宇宙 更新时间:2023-11-04 08:32:22 25 4
gpt4 key购买 nike

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

场景:我的 gui 菜单中有一个菜单项“新建文件”,它从流创建新的 xlsx 文件。第一次当我单击菜单项“新建文件”时,会出现文件新对话框,我给出新 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

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

为了解决这个问题,我把

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

jMenuItem1ActionPerformed内并解决了问题。

关于java - XSSFWorkbook 问题 - 创建新文件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7227380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com