diff --git a/apache-poi/pom.xml b/apache-poi/pom.xml index 546eedec5b..78781cf215 100644 --- a/apache-poi/pom.xml +++ b/apache-poi/pom.xml @@ -30,6 +30,16 @@ + + org.dhatim + fastexcel + ${fastexcel.version} + + + org.dhatim + fastexcel-reader + ${fastexcel.version} + @@ -52,6 +62,7 @@ 5.2.0 1.0.6 + 0.15.3 3.2.0 diff --git a/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java b/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java new file mode 100644 index 0000000000..524f345184 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/fastexcel/FastexcelHelper.java @@ -0,0 +1,64 @@ +package com.baeldung.fastexcel; + +import org.dhatim.fastexcel.Workbook; +import org.dhatim.fastexcel.Worksheet; +import org.dhatim.fastexcel.reader.Cell; +import org.dhatim.fastexcel.reader.ReadableWorkbook; +import org.dhatim.fastexcel.reader.Row; +import org.dhatim.fastexcel.reader.Sheet; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +public class FastexcelHelper { + + public Map> readExcel(String fileLocation) throws IOException { + Map> data = new HashMap<>(); + + try (FileInputStream file = new FileInputStream(fileLocation); ReadableWorkbook wb = new ReadableWorkbook(file)) { + Sheet sheet = wb.getFirstSheet(); + try (Stream rows = sheet.openStream()) { + rows.forEach(r -> { + data.put(r.getRowNum(), new ArrayList<>()); + + for (Cell cell : r) { + data.get(r.getRowNum()).add(cell.getRawValue()); + } + }); + } + } + + return data; + } + + public void writeExcel() throws IOException { + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + String fileLocation = path.substring(0, path.length() - 1) + "fastexcel.xlsx"; + + try (OutputStream os = Files.newOutputStream(Paths.get(fileLocation)); Workbook wb = new Workbook(os, "MyApplication", "1.0")) { + Worksheet ws = wb.newWorksheet("Sheet 1"); + + ws.width(0, 25); + ws.width(1, 15); + + + ws.range(0, 0, 0, 1).style().fontName("Arial").fontSize(16).bold().fillColor("3366FF").set(); + ws.value(0, 0, "Name"); + ws.value(0, 1, "Age"); + + ws.range(1, 0, 1, 1).style().wrapText(true).set(); + ws.value(2, 0, "John Smith"); + ws.value(2, 1, 20L); + } + } +} diff --git a/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java b/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java new file mode 100644 index 0000000000..8c1d568b25 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/fastexcel/FastexcelIntegrationTest.java @@ -0,0 +1,50 @@ +package com.baeldung.fastexcel; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class FastexcelIntegrationTest { + + private FastexcelHelper fastexcelHelper; + private static String FILE_NAME = "fastexcel.xlsx"; + private String fileLocation; + + @Before + public void generateExcelFile() throws IOException { + + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + fileLocation = path.substring(0, path.length() - 1) + FILE_NAME; + System.out.println(fileLocation); + + fastexcelHelper = new FastexcelHelper(); + fastexcelHelper.writeExcel(); + } + + @Test + public void whenParsingExcelFile_thenCorrect() throws IOException { + Map> data = fastexcelHelper.readExcel(fileLocation); + + assertEquals("Name", data.get(1).get(0)); + assertEquals("Age", data.get(1).get(1)); + + assertEquals("John Smith", data.get(3).get(0)); + assertEquals("20", data.get(3).get(1)); + } + + @After + public void cleanup() { + File testFile = new File(fileLocation); + if (testFile.exists()) { + testFile.delete(); + } + } +} \ No newline at end of file