Mih*_*ail 19 java excel apache-poi
尝试.xlsx
使用Apache POI 编写文件时,我收到以下异常:org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
看来问题是第二次使用write()方法.使用HSSFWorkbook时,不会出现此问题.
这是代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Sco*_*fen 11
我今天遇到了同样的问题.我注意到很多人在很多不同的论坛上都提出同样的问题,但我没有在任何地方看到答案.所以,这就是我想出的.它远非理想(我可以想到至少有两种情况,这可能是一个坏主意),并且可能无法满足所有需求,但它有效!
在工作簿对象所属的类中的每个保存操作之后,我从刚保存到的文件中重新加载工作簿.
使用上面的代码示例,我会像这样修改方法:
void method(int i) throws InvalidFormatException, IOException {
...
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
workbook.write(fileOut);
fileOut.close();
// Reload the workbook, workaround for bug 49940
// https://issues.apache.org/bugzilla/show_bug.cgi?id=49940
workbook = new XSSFWorkbook(new FileInputStream("workbook.xlsx"));
}
Run Code Online (Sandbox Code Playgroud)
我在我的代码中对此进行了测试,并且很好地解决了这个问题.只需确保从保存它的同一文件中读回来,而不是早期版本或不同版本.
Ern*_*ert 10
这很可能是一个错误.
https://issues.apache.org/bugzilla/show_bug.cgi?id=49940
我建议您订阅该机票以获得有关当前改进/替代方案的通知.
如果我找到解决方法,我会告诉你.
归档时间: |
|
查看次数: |
25044 次 |
最近记录: |