本文介绍了使用apache poi 3.7写入xlsx文档时有异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在尝试使用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);
}
}
推荐答案
很可能是一个错误。
我建议您订阅该机票以获得通知关于当前的改进/替代方案。
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文档时有异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!