Я использую apache-poi для изменения ячейки Excel (с помощью setCellValue) и получения значения cellM14, значение которого зависит от измененной ячейки.
Когда я вручную открываю Excel, ячейка M14 обновляется правильно, но когда я пытаюсь распечатайте
System.out.println("Updated Value at M14: " + cellM14.getNumericCellValue());
печатается старое значение ячейкиM14. Я не могу использовать Formula Evaluator, так как существует ошибка с именованием, см.:
FormulaEvaluatorFormulaParseException Выражение точечного диапазона (полная строка или столбец) «C.aRG» должно содержать ровно 2 точки.
Может ли кто-нибудь помочь мне распечатать обновленную версию cellM14?
String filePath = "file";
try (FileInputStream fileInputStream = new FileInputStream(new File(filePath));
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream)) {
Sheet sheet = workbook.getSheet("input");
workbook.setForceFormulaRecalculation(true);
// Check if Row 13 exists and print original value if the cell exists
Row row13 = sheet.getRow(13);
if (row13 != null) {
Cell cellM14 = row13.getCell(11);
if (cellM14 != null) {
System.out.println("Original Value at M14: " + cellM14.getNumericCellValue());
} else {
System.out.println("Cell M14 is null.");
}
} else {
System.out.println("Row 13 is null.");
}
// Change the value in cell H5 (row index 4, column index 7)
Row row4 = sheet.getRow(4);
if (row4 == null) {
row4 = sheet.createRow(4);
}
Cell sub = row4.getCell(7);
if (sub == null) {
sub = row4.createCell(7);
}
sub.setCellValue("nanoparticle");
// Save workbook
try (FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath))) {
workbook.write(fileOutputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
try (FileInputStream fileInputStream = new FileInputStream(new File(filePath));
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream)) {
workbook.setForceFormulaRecalculation(true);
try (FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath))) {
workbook.write(fileOutputStream);
}
Sheet sheet = workbook.getSheet("input");
// Check if Row 13 exists and print original value if the cell exists
Row row13 = sheet.getRow(13);
if (row13 != null) {
Cell cellM14 = row13.getCell(11);
if (cellM14 != null) {
System.out.println("Updated Value at M14: " + cellM14.getNumericCellValue());
} else {
System.out.println("Cell M14 is null.");
}
} else {
System.out.println("Row 13 is null.");
}
} catch (Exception e) {
e.printStackTrace();
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... -correctly