From 1f08721fb6b06e78b884a438ee040836029da70b Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Sat, 25 Dec 2021 20:25:49 +0530 Subject: [PATCH] Bael 5200 numeric excel (#11594) * Changes for Numeric Format * Update NumberCellValueUnitTest.java * Corrected the workbook format * Unwanted change removed * Corrected the spell issue * Update NumberCellValueUnitTest.java * Fixed review comment * Update NumberCellValueUnitTest.java --- .../newcolumn/numeric/ExcelNumericFormat.java | 19 ++++ .../numeric/NumberCellValueUnitTest.java | 105 ++++++++++++++++++ .../excel/multilinetext/MultilineText.java | 1 - 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 apache-poi-2/src/main/java/com/baeldung/poi/excel/newcolumn/numeric/ExcelNumericFormat.java create mode 100644 apache-poi-2/src/test/java/com/baeldung/poi/excel/newcolumn/numeric/NumberCellValueUnitTest.java diff --git a/apache-poi-2/src/main/java/com/baeldung/poi/excel/newcolumn/numeric/ExcelNumericFormat.java b/apache-poi-2/src/main/java/com/baeldung/poi/excel/newcolumn/numeric/ExcelNumericFormat.java new file mode 100644 index 0000000000..02148c783b --- /dev/null +++ b/apache-poi-2/src/main/java/com/baeldung/poi/excel/newcolumn/numeric/ExcelNumericFormat.java @@ -0,0 +1,19 @@ +package com.baeldung.poi.excel.newcolumn.numeric; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.DataFormat; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Workbook; + +public class ExcelNumericFormat { + + public static void applyNumericFormat(Workbook outWorkbook, Row row, Cell cell, Double value, String styleFormat) { + CellStyle style = outWorkbook.createCellStyle(); + DataFormat format = outWorkbook.createDataFormat(); + style.setDataFormat(format.getFormat(styleFormat)); + cell.setCellValue(value); + cell.setCellStyle(style); + } + +} diff --git a/apache-poi-2/src/test/java/com/baeldung/poi/excel/newcolumn/numeric/NumberCellValueUnitTest.java b/apache-poi-2/src/test/java/com/baeldung/poi/excel/newcolumn/numeric/NumberCellValueUnitTest.java new file mode 100644 index 0000000000..ae1090fd69 --- /dev/null +++ b/apache-poi-2/src/test/java/com/baeldung/poi/excel/newcolumn/numeric/NumberCellValueUnitTest.java @@ -0,0 +1,105 @@ +package com.baeldung.poi.excel.newcolumn.numeric; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +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; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.DecimalFormat; + +public class NumberCellValueUnitTest { + + @Test + public void decimalDisplay_whenAddedDouble_thenNumericCellCreated() throws IOException { + File file = new File("number_test.xlsx"); + try (Workbook outWorkbook = new XSSFWorkbook()) { + Sheet sheet = outWorkbook.createSheet("Numeric Sheet"); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + ExcelNumericFormat.applyNumericFormat(outWorkbook, row, cell, 10.251, "0.00"); + FileOutputStream fileOut = new FileOutputStream(file); + outWorkbook.write(fileOut); + fileOut.close(); + } + try (Workbook inWorkbook = new XSSFWorkbook("number_test.xlsx")) { + Sheet sheet = inWorkbook.cloneSheet(0); + Row row = sheet.getRow(0); + Assertions.assertEquals(10.251, row.getCell(0) + .getNumericCellValue()); + file.delete(); + } + } + + @Test + public void decimalRoundedDisplay_whenAddedDouble_thenNumericCellCreated() throws IOException { + File file = new File("number_test.xlsx"); + try (Workbook outWorkbook = new XSSFWorkbook()) { + Sheet sheet = outWorkbook.createSheet("Numeric Sheet"); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + ExcelNumericFormat.applyNumericFormat(outWorkbook, row, cell, 10.251123, "#,##0.0000"); + FileOutputStream fileOut = new FileOutputStream(file); + outWorkbook.write(fileOut); + fileOut.close(); + } + try (Workbook inWorkbook = new XSSFWorkbook("number_test.xlsx")) { + Sheet sheet = inWorkbook.cloneSheet(0); + Row row = sheet.getRow(0); + Assertions.assertEquals(10.251123, row.getCell(0) + .getNumericCellValue()); + file.delete(); + } + } + + @Test + public void decimalDisplayInXLS_whenAddedDouble_thenNumericCellCreated() throws IOException { + File file = new File("number_test.xls"); + try (Workbook outWorkbook = new HSSFWorkbook()) { + Sheet sheet = outWorkbook.createSheet("Numeric Sheet"); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + ExcelNumericFormat.applyNumericFormat(outWorkbook, row, cell, 10.251, "0.00"); + FileOutputStream fileOut = new FileOutputStream(file); + outWorkbook.write(fileOut); + fileOut.close(); + } + try (Workbook inWorkbook = new HSSFWorkbook(new FileInputStream(file))) { + Sheet sheet = inWorkbook.cloneSheet(0); + Row row = sheet.getRow(0); + Assertions.assertEquals(10.251, row.getCell(0) + .getNumericCellValue()); + file.delete(); + } + } + + @Test + public void decimalValue_whenAddedDouble_thenNumericCellCreated() throws IOException { + File file = new File("number_test.xlsx"); + try (Workbook outWorkbook = new XSSFWorkbook()) { + Sheet sheet = outWorkbook.createSheet("Numeric Sheet"); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + DecimalFormat df = new DecimalFormat("#,###.##"); + ExcelNumericFormat.applyNumericFormat(outWorkbook, row, cell, Double.valueOf(df.format(10.251)), "#,###.##"); + + FileOutputStream fileOut = new FileOutputStream(file); + outWorkbook.write(fileOut); + fileOut.close(); + } + try (Workbook inWorkbook = new XSSFWorkbook("number_test.xlsx")) { + Sheet sheet = inWorkbook.cloneSheet(0); + Row row = sheet.getRow(0); + Assertions.assertEquals(10.25, row.getCell(0) + .getNumericCellValue()); + file.delete(); + } + } +} \ No newline at end of file diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/multilinetext/MultilineText.java b/apache-poi/src/main/java/com/baeldung/poi/excel/multilinetext/MultilineText.java index 2e0cc5770e..231d7b8968 100644 --- a/apache-poi/src/main/java/com/baeldung/poi/excel/multilinetext/MultilineText.java +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/multilinetext/MultilineText.java @@ -2,7 +2,6 @@ package com.baeldung.poi.excel.multilinetext; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.Row; public class MultilineText { public void formatMultilineText(Cell cell, int cellNumber) {