diff --git a/excelformula/.gitignore b/excelformula/.gitignore new file mode 100644 index 0000000000..719e322c1d --- /dev/null +++ b/excelformula/.gitignore @@ -0,0 +1,34 @@ +HELP.md +mvnw +mvnw.cmd +.mvn +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/** +!**/src/test/** + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ + +### VS Code ### +.vscode/ diff --git a/excelformula/pom.xml b/excelformula/pom.xml new file mode 100644 index 0000000000..4ea65b5e57 --- /dev/null +++ b/excelformula/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.1.RELEASE + + + com.bealdung.poi + excelformula + 0.0.1-SNAPSHOT + excelformula + Demo project for Spring Boot + + 11 + 4.1.2 + 4.13 + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.apache.poi + poi-ooxml + ${poi-ooxml.version} + + + junit + junit + ${junit.version} + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/excelformula/src/main/java/com/bealdung/poi/excelformula/ExcelFormula.java b/excelformula/src/main/java/com/bealdung/poi/excelformula/ExcelFormula.java new file mode 100644 index 0000000000..1e2fa5acfe --- /dev/null +++ b/excelformula/src/main/java/com/bealdung/poi/excelformula/ExcelFormula.java @@ -0,0 +1,36 @@ +package com.bealdung.poi.excelformula; + +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 org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.List; + +@Component +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); + int lastCellNum = sheet.getRow(0).getLastCellNum(); + XSSFCell formulaCell = sheet.getRow(0).createCell(lastCellNum + 1); + formulaCell.setCellFormula(formula); + XSSFFormulaEvaluator formulaEvaluator = excel.getCreationHelper().createFormulaEvaluator(); + CellValue evaluate = formulaEvaluator.evaluate(formulaCell); + if(excel != null) + excel.close(); + return evaluate.getNumberValue(); + } +} diff --git a/excelformula/src/main/java/com/bealdung/poi/excelformula/ExcelformulaApplication.java b/excelformula/src/main/java/com/bealdung/poi/excelformula/ExcelformulaApplication.java new file mode 100644 index 0000000000..b857bd4266 --- /dev/null +++ b/excelformula/src/main/java/com/bealdung/poi/excelformula/ExcelformulaApplication.java @@ -0,0 +1,13 @@ +package com.bealdung.poi.excelformula; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ExcelformulaApplication { + + public static void main(String[] args) { + SpringApplication.run(ExcelformulaApplication.class, args); + } + +} diff --git a/excelformula/src/main/resources/application.properties b/excelformula/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/excelformula/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/excelformula/src/test/java/com/bealdung/poi/excelformula/ExcelformulaApplicationTests.java b/excelformula/src/test/java/com/bealdung/poi/excelformula/ExcelformulaApplicationTests.java new file mode 100644 index 0000000000..c2385911d0 --- /dev/null +++ b/excelformula/src/test/java/com/bealdung/poi/excelformula/ExcelformulaApplicationTests.java @@ -0,0 +1,38 @@ +package com.bealdung.poi.excelformula; + +import org.apache.poi.ss.util.CellReference; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.junit.Assert; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest +class ExcelformulaApplicationTests { + @Autowired + private ExcelFormula excelFormula; + + @Test + void givenExcelData_whenSetAndEvaluateFormula() throws IOException { + List data = Arrays.asList(2, 5, 10, 15, 7, 9); + XSSFSheet sheet = excelFormula.inserData(data); + int result = 0; + for (int row = 0; row <= sheet.getLastRowNum(); row++) { + result += sheet.getRow(row).getCell(0).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); + } + +}