add excel formula
This commit is contained in:
parent
33601cc191
commit
67e9dc8187
|
@ -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/
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.3.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.bealdung.poi</groupId>
|
||||
<artifactId>excelformula</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>excelformula</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<poi-ooxml.version>4.1.2</poi-ooxml.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>${poi-ooxml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -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<Integer> 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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -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<Integer> 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue