Skip to content

Commit 42da6b8

Browse files
authored
Merge pull request #7 from MBenincasa/develop
Develop
2 parents e58e17f + 95c9528 commit 42da6b8

File tree

38 files changed

+1702
-1273
lines changed

38 files changed

+1702
-1273
lines changed

CHANGELOG.MD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# v0.4.0
2+
### Deprecations
3+
* All methods of the Converter class up to version 0.2.1 have been deprecated
4+
### Features
5+
* There are new methods that convert Excel <-> POJOs and Excel <-> CSV. Besides conversion between files, conversions between streams and byte arrays are now available. Data is no longer returned in a List but in a Stream
6+
* Excel <-> Json conversion is now available
7+
* The writeAndClose method is available in ExcelWorkbook
8+
* ExcelCell provides new ways to read a cell.
9+
### Fixes
10+
* The error is handled better when trying to create a Sheet already present in the Workbook
11+
* The readCell(Class<?> type) method did not always return the requested type
12+
### Removed
13+
* The WorkbookUtility and SheetUtility classes have been removed
14+
* Several ExcelUtility methods have been removed
15+
116
# v0.3.0
217
### Deprecations
318
* The WorkbookUtility and SheetUtility classes have been deprecated

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ At any time you can retrieve the associated Apache POI components.
2626

2727
```
2828
public void toPOI() {
29+
// Initialize the components
2930
Workbook workbook = excelWorkbook.getWorkbook();
3031
Sheet sheet = excelSheet.getSheet();
3132
Row row = excelRow.getRow();
@@ -36,9 +37,34 @@ public void toPOI() {
3637
One of the main features of the library is to be able to perform conversions. The **Converter** class has methods that convert **Excel <-> POJOs** and **Excel <-> CSV**.<br>
3738
This is an example of Excel to POJOs:
3839
```
39-
public void ExcelToObjects() {
40+
public void excelToObjects() {
41+
// Initialize List<ExcelToObject<?>> excelToObjects...
4042
File file = new File("./src/main/resources/car.xlsx");
41-
List<Car> cars = (List<Car>) Converter.excelToObjects(file, Car.class);
43+
Map<String, Stream<?>> map = Converter.excelFileToObjects(file, excelToObjects);
44+
for (Map.Entry<String, Stream<?>> entry : map.entrySet()) {
45+
System.out.println("Sheet: " + entry.getKey());
46+
System.out.println("Data: " + entry.getValue().toList());
47+
}
48+
}
49+
```
50+
51+
This is an example of POJOs to Excel:
52+
```
53+
public void objectsToExcel() {
54+
// Initialize List<ObjectToExcel<?>> list...
55+
list.add(new ObjectToExcel<>("Employee", Employee.class, employeeStream));
56+
list.add(new ObjectToExcel<>("Office", Office.class, officeStream));
57+
File fileOutput = Converter.objectsToExcelFile(list, Extension.XLSX, "./src/main/resources/result", true);
58+
}
59+
```
60+
61+
ExcelCell provides generic methods for reading a cell.
62+
```
63+
public void readValue() {
64+
// Initialize ExcelCell excelCell...
65+
Integer intVal = excelCell.readValue(Integer.class);
66+
Double doubleVal = excelCell.readValue();
67+
String stringVal = excelCell.readValueAsString();
4268
}
4369
```
4470

@@ -55,6 +81,8 @@ Java 17 or above.
5581
- org.projectlombok:lombok:jar:1.18.24
5682
- commons-beanutils:commons-beanutils:jar:1.9.4
5783
- com.opencsv:opencsv:jar:5.7.1
84+
- com.fasterxml.jackson.core:jackson-databind:jar:2.14.2
85+
- org.apache.logging.log4j:log4j-core:jar:2.20.0
5886
- org.junit.jupiter:junit-jupiter:jar:5.9.2
5987
- org.junit.platform:junit-platform-suite-engine:jar:1.9.2
6088

@@ -63,7 +91,7 @@ Java 17 or above.
6391
<dependency>
6492
<groupId>io.github.mbenincasa</groupId>
6593
<artifactId>java-excel-utils</artifactId>
66-
<version>x.y.z</version>
94+
<version>0.4.0</version>
6795
</dependency>
6896
```
6997

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.mbenincasa</groupId>
88
<artifactId>java-excel-utils</artifactId>
9-
<version>0.3.0</version>
9+
<version>0.4.0</version>
1010
<packaging>jar</packaging>
1111
<name>Java library with tools for Excel files</name>
1212
<description>Java library that collects tools and methods to speed up development with Excel sheets</description>
@@ -133,6 +133,16 @@
133133
<artifactId>opencsv</artifactId>
134134
<version>5.7.1</version>
135135
</dependency>
136+
<dependency>
137+
<groupId>com.fasterxml.jackson.core</groupId>
138+
<artifactId>jackson-databind</artifactId>
139+
<version>2.14.2</version>
140+
</dependency>
141+
<dependency>
142+
<groupId>org.apache.logging.log4j</groupId>
143+
<artifactId>log4j-core</artifactId>
144+
<version>2.20.0</version>
145+
</dependency>
136146
<dependency>
137147
<groupId>org.junit.jupiter</groupId>
138148
<artifactId>junit-jupiter</artifactId>

src/main/java/io/github/mbenincasa/javaexcelutils/enums/Extension.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ public enum Extension {
2929
/**
3030
* This extension is used for CSV (Comma-separated values) files
3131
*/
32-
CSV("csv", "CSV");
32+
CSV("csv", "CSV"),
33+
34+
/**
35+
* @since 0.4.0
36+
* This extension is used for JSON (JavaScript Object Notation) files
37+
*/
38+
JSON("json", "JSON");
3339

3440
/**
3541
* The extension's name
@@ -60,6 +66,6 @@ public static Extension getExcelExtension(String ext) throws ExtensionNotValidEx
6066
* @since 0.1.1
6167
*/
6268
public Boolean isExcelExtension() {
63-
return this.getType().equalsIgnoreCase("EXCEL");
69+
return this.type.equalsIgnoreCase("EXCEL");
6470
}
6571
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.mbenincasa.javaexcelutils.model.converter;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
/**
8+
* This is a support class that is used by the {@code Converter} to perform conversions from Excel to Objects
9+
* @author Mirko Benincasa
10+
* @since 0.4.0
11+
* @param <T> The class parameter, for each Sheet, that maps a row into the parameter object
12+
*/
13+
@AllArgsConstructor
14+
@Getter
15+
@Setter
16+
public class ExcelToObject<T> {
17+
18+
/**
19+
* The name of the Sheet to read
20+
*/
21+
private String sheetName;
22+
23+
/**
24+
* The object class
25+
*/
26+
private Class<T> clazz;
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.mbenincasa.javaexcelutils.model.converter;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
/**
8+
* This is a support class that is used by the {@code Converter} to perform conversions from Json to Excel
9+
* @author Mirko Benincasa
10+
* @since 0.4.0
11+
* @param <T> The class parameter, for each Sheet, that maps a Json object into the parameter object
12+
*/
13+
@AllArgsConstructor
14+
@Getter
15+
@Setter
16+
public class JsonToExcel<T> {
17+
18+
/**
19+
* The name to assign to the Sheet
20+
*/
21+
private String sheetName;
22+
23+
/**
24+
* The object class
25+
*/
26+
private Class<T> clazz;
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.mbenincasa.javaexcelutils.model.converter;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
import java.util.stream.Stream;
8+
9+
/**
10+
* This is a helper class used by {@code Converter} to convert objects in an Excel Sheet
11+
* @author Mirko Benincasa
12+
* @since 0.4.0
13+
* @param <T> The class parameter, for each sheet, which maps objects to a Sheet
14+
*/
15+
@AllArgsConstructor
16+
@Getter
17+
@Setter
18+
public class ObjectToExcel<T> {
19+
20+
/**
21+
* The name to assign to the Sheet
22+
*/
23+
private String sheetName;
24+
25+
/**
26+
* The object class
27+
*/
28+
private Class<T> clazz;
29+
30+
/**
31+
* A Stream of objects
32+
*/
33+
private Stream<T> stream;
34+
}

src/main/java/io/github/mbenincasa/javaexcelutils/model/ExcelCell.java renamed to src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelCell.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
package io.github.mbenincasa.javaexcelutils.model;
1+
package io.github.mbenincasa.javaexcelutils.model.excel;
22

33
import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException;
44
import lombok.AllArgsConstructor;
55
import lombok.EqualsAndHashCode;
66
import lombok.Getter;
7-
import org.apache.poi.ss.usermodel.Cell;
8-
import org.apache.poi.ss.usermodel.CellStyle;
9-
import org.apache.poi.ss.usermodel.FormulaEvaluator;
10-
import org.apache.poi.ss.usermodel.Row;
7+
import org.apache.poi.ss.usermodel.*;
118

129
import java.time.LocalDate;
1310
import java.time.LocalDateTime;
@@ -44,15 +41,45 @@ public ExcelRow getRow() {
4441

4542
/**
4643
* Read the value written inside the Cell
47-
* @param type The class type of the object written to the Cell
4844
* @return The value written in the Cell
4945
* @throws ReadValueException If an error occurs while reading
46+
* @since 0.4.0
5047
*/
51-
public Object readValue(Class<?> type) throws ReadValueException {
48+
public Object readValue() throws ReadValueException {
5249
Object val;
5350
switch (this.cell.getCellType()) {
5451
case BOOLEAN -> val = this.cell.getBooleanCellValue();
5552
case STRING -> val = this.cell.getStringCellValue();
53+
case NUMERIC -> val = this.cell.getNumericCellValue();
54+
case FORMULA -> val = this.cell.getCellFormula();
55+
case BLANK -> val = "";
56+
default -> throw new ReadValueException("An error occurred while reading. CellType '" + this.cell.getCellType() + "'");
57+
}
58+
59+
return val;
60+
}
61+
62+
/**
63+
* Read the value written inside the Cell
64+
* @param type The class type of the object written to the Cell
65+
* @return The value written in the Cell
66+
* @throws ReadValueException If an error occurs while reading
67+
*/
68+
public Object readValue(Class<?> type) throws ReadValueException {
69+
Object val;
70+
switch (this.cell.getCellType()) {
71+
case BOOLEAN -> {
72+
if (!Boolean.class.equals(type)) {
73+
throw new ReadValueException("This type '" + type + "' is either incompatible with the CellType '" + this.cell.getCellType() + "'");
74+
}
75+
val = this.cell.getBooleanCellValue();
76+
}
77+
case STRING -> {
78+
if (!String.class.equals(type)) {
79+
throw new ReadValueException("This type '" + type + "' is either incompatible with the CellType '" + this.cell.getCellType() + "'");
80+
}
81+
val = this.cell.getStringCellValue();
82+
}
5683
case NUMERIC -> {
5784
if (Integer.class.equals(type)) {
5885
val = (int) this.cell.getNumericCellValue();
@@ -67,7 +94,7 @@ public Object readValue(Class<?> type) throws ReadValueException {
6794
} else if (LocalDate.class.equals(type)) {
6895
val = this.cell.getLocalDateTimeCellValue().toLocalDate();
6996
} else {
70-
throw new ReadValueException("This numeric type is not supported: " + type);
97+
throw new ReadValueException("This type '" + type + "' is either incompatible with the CellType '" + this.cell.getCellType() + "' or not yet supported");
7198
}
7299
}
73100
case FORMULA -> {
@@ -85,6 +112,16 @@ public Object readValue(Class<?> type) throws ReadValueException {
85112
return val;
86113
}
87114

115+
/**
116+
* Read the value written inside the Cell as String
117+
* @return The value written in the Cell
118+
* @since 0.4.0
119+
*/
120+
public String readValueAsString() {
121+
DataFormatter formatter = new DataFormatter(true);
122+
return formatter.formatCellValue(this.cell);
123+
}
124+
88125
/**
89126
* Writes inside the cell
90127
* @param val The value to write in the Cell

src/main/java/io/github/mbenincasa/javaexcelutils/model/ExcelRow.java renamed to src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelRow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.mbenincasa.javaexcelutils.model;
1+
package io.github.mbenincasa.javaexcelutils.model.excel;
22

33
import lombok.AllArgsConstructor;
44
import lombok.EqualsAndHashCode;

src/main/java/io/github/mbenincasa/javaexcelutils/model/ExcelSheet.java renamed to src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelSheet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.mbenincasa.javaexcelutils.model;
1+
package io.github.mbenincasa.javaexcelutils.model.excel;
22

33
import lombok.AllArgsConstructor;
44
import lombok.EqualsAndHashCode;

0 commit comments

Comments
 (0)