diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/setFormula/ExcelFormula.java b/apache-poi/src/main/java/com/baeldung/poi/excel/setFormula/ExcelFormula.java index 7bd8d8a035..8b1ca5a89a 100644 --- a/apache-poi/src/main/java/com/baeldung/poi/excel/setFormula/ExcelFormula.java +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/setFormula/ExcelFormula.java @@ -1,34 +1,26 @@ package com.baeldung.poi.excel.setFormula; -import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; -import java.util.List; public class ExcelFormula { - XSSFWorkbook excel; - public XSSFSheet inserData(List dataList) { - excel = new XSSFWorkbook(); - XSSFSheet sheet = excel.createSheet(); - int rowNum =0 ; - for (Integer data : dataList){ - sheet.createRow(rowNum++).createCell(0).setCellValue(data); - } - return sheet; - } - public double setFormula(String formula) throws IOException { - XSSFSheet sheet = excel.getSheetAt(0); + public double setFormula(String fileLocation, XSSFWorkbook wb, String formula) throws IOException { + XSSFSheet sheet = wb.getSheetAt(0); int lastCellNum = sheet.getRow(0).getLastCellNum(); - XSSFCell formulaCell = sheet.getRow(0).createCell(lastCellNum + 1); + XSSFCell formulaCell = sheet.getRow(0).createCell(lastCellNum); formulaCell.setCellFormula(formula); - XSSFFormulaEvaluator formulaEvaluator = excel.getCreationHelper().createFormulaEvaluator(); - CellValue evaluate = formulaEvaluator.evaluate(formulaCell); - if(excel != null) - excel.close(); - return evaluate.getNumberValue(); + XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); + formulaEvaluator.evaluateFormulaCell(formulaCell); + FileOutputStream fileOut = new FileOutputStream(new File(fileLocation)); + wb.write(fileOut); + wb.close(); + fileOut.close(); + return formulaCell.getNumericCellValue(); } } diff --git a/apache-poi/src/main/resources/SetFormulaTest.xlsx b/apache-poi/src/main/resources/SetFormulaTest.xlsx new file mode 100644 index 0000000000..a0fe73f0eb Binary files /dev/null and b/apache-poi/src/main/resources/SetFormulaTest.xlsx differ diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/setFormula/ExcelFormulaUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/setFormula/ExcelFormulaUnitTest.java index 8daf311149..990b93fcbb 100644 --- a/apache-poi/src/test/java/com/baeldung/poi/excel/setFormula/ExcelFormulaUnitTest.java +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/setFormula/ExcelFormulaUnitTest.java @@ -2,28 +2,48 @@ package com.baeldung.poi.excel.setFormula; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; -import java.util.Arrays; -import java.util.List; +import java.net.URISyntaxException; +import java.nio.file.Paths; class ExcelFormulaUnitTest { + private static String FILE_NAME = "SetFormulaTest.xlsx"; + private String fileLocation; + private ExcelFormula excelFormula; + + @BeforeEach + public void setup() throws URISyntaxException { + fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString(); + excelFormula = new ExcelFormula(); + } + @Test void givenExcelData_whenSetAndEvaluateFormula() throws IOException { - ExcelFormula excelFormula = new ExcelFormula(); - List data = Arrays.asList(2, 5, 10, 15, 7, 9); - XSSFSheet sheet = excelFormula.inserData(data); - int result = 0; + FileInputStream inputStream = new FileInputStream(new File(fileLocation)); + XSSFWorkbook wb = new XSSFWorkbook(inputStream); + XSSFSheet sheet = wb.getSheetAt(0); + double resultColumnA = 0; + double resultColumnB = 0; for (int row = 0; row <= sheet.getLastRowNum(); row++) { - result += sheet.getRow(row).getCell(0).getNumericCellValue(); + resultColumnA += sheet.getRow(row).getCell(0).getNumericCellValue(); + resultColumnB += sheet.getRow(row).getCell(1).getNumericCellValue(); } - String colName = CellReference.convertNumToColString(0); - String startCell = colName + 1; - String stopCell = colName + (sheet.getLastRowNum() + 1); - String sumFormula = String.format("SUM(%s:%s)", startCell, stopCell); - int resultValue = (int) excelFormula.setFormula(sumFormula); - Assert.assertEquals("The results are the same!", resultValue , result); + String colNameA = CellReference.convertNumToColString(0); + String colNameB = CellReference.convertNumToColString(1); + String startCellA = colNameA + 1; + String stopCellA = colNameA + (sheet.getLastRowNum() + 1); + String sumFormulaForColumnA = String.format("SUM(%s:%s)", startCellA, stopCellA); + String startCellB = colNameB + 1; + String stopCellB = colNameB + (sheet.getLastRowNum() + 1); + String sumFormulaForColumnB = String.format("SUM(%s:%s)", startCellB, stopCellB); + double resultValue = excelFormula.setFormula(fileLocation, wb, sumFormulaForColumnA + "-" + sumFormulaForColumnB); + Assert.assertEquals(resultValue, resultColumnA - resultColumnB, 0d); } }