Skip to content

Commit 297f871

Browse files
committed
New methods to read the value of a cell. Added methods to convert an Excel file to Json
1 parent e12c5d5 commit 297f871

File tree

11 files changed

+320
-80
lines changed

11 files changed

+320
-80
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Java 17 or above.
5555
- org.projectlombok:lombok:jar:1.18.24
5656
- commons-beanutils:commons-beanutils:jar:1.9.4
5757
- com.opencsv:opencsv:jar:5.7.1
58+
- com.fasterxml.jackson.core:jackson-databind:jar:2.14.2
59+
- org.apache.logging.log4j:log4j-core:jar:2.20.0
5860
- org.junit.jupiter:junit-jupiter:jar:5.9.2
5961
- org.junit.platform:junit-platform-suite-engine:jar:1.9.2
6062

@@ -63,7 +65,7 @@ Java 17 or above.
6365
<dependency>
6466
<groupId>io.github.mbenincasa</groupId>
6567
<artifactId>java-excel-utils</artifactId>
66-
<version>x.y.z</version>
68+
<version>0.3.0</version>
6769
</dependency>
6870
```
6971

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ 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+
* This extension is used for JSON (JavaScript Object Notation) files
36+
*/
37+
JSON("json", "JSON");
3338

3439
/**
3540
* The extension's name

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
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;
@@ -42,6 +39,20 @@ public ExcelRow getRow() {
4239
return new ExcelRow(row, row.getRowNum());
4340
}
4441

42+
public Object readValue() throws ReadValueException {
43+
Object val;
44+
switch (this.cell.getCellType()) {
45+
case BOOLEAN -> val = this.cell.getBooleanCellValue();
46+
case STRING -> val = this.cell.getStringCellValue();
47+
case NUMERIC -> val = this.cell.getNumericCellValue();
48+
case FORMULA -> val = this.cell.getCellFormula();
49+
case BLANK -> val = "";
50+
default -> throw new ReadValueException("An error occurred while reading. CellType '" + this.cell.getCellType() + "'");
51+
}
52+
53+
return val;
54+
}
55+
4556
/**
4657
* Read the value written inside the Cell
4758
* @param type The class type of the object written to the Cell
@@ -95,6 +106,11 @@ public Object readValue(Class<?> type) throws ReadValueException {
95106
return val;
96107
}
97108

109+
public String readValueAsString() {
110+
DataFormatter formatter = new DataFormatter(true);
111+
return formatter.formatCellValue(this.cell);
112+
}
113+
98114
/**
99115
* Writes inside the cell
100116
* @param val The value to write in the Cell

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.mbenincasa.javaexcelutils.enums.Extension;
66
import io.github.mbenincasa.javaexcelutils.exceptions.ExtensionNotValidException;
77
import io.github.mbenincasa.javaexcelutils.exceptions.OpenWorkbookException;
8+
import io.github.mbenincasa.javaexcelutils.exceptions.SheetAlreadyExistsException;
89
import io.github.mbenincasa.javaexcelutils.exceptions.SheetNotFoundException;
910
import lombok.AllArgsConstructor;
1011
import lombok.EqualsAndHashCode;
@@ -207,7 +208,8 @@ public Integer countSheets() {
207208
return this.workbook.getNumberOfSheets();
208209
}
209210

210-
/** The list of Sheets related to the Workbook
211+
/**
212+
* The list of Sheets related to the Workbook
211213
* @return A list of Sheets
212214
*/
213215
public List<ExcelSheet> getSheets() {
@@ -231,10 +233,15 @@ public ExcelSheet createSheet() {
231233
* Create a new Sheet inside the Workbook
232234
* @param sheetName The name of the sheet to create
233235
* @return The newly created Sheet
236+
* @throws SheetAlreadyExistsException If you try to insert a Sheet that already exists
234237
*/
235-
public ExcelSheet createSheet(String sheetName) {
236-
Sheet sheet = this.workbook.createSheet(sheetName);
237-
return new ExcelSheet(sheet, this.workbook.getSheetIndex(sheet), sheet.getSheetName());
238+
public ExcelSheet createSheet(String sheetName) throws SheetAlreadyExistsException {
239+
try {
240+
Sheet sheet = this.workbook.createSheet(sheetName);
241+
return new ExcelSheet(sheet, this.workbook.getSheetIndex(sheet), sheet.getSheetName());
242+
} catch (IllegalArgumentException ex) {
243+
throw new SheetAlreadyExistsException(ex.getMessage());
244+
}
238245
}
239246

240247
/**

src/main/java/io/github/mbenincasa/javaexcelutils/samples/convertExcelFileToCsvFile/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void main(String[] args) {
1313

1414
try {
1515
System.out.println("Start the conversion...");
16-
Map<String, File> fileMap = Converter.excelToCsvFile(excelFile, "./src/main/resources/");
16+
Map<String, File> fileMap = Converter.excelToCsvFile(excelFile, "./src/main/resources");
1717
System.out.println("... completed");
1818
for (Map.Entry<String, File> entry : fileMap.entrySet()) {
1919
System.out.println("The file is ready. Path: " + entry.getValue().getAbsolutePath());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.mbenincasa.javaexcelutils.samples.convertExcelFileToJsonFile;
2+
3+
import io.github.mbenincasa.javaexcelutils.tools.Converter;
4+
5+
import java.io.File;
6+
7+
public class Main {
8+
9+
public static void main(String[] args) {
10+
11+
File excelFile = new File("./src/main/resources/employee.xlsx");
12+
13+
try {
14+
System.out.println("Start the conversion...");
15+
File jsonFile = Converter.excelToJsonFile(excelFile, "./src/main/resources/result");
16+
System.out.println("... completed");
17+
System.out.println("The file is ready. Path: " + jsonFile.getAbsolutePath());
18+
} catch (Exception e) {
19+
System.err.println("There was an error. Check the console");
20+
throw new RuntimeException(e);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)