Skip to content

Commit eb891a5

Browse files
committed
Added methods to convert Json files to Excel. Added the remaining javadocs
1 parent e7bae35 commit eb891a5

File tree

11 files changed

+527
-10
lines changed

11 files changed

+527
-10
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public enum Extension {
3232
CSV("csv", "CSV"),
3333

3434
/**
35+
* @since 0.4.0
3536
* This extension is used for JSON (JavaScript Object Notation) files
3637
*/
3738
JSON("json", "JSON");
@@ -65,6 +66,6 @@ public static Extension getExcelExtension(String ext) throws ExtensionNotValidEx
6566
* @since 0.1.1
6667
*/
6768
public Boolean isExcelExtension() {
68-
return this.getType().equalsIgnoreCase("EXCEL");
69+
return this.type.equalsIgnoreCase("EXCEL");
6970
}
7071
}

src/main/java/io/github/mbenincasa/javaexcelutils/model/converter/ExcelToObject.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
import lombok.Getter;
55
import lombok.Setter;
66

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+
*/
713
@AllArgsConstructor
814
@Getter
915
@Setter
1016
public class ExcelToObject<T> {
1117

18+
/**
19+
* The name of the Sheet to read
20+
*/
1221
private String sheetName;
22+
23+
/**
24+
* The object class
25+
*/
1326
private Class<T> clazz;
1427
}
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+
}

src/main/java/io/github/mbenincasa/javaexcelutils/model/converter/ObjectToExcel.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,29 @@
66

77
import java.util.stream.Stream;
88

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+
*/
915
@AllArgsConstructor
1016
@Getter
1117
@Setter
1218
public class ObjectToExcel<T> {
1319

20+
/**
21+
* The name to assign to the Sheet
22+
*/
1423
private String sheetName;
24+
25+
/**
26+
* The object class
27+
*/
1528
private Class<T> clazz;
29+
30+
/**
31+
* A Stream of objects
32+
*/
1633
private Stream<T> stream;
1734
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public ExcelRow getRow() {
3939
return new ExcelRow(row, row.getRowNum());
4040
}
4141

42+
/**
43+
* Read the value written inside the Cell
44+
* @return The value written in the Cell
45+
* @throws ReadValueException If an error occurs while reading
46+
* @since 0.4.0
47+
*/
4248
public Object readValue() throws ReadValueException {
4349
Object val;
4450
switch (this.cell.getCellType()) {
@@ -106,6 +112,11 @@ public Object readValue(Class<?> type) throws ReadValueException {
106112
return val;
107113
}
108114

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+
*/
109120
public String readValueAsString() {
110121
DataFormatter formatter = new DataFormatter(true);
111122
return formatter.formatCellValue(this.cell);

src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelWorkbook.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public void close(CSVWriter writer) throws IOException {
190190
}
191191

192192
/**
193+
* Close a workbook
193194
* @param outputStream The {@code OutputStream} to close
194195
* @param reader The {@code CSVReader} to close
195196
* @throws IOException If an I/O error has occurred
@@ -351,6 +352,13 @@ public FormulaEvaluator getFormulaEvaluator() {
351352
return this.workbook.getCreationHelper().createFormulaEvaluator();
352353
}
353354

355+
356+
/**
357+
* Writes the OutputStream to the Workbook and then closes them
358+
* @param outputStream The {@code OutputStream} to close
359+
* @throws IOException If an I/O error has occurred
360+
* @since 0.4.0
361+
*/
354362
public void writeAndClose(OutputStream outputStream) throws IOException {
355363
this.workbook.write(outputStream);
356364
this.close(outputStream);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.mbenincasa.javaexcelutils.samples.convertJsonFileToExcelFile;
2+
3+
import io.github.mbenincasa.javaexcelutils.enums.Extension;
4+
import io.github.mbenincasa.javaexcelutils.model.converter.JsonToExcel;
5+
import io.github.mbenincasa.javaexcelutils.tools.Converter;
6+
7+
import java.io.File;
8+
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
13+
File jsonFile = new File("./src/main/resources/office.json");
14+
15+
try {
16+
System.out.println("Start the conversion...");
17+
JsonToExcel<Office> officeJsonToExcel = new JsonToExcel<>("office", Office.class);
18+
File excelFile = Converter.jsonToExcelFile(jsonFile, officeJsonToExcel, Extension.XLSX, "./src/main/resources/from-json-to-excel", true);
19+
System.out.println("... completed");
20+
System.out.println("The file is ready. Path: " + excelFile.getAbsolutePath());
21+
} catch (Exception e) {
22+
System.err.println("There was an error. Check the console");
23+
throw new RuntimeException(e);
24+
}
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.mbenincasa.javaexcelutils.samples.convertJsonFileToExcelFile;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.github.mbenincasa.javaexcelutils.annotations.ExcelBodyStyle;
5+
import io.github.mbenincasa.javaexcelutils.annotations.ExcelField;
6+
import io.github.mbenincasa.javaexcelutils.annotations.ExcelHeaderStyle;
7+
import lombok.AllArgsConstructor;
8+
import lombok.NoArgsConstructor;
9+
import lombok.Setter;
10+
import lombok.ToString;
11+
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@Setter
15+
@ToString
16+
@ExcelHeaderStyle(autoSize = true)
17+
@ExcelBodyStyle
18+
public class Office {
19+
20+
@ExcelField(name = "CITY")
21+
@JsonProperty("CITY")
22+
private String city;
23+
@ExcelField(name = "PROVINCE")
24+
@JsonProperty("PROVINCE")
25+
private String province;
26+
@ExcelField(name = "NUMBER OF STATIONS")
27+
@JsonProperty("NUMBER OF STATIONS")
28+
private Integer numStations;
29+
}

0 commit comments

Comments
 (0)