From 2859935089915c46036c7d27b29c6913a2efe156 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Mon, 7 Mar 2022 16:32:43 +0000 Subject: [PATCH] [JAVA-10463] Split apache-poi module --- apache-poi-2/README.md | 1 + apache-poi-2/pom.xml | 2 +- .../poi/excel/setformula/ExcelFormula.java | 51 ++++--- .../poi/excel/setformula/SetFormulaTest.xlsx | Bin .../setformula/ExcelFormulaUnitTest.java | 11 +- apache-poi/README.md | 1 - .../baeldung/poi/excel/ExcelUtility.java.orig | 128 ------------------ .../poi/excel/ExcelUtilityUnitTest.java.orig | 112 --------------- 8 files changed, 33 insertions(+), 273 deletions(-) rename {apache-poi => apache-poi-2}/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java (88%) rename {apache-poi => apache-poi-2}/src/main/resources/com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx (100%) rename {apache-poi => apache-poi-2}/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java (85%) delete mode 100644 apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java.orig delete mode 100644 apache-poi/src/test/java/com/baeldung/poi/excel/ExcelUtilityUnitTest.java.orig diff --git a/apache-poi-2/README.md b/apache-poi-2/README.md index f55ec7eb6a..2c0deec575 100644 --- a/apache-poi-2/README.md +++ b/apache-poi-2/README.md @@ -10,4 +10,5 @@ This module contains articles about Apache POI. - [Microsoft Word Processing in Java with Apache POI](https://www.baeldung.com/java-microsoft-word-with-apache-poi) - [Creating a MS PowerPoint Presentation in Java](https://www.baeldung.com/apache-poi-slideshow) - [Finding the Last Row in an Excel Spreadsheet From Java](https://www.baeldung.com/java-excel-find-last-row) +- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas) - More articles: [[<-- prev]](../apache-poi) diff --git a/apache-poi-2/pom.xml b/apache-poi-2/pom.xml index a46365c63c..30270cd7be 100644 --- a/apache-poi-2/pom.xml +++ b/apache-poi-2/pom.xml @@ -22,7 +22,7 @@ - 5.0.0 + 5.2.0 diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java b/apache-poi-2/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java similarity index 88% rename from apache-poi/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java rename to apache-poi-2/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java index f5179b19c9..ccff1fa709 100644 --- a/apache-poi/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java +++ b/apache-poi-2/src/main/java/com/baeldung/poi/excel/setformula/ExcelFormula.java @@ -1,26 +1,25 @@ -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(); - } -} +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.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(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-2/src/main/resources/com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx similarity index 100% rename from apache-poi/src/main/resources/com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx rename to apache-poi-2/src/main/resources/com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java b/apache-poi-2/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java similarity index 85% rename from apache-poi/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java rename to apache-poi-2/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java index fa5baa37fa..7a0f15b3f7 100644 --- a/apache-poi/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java +++ b/apache-poi-2/src/test/java/com/baeldung/poi/excel/setformula/ExcelFormulaUnitTest.java @@ -3,18 +3,19 @@ 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; +import static org.junit.jupiter.api.Assertions.assertEquals; + class ExcelFormulaUnitTest { - private static String FILE_NAME = "com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx"; + private static final String FILE_NAME = "com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx"; + private String fileLocation; private ExcelFormula excelFormula; @@ -26,7 +27,7 @@ class ExcelFormulaUnitTest { @Test void givenExcelData_whenSetFormula_thenSuccess() throws IOException { - FileInputStream inputStream = new FileInputStream(new File(fileLocation)); + FileInputStream inputStream = new FileInputStream(fileLocation); XSSFWorkbook wb = new XSSFWorkbook(inputStream); XSSFSheet sheet = wb.getSheetAt(0); double resultColumnA = 0; @@ -46,6 +47,6 @@ class ExcelFormulaUnitTest { double resultValue = excelFormula.setFormula(fileLocation, wb, sumFormulaForColumnA + "-" + sumFormulaForColumnB); - Assert.assertEquals(resultColumnA - resultColumnB, resultValue, 0d); + assertEquals(resultColumnA - resultColumnB, resultValue, 0d); } } diff --git a/apache-poi/README.md b/apache-poi/README.md index ed30d9a4f3..9edf69d67c 100644 --- a/apache-poi/README.md +++ b/apache-poi/README.md @@ -8,7 +8,6 @@ This module contains articles about Apache POI. - [Merge Cells in Excel Using Apache POI](https://www.baeldung.com/java-apache-poi-merge-cells) - [Get String Value of Excel Cell with Apache POI](https://www.baeldung.com/java-apache-poi-cell-string-value) - [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula) -- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas) - [Insert a Row in Excel Using Apache POI](https://www.baeldung.com/apache-poi-insert-excel-row) - [Multiline Text in Excel Cell Using Apache POI](https://www.baeldung.com/apache-poi-write-multiline-text) - [Set Background Color of a Cell with Apache POI](https://www.baeldung.com/apache-poi-background-color) diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java.orig b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java.orig deleted file mode 100644 index c058f3abcf..0000000000 --- a/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java.orig +++ /dev/null @@ -1,128 +0,0 @@ -package com.baeldung.poi.excel; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellType; -import org.apache.poi.ss.usermodel.DateUtil; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; - -public class ExcelUtility { -<<<<<<< HEAD - private static final String ENDLINE = System.getProperty("line.separator"); - - public static String readExcel(String filePath) throws IOException { - File file = new File(filePath); - FileInputStream inputStream = null; - StringBuilder toReturn = new StringBuilder(); - try { - inputStream = new FileInputStream(file); - Workbook baeuldungWorkBook = new XSSFWorkbook(inputStream); - for (Sheet sheet : baeuldungWorkBook) { - toReturn.append("--------------------------------------------------------------------") - .append(ENDLINE); - toReturn.append("Worksheet :") - .append(sheet.getSheetName()) - .append(ENDLINE); - toReturn.append("--------------------------------------------------------------------") - .append(ENDLINE); - int firstRow = sheet.getFirstRowNum(); - int lastRow = sheet.getLastRowNum(); - for (int index = firstRow + 1; index <= lastRow; index++) { - Row row = sheet.getRow(index); - toReturn.append("|| "); - for (int cellIndex = row.getFirstCellNum(); cellIndex < row.getLastCellNum(); cellIndex++) { - Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); - printCellValue(cell, toReturn); - } - toReturn.append(" ||") - .append(ENDLINE); - } - } - inputStream.close(); - - } catch (IOException e) { - throw e; - } - return toReturn.toString(); - } - - public static void printCellValue(Cell cell, StringBuilder toReturn) { - CellType cellType = cell.getCellType() - .equals(CellType.FORMULA) ? cell.getCachedFormulaResultType() : cell.getCellType(); - if (cellType.equals(CellType.STRING)) { - toReturn.append(cell.getStringCellValue()) - .append(" | "); - } - if (cellType.equals(CellType.NUMERIC)) { - if (DateUtil.isCellDateFormatted(cell)) { - toReturn.append(cell.getDateCellValue()) - .append(" | "); - } else { - toReturn.append(cell.getNumericCellValue()) - .append(" | "); - } - } - if (cellType.equals(CellType.BOOLEAN)) { - toReturn.append(cell.getBooleanCellValue()) - .append(" | "); - } - } -======= - private static final String ENDLINE = System.getProperty("line.separator"); - - public static String readExcel(String filePath) throws IOException { - File file = new File(filePath); - FileInputStream inputStream = null; - StringBuilder toReturn = new StringBuilder(); - try { - inputStream = new FileInputStream(file); - Workbook baeuldungWorkBook = new XSSFWorkbook(inputStream); - for (Sheet sheet : baeuldungWorkBook) { - toReturn.append("--------------------------------------------------------------------").append(ENDLINE); - toReturn.append("Worksheet :").append(sheet.getSheetName()).append(ENDLINE); - toReturn.append("--------------------------------------------------------------------").append(ENDLINE); - int firstRow = sheet.getFirstRowNum(); - int lastRow = sheet.getLastRowNum(); - for (int index = firstRow + 1; index <= lastRow; index++) { - Row row = sheet.getRow(index); - toReturn.append("|| "); - for (int cellIndex = row.getFirstCellNum(); cellIndex < row.getLastCellNum(); cellIndex++) { - Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); - printCellValue(cell, toReturn); - } - toReturn.append(" ||").append(ENDLINE); - } - } - inputStream.close(); - - } catch (IOException e) { - throw e; - } - return toReturn.toString(); - } - - public static void printCellValue(Cell cell, StringBuilder toReturn) { - CellType cellType = cell.getCellType().equals(CellType.FORMULA) ? cell.getCachedFormulaResultType() - : cell.getCellType(); - if (cellType.equals(CellType.STRING)) { - toReturn.append(cell.getStringCellValue()).append(" | "); - } - if (cellType.equals(CellType.NUMERIC)) { - if (DateUtil.isCellDateFormatted(cell)) { - toReturn.append(cell.getDateCellValue()).append(" | "); - } else { - toReturn.append(cell.getNumericCellValue()).append(" | "); - } - } - if (cellType.equals(CellType.BOOLEAN)) { - toReturn.append(cell.getBooleanCellValue()).append(" | "); - } - } ->>>>>>> master -} \ No newline at end of file diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelUtilityUnitTest.java.orig b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelUtilityUnitTest.java.orig deleted file mode 100644 index cfc3062b5a..0000000000 --- a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelUtilityUnitTest.java.orig +++ /dev/null @@ -1,112 +0,0 @@ -package com.baeldung.poi.excel; - -import static org.junit.Assert.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Paths; -import java.text.ParseException; -import java.text.SimpleDateFormat; - -import org.junit.Before; -import org.junit.Test; - -public class ExcelUtilityUnitTest { -<<<<<<< HEAD - private static final String FILE_NAME = "baeldung.xlsx"; - private String fileLocation; - private static final String ENDLINE = System.getProperty("line.separator"); - private StringBuilder output; - - @Before - public void setupUnitTest() throws IOException, URISyntaxException, ParseException { - output = new StringBuilder(); - output.append("--------------------------------------------------------------------") - .append(ENDLINE); - output.append("Worksheet :Sheet1") - .append(ENDLINE); - output.append("--------------------------------------------------------------------") - .append(ENDLINE); - output.append("|| Name1 | Surname1 | 3.55696564113E11 | ") - .append(new SimpleDateFormat("dd/MM/yyyy").parse("4/11/2021") - .toString()) - .append(" | ‡ | ||") - .append(ENDLINE); - output.append("|| Name2 | Surname2 | 5.646513512E9 | ") - .append(new SimpleDateFormat("dd/MM/yyyy").parse("4/12/2021") - .toString()) - .append(" | false | ||") - .append(ENDLINE); - output.append("|| Name3 | Surname3 | 3.55696564113E11 | ") - .append(new SimpleDateFormat("dd/MM/yyyy").parse("4/11/2021") - .toString()) - .append(" | 7.17039641738E11 | ||") - .append(ENDLINE); - output.append("--------------------------------------------------------------------") - .append(ENDLINE); - output.append("Worksheet :Sheet2") - .append(ENDLINE); - output.append("--------------------------------------------------------------------") - .append(ENDLINE); - output.append("|| Name4 | Surname4 | 3.55675623232E11 | 13/04/2021 | ||") - .append(ENDLINE); - - fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME) - .toURI()) - .toString(); - } - - @Test - public void givenStringPath_whenReadExcel_thenReturnStringValue() throws IOException { - assertEquals(output.toString(), ExcelUtility.readExcel(fileLocation)); - - } - - @Test - public void givenStringPath_whenReadExcel_thenThrowException() { - assertThrows(IOException.class, () -> { - ExcelUtility.readExcel("baeldung"); - }); - } -======= - private static final String FILE_NAME = "baeldung.xlsx"; - private String fileLocation; - private static final String ENDLINE = System.getProperty("line.separator"); - private StringBuilder output; - - @Before - public void setupUnitTest() throws IOException, URISyntaxException, ParseException { - output = new StringBuilder(); - output.append("--------------------------------------------------------------------").append(ENDLINE); - output.append("Worksheet :Sheet1").append(ENDLINE); - output.append("--------------------------------------------------------------------").append(ENDLINE); - output.append("|| Name1 | Surname1 | 3.55696564113E11 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/11/2021").toString()).append(" | ‡ | ||") - .append(ENDLINE); - output.append("|| Name2 | Surname2 | 5.646513512E9 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/12/2021").toString()).append(" | false | ||") - .append(ENDLINE); - output.append("|| Name3 | Surname3 | 3.55696564113E11 | ").append(new SimpleDateFormat("dd/MM/yyyy").parse("4/11/2021").toString()).append(" | 7.17039641738E11 | ||") - .append(ENDLINE); - output.append("--------------------------------------------------------------------").append(ENDLINE); - output.append("Worksheet :Sheet2").append(ENDLINE); - output.append("--------------------------------------------------------------------").append(ENDLINE); - output.append("|| Name4 | Surname4 | 3.55675623232E11 | 13/04/2021 | ||").append(ENDLINE); - - fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString(); - } - - @Test - public void givenStringPath_whenReadExcel_thenReturnStringValue() throws IOException { - assertEquals(output.toString(), ExcelUtility.readExcel(fileLocation)); - - } - - @Test - public void givenStringPath_whenReadExcel_thenThrowException() { - assertThrows(IOException.class, () -> { - ExcelUtility.readExcel("baeldung"); - }); - } ->>>>>>> master - -}