java - Java Apache POI - 编辑和保存后需要修复工作簿
问题描述
我的大部分 Java 程序都处理 Excel 和 Apache POI。但是,一种方法似乎会损坏文件,迫使用户在打开之前对其进行修复。尝试打开时的 Excel 消息是:“我们发现 [工作簿] 中的某些内容存在问题。您希望我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击是。”
事实上,我用另一种方法编辑了这个确切的工作簿,没有任何问题。据我所知,在这段看似错误的代码中,我没有做任何我以前没有做过的事情。
public static void refreshExcel() {
try {
workbook = new XSSFWorkbook(new BufferedInputStream(new FileInputStream(file)));
sheet = workbook.getSheet("DISTRIBUTION");
} catch (Exception ex) {
MasterLog.appendError(ex);
}//try-catch
sheet.disableLocking();
//you can ignore the next 3 lines, they essentially find the rows that need to be updated
ArrayList<String> list = Log.modelLackingSCs();
for (String sc : list) {
int desiredIndex = findFirstIndex(sc);
XSSFRow row = sheet.getRow(desiredIndex);
try {
while (row.getCell(10).getNumericCellValue() == Integer.parseInt(sc)) {
row.createCell(8).setCellValue(OrderLog.findShipDate(OrderLog.getWB(sc)));
row = sheet.getRow(++desiredIndex);
}//while
} catch (IllegalStateException ise) {
MasterLog.appendEntry("ise");
} catch (Exception ex) {
MasterLog.appendError(ex);
}//try-catch
}//for
sheet.enableLocking();
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
workbook.write(bos);
} catch (Exception ex) {
MasterLog.appendError(ex);
}//try-catch
}//refreshExcel()
解决方案
推荐阅读
- c# - JSON 反序列化器返回 NULL 值
- python - 当试验多于对象时,我的随机播放算法崩溃
- swift - 带有“b”的字典:“\u0001”,无法转换
- javascript - Vuejs 路由器链接动态变化
- roku - ROKU:从屏幕打开另一个场景/屏幕
- liquibase - Liquibase:直接修改数据库后如何创建更新更改日志?
- c++ - 将 stl::vector 迭代器入口传递给函数
- mysql - Node.js - Express & mysql TypeError: res.json is not a function 尽管插入成功
- python - 带有浮点输入的 feed dict TensorFlow 中的错误
- kubernetes - kubeadm 创建集群的 kube-controller-manager 和 kube-apiserver 问题