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
This commit is contained in:
Amitabh Tiwari 2021-12-25 20:25:49 +05:30 committed by GitHub
parent b9cc959bed
commit 9b905cef77
3 changed files with 124 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}
}

View File

@ -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) {