Fastexcel

This commit is contained in:
neha298 2023-04-16 23:42:27 +05:30 committed by GitHub
parent 9882fe90ea
commit d1da4b1494
3 changed files with 125 additions and 0 deletions

View File

@ -30,6 +30,16 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel</artifactId>
<version>${fastexcel.version}</version>
</dependency>
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel-reader</artifactId>
<version>${fastexcel.version}</version>
</dependency>
</dependencies>
<build>
@ -52,6 +62,7 @@
<properties>
<poi.version>5.2.0</poi.version>
<jexcel.version>1.0.6</jexcel.version>
<fastexcel.version>0.15.3</fastexcel.version>
<maven.resources.plugin.version>3.2.0</maven.resources.plugin.version>
</properties>

View File

@ -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<Integer, List<String>> readExcel(String fileLocation) throws IOException {
Map<Integer, List<String>> data = new HashMap<>();
try (FileInputStream file = new FileInputStream(fileLocation); ReadableWorkbook wb = new ReadableWorkbook(file)) {
Sheet sheet = wb.getFirstSheet();
try (Stream<Row> 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);
}
}
}

View File

@ -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<Integer, List<String>> 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();
}
}
}