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 new file mode 100644 index 0000000000..f5179b19c9 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java @@ -0,0 +1,26 @@ +package com.baeldung.poi.excel.setformula; + +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; + +public class ExcelFormula { + 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); + formulaCell.setCellFormula(formula); + 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/com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx b/apache-poi/src/main/resources/com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx new file mode 100644 index 0000000000..a0fe73f0eb Binary files /dev/null and b/apache-poi/src/main/resources/com/baeldung/poi/excel/setformula/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 new file mode 100644 index 0000000000..fa5baa37fa --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java @@ -0,0 +1,51 @@ +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.net.URISyntaxException; +import java.nio.file.Paths; + +class ExcelFormulaUnitTest { + private static String FILE_NAME = "com/baeldung/poi/excel/setformula/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_whenSetFormula_thenSuccess() throws IOException { + 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++) { + resultColumnA += sheet.getRow(row).getCell(0).getNumericCellValue(); + resultColumnB += sheet.getRow(row).getCell(1).getNumericCellValue(); + } + 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(resultColumnA - resultColumnB, resultValue, 0d); + } +} diff --git a/excelformula/README.md b/excelformula/README.md new file mode 100644 index 0000000000..86ddaba413 --- /dev/null +++ b/excelformula/README.md @@ -0,0 +1,8 @@ +## Apache POI + +This module contains articles about Apache POI + +### Relevant Articles: +- [Working with Microsoft Excel in Java](https://www.baeldung.com/java-microsoft-excel) +- [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula) +- [Upload and Display Excel Files with Spring MVC](https://www.baeldung.com/spring-mvc-excel-files)