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
| XSSFWorkbook workbook = new XSSFWorkbook(getFile("test.xlsx"));
String sheetName = "Product Mix"; XSSFSheet sheet = workbook.getSheet(sheetName); assertThat(sheet.getSheetName(), is(sheetName));
XSSFCell tvsetNumber = getCell(sheet, "D4"); assertThat(tvsetNumber.getNumericCellValue(), closeTo(100, 0.1));
XSSFCell total = getCell(sheet, "D13"); assertThat(total.getNumericCellValue(), closeTo(16000, 0.1));
tvsetNumber.setCellValue(200); assertThat(tvsetNumber.getNumericCellValue(), closeTo(200, 0.1));
XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook); assertThat(total.getNumericCellValue(), closeTo(23500, 0.1));
workbook.write(new FileOutputStream(getFile("update.xlsx")));
XSSFWorkbook updateWorkBook = new XSSFWorkbook(getFile("update.xlsx")); assertThat(getCell(updateWorkBook.getSheet(sheetName), "D4").getNumericCellValue(), closeTo(200, 0.1)); assertThat(getCell(updateWorkBook.getSheet(sheetName), "D13").getNumericCellValue(), closeTo(23500, 0.1));
XSSFName test_name = workbook.getName("test_cell_name"); AreaReference areaReference = new AreaReference(test_name.getRefersToFormula(), SpreadsheetVersion.EXCEL2007);
CellReference firstCell = areaReference.getFirstCell(); XSSFSheet sheet = workbook.getSheet(firstCell.getSheetName()); XSSFRow row = sheet.getRow(firstCell.getRow()); XSSFCell cell = row.getCell(firstCell.getCol());
private XSSFCell getCell(XSSFSheet sheet, String cellCoordinate) { CellReference d4 = new CellReference(cellCoordinate); XSSFRow row = sheet.getRow(d4.getRow()); return row.getCell(d4.getCol()); }
|