使用 apache poi 3.7 多次写入 xlsx 文档时出现异常 [英] Exception when writing to the xlsx document several times using apache poi 3.7
问题描述
我在尝试使用 Apache POI 编写 .xlsx
文件时遇到以下异常:org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
I am getting the following exception while trying to write an .xlsx
file using Apache POI: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
问题似乎是第二次使用方法write().使用 HSSFWorkbook 时不会出现此问题.
It seems the problem is using the method write () second time. When working with a HSSFWorkbook of this problem does not arise.
代码如下:
public class SomeClass{
XSSFWorkbook workbook;
public SomeClass() throws IOException{
File excelFile = new File("workbook.xlsx");
InputStream inp = new FileInputStream(excelFile);
workbook = new XSSFWorkbook(inp);
inp.close();
}
void method(int i) throws InvalidFormatException, IOException {
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(i);
if (row == null) {
row = sheet.createRow(i);
}
XSSFCell cell = row.getCell(i);
if (cell == null)
cell = row.createCell(i);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
workbook.write(fileOut);
fileOut.close();
}
public static void main(String[] args) throws Exception {
SomeClass sc = new SomeClass();
sc.method(1);
sc.method(2);
}
}
推荐答案
这很可能是一个错误.
https://issues.apache.org/bugzilla/show_bug.cgi?id=49940
我建议您订阅该票以获取有关当前改进/替代方案的通知.
I suggest you subscribe to that ticket to get notified about current improvements / alternatives.
如果我找到解决方法,我会通知您.
If I find a workaround I will let you know.
这篇关于使用 apache poi 3.7 多次写入 xlsx 文档时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!