且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

写入XLSX文档时的异常使用Apache POI 3.7数次

更新时间:2022-10-14 21:55:29

这是最有可能的一个bug。

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.