Нужно обновить данные в файле со многими сложными формулами.
Использовать функции эвалюации не представляется возможным, так-как в файле используются формулы, не поддеживаемые Apache POI
Как удалить из файла (после его обновления) кешированные результаты вычислений по формулам, перед сохранением нового файла?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
public class POITestRB {
static String excelFileOrig = "C:/Test/1.xlsm";
static String excelFileNew = "C:/Test/excelFileNew.xlsm";
static FileInputStream fis;
static XSSFWorkbook workbook;
public static void main(String[] args) throws IOException {
fis = new FileInputStream(excelFileOrig);
workbook = new XSSFWorkbook(fis);
gettingCellContents(workbook, "D5");
updateCell(workbook, 10.0);
fis.close();
workbook.close();
fis = new FileInputStream(excelFileNew);
workbook = new XSSFWorkbook(fis);
gettingCellContents(workbook, "D5");
fis.close();
workbook.close();
}
public static void updateCell(XSSFWorkbook workbook, Double newData) {
try {
XSSFSheet sheet = workbook.getSheetAt(1);
CellReference ref = new CellReference("C8");
int row = ref.getRow();
int col = ref.getCol();
Cell cell = sheet.getRow(row).getCell(col);
if (cell != null) {
cell.setCellValue(newData);
}
workbook.getCreationHelper().createFormulaEvaluator().clearAllCachedResultValues();
// workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
OutputStream os = new FileOutputStream(excelFileNew);
workbook.write(os);
os.flush();
os.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
private static void gettingCellContents(XSSFWorkbook workbook, String cellId) {
workbook.setForceFormulaRecalculation(true);
XSSFSheet sheet = workbook.getSheetAt(1);
CellReference ref = new CellReference(cellId);
int row = ref.getRow();
int col = ref.getCol();
Cell cell = sheet.getRow(row).getCell(col);
switch (cell.getCellTypeEnum()) {
case STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case FORMULA:
System.out.println("Formula is: " + cell.getCellFormula());
System.out.println("cell.getCachedFormulaResultType(): " + cell.getCachedFormulaResultType());
switch(cell.getCachedFormulaResultType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.println("0. case Cell.CELL_TYPE_NUMERIC --> Last evaluated as: " + cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println("4. case Cell.CELL_TYPE_STRING --> Last evaluated as \"" + cell.getRichStringCellValue() + "\"");
break;
case Cell.CELL_TYPE_ERROR:
System.out.println("5. case Cell.CELL_TYPE_ERROR --> ");
break;
}
break;
case BLANK:
System.out.println();
break;
default:
System.out.println("default");
}
}
}