写入XLSX文档时的异常使用Apache POI 3.7数次 [英] Exception when writing to the xlsx document several times using apache poi 3.7
问题描述
我收到以下异常试图写一个的.xlsx
使用Apache POI文件: org.apache.xmlbeans.impl.values。 XmlValueDisconnectedException
看来问题是使用方法写()第二次。
当这个问题的一个HSSFWorkbook工作不会产生
这里的code:
公共类SomeClass的{XSSFWorkbook工作簿;公共SomeClass的()抛出IOException
文件excelFile =新的文件(workbook.xlsx); InputStream的INP =新的FileInputStream(excelFile);
工作簿=新XSSFWorkbook(INP);
inp.close();
}无效的方法(int i)以抛出InvalidFormatException,IOException异常{ XSSFSheet片= workbook.getSheetAt(0);
XSSFRow行= sheet.getRow(I)
如果(行== NULL){
行= sheet.createRow(I)
}
XSSFCell细胞= row.getCell(ⅰ);
如果(细胞== NULL)
细胞= row.createCell(ⅰ);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(测试); //写输出到文件
FileOutputStream中FILEOUT =新的FileOutputStream(workbook.xlsx);
workbook.write(FILEOUT);
fileOut.close();}公共静态无效的主要(字串[] args)抛出异常{
SomeClass的SC =新SomeClass的(); sc.method(1);
sc.method(2);
}
}
这是最有可能的一个bug。
<一个href=\"https://issues.apache.org/bugzilla/show_bug.cgi?id=49940\">https://issues.apache.org/bugzilla/show_bug.cgi?id=49940
我建议您订阅的门票收到通知当前的改进/替代方案。
如果我找到一个解决办法,我会让你知道。
I am getting the following exception while trying to write an .xlsx
file using Apache POI: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
It seems the problem is using the method write () second time. When working with a HSSFWorkbook of this problem does not arise.
Here's the Code:
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);
}
}
This is most likely a bug.
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.
这篇关于写入XLSX文档时的异常使用Apache POI 3.7数次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!