Skip to content

Commit ebb67f5

Browse files
committed
[JAVA-10463] Split apache-poi module
1 parent 70428da commit ebb67f5

8 files changed

Lines changed: 33 additions & 273 deletions

File tree

apache-poi-2/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ This module contains articles about Apache POI.
1010
- [Microsoft Word Processing in Java with Apache POI](https://www.baeldung.com/java-microsoft-word-with-apache-poi)
1111
- [Creating a MS PowerPoint Presentation in Java](https://www.baeldung.com/apache-poi-slideshow)
1212
- [Finding the Last Row in an Excel Spreadsheet From Java](https://www.baeldung.com/java-excel-find-last-row)
13+
- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas)
1314
- More articles: [[<-- prev]](../apache-poi)

apache-poi-2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</dependencies>
2323

2424
<properties>
25-
<poi.version>5.0.0</poi.version>
25+
<poi.version>5.2.0</poi.version>
2626
</properties>
2727

2828
</project>

apache-poi/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java renamed to apache-poi-2/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
package com.baeldung.poi.excel.setformula;
2-
3-
import org.apache.poi.xssf.usermodel.XSSFCell;
4-
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
5-
import org.apache.poi.xssf.usermodel.XSSFSheet;
6-
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
7-
8-
import java.io.File;
9-
import java.io.FileOutputStream;
10-
import java.io.IOException;
11-
12-
public class ExcelFormula {
13-
public double setFormula(String fileLocation, XSSFWorkbook wb, String formula) throws IOException {
14-
XSSFSheet sheet = wb.getSheetAt(0);
15-
int lastCellNum = sheet.getRow(0).getLastCellNum();
16-
XSSFCell formulaCell = sheet.getRow(0).createCell(lastCellNum);
17-
formulaCell.setCellFormula(formula);
18-
XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
19-
formulaEvaluator.evaluateFormulaCell(formulaCell);
20-
FileOutputStream fileOut = new FileOutputStream(new File(fileLocation));
21-
wb.write(fileOut);
22-
wb.close();
23-
fileOut.close();
24-
return formulaCell.getNumericCellValue();
25-
}
26-
}
1+
package com.baeldung.poi.excel.setformula;
2+
3+
import org.apache.poi.xssf.usermodel.XSSFCell;
4+
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
5+
import org.apache.poi.xssf.usermodel.XSSFSheet;
6+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
7+
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
11+
public class ExcelFormula {
12+
public double setFormula(String fileLocation, XSSFWorkbook wb, String formula) throws IOException {
13+
XSSFSheet sheet = wb.getSheetAt(0);
14+
int lastCellNum = sheet.getRow(0).getLastCellNum();
15+
XSSFCell formulaCell = sheet.getRow(0).createCell(lastCellNum);
16+
formulaCell.setCellFormula(formula);
17+
XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
18+
formulaEvaluator.evaluateFormulaCell(formulaCell);
19+
FileOutputStream fileOut = new FileOutputStream(fileLocation);
20+
wb.write(fileOut);
21+
wb.close();
22+
fileOut.close();
23+
return formulaCell.getNumericCellValue();
24+
}
25+
}

apache-poi/src/main/resources/com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx renamed to apache-poi-2/src/main/resources/com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx

File renamed without changes.

apache-poi/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java renamed to apache-poi-2/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
import org.apache.poi.ss.util.CellReference;
44
import org.apache.poi.xssf.usermodel.XSSFSheet;
55
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
6-
import org.junit.Assert;
76
import org.junit.jupiter.api.BeforeEach;
87
import org.junit.jupiter.api.Test;
98

10-
import java.io.File;
119
import java.io.FileInputStream;
1210
import java.io.IOException;
1311
import java.net.URISyntaxException;
1412
import java.nio.file.Paths;
1513

14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
1616
class ExcelFormulaUnitTest {
17-
private static String FILE_NAME = "com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx";
17+
private static final String FILE_NAME = "com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx";
18+
1819
private String fileLocation;
1920
private ExcelFormula excelFormula;
2021

@@ -26,7 +27,7 @@ public void setup() throws URISyntaxException {
2627

2728
@Test
2829
void givenExcelData_whenSetFormula_thenSuccess() throws IOException {
29-
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
30+
FileInputStream inputStream = new FileInputStream(fileLocation);
3031
XSSFWorkbook wb = new XSSFWorkbook(inputStream);
3132
XSSFSheet sheet = wb.getSheetAt(0);
3233
double resultColumnA = 0;
@@ -46,6 +47,6 @@ void givenExcelData_whenSetFormula_thenSuccess() throws IOException {
4647

4748
double resultValue = excelFormula.setFormula(fileLocation, wb, sumFormulaForColumnA + "-" + sumFormulaForColumnB);
4849

49-
Assert.assertEquals(resultColumnA - resultColumnB, resultValue, 0d);
50+
assertEquals(resultColumnA - resultColumnB, resultValue, 0d);
5051
}
5152
}

apache-poi/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ This module contains articles about Apache POI.
88
- [Merge Cells in Excel Using Apache POI](https://www.baeldung.com/java-apache-poi-merge-cells)
99
- [Get String Value of Excel Cell with Apache POI](https://www.baeldung.com/java-apache-poi-cell-string-value)
1010
- [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula)
11-
- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas)
1211
- [Insert a Row in Excel Using Apache POI](https://www.baeldung.com/apache-poi-insert-excel-row)
1312
- [Multiline Text in Excel Cell Using Apache POI](https://www.baeldung.com/apache-poi-write-multiline-text)
1413
- [Set Background Color of a Cell with Apache POI](https://www.baeldung.com/apache-poi-background-color)

apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java.orig

Lines changed: 0 additions & 128 deletions
This file was deleted.

apache-poi/src/test/java/com/baeldung/poi/excel/ExcelUtilityUnitTest.java.orig

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)