Skip to content

Commit 0f64684

Browse files
authored
Merge pull request #3 from MBenincasa/develop
RELEASE v0.2.1 - I have handled the case of unsupported values in con…
2 parents 4781e40 + f424da3 commit 0f64684

File tree

6 files changed

+107
-23
lines changed

6 files changed

+107
-23
lines changed

CHANGELOG.MD

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v0.2.1
2+
### Features
3+
* New services for writing to an existing Excel file that take different input parameters
4+
### Fixes
5+
* Converting a value within a cell to an object returned null or failed. An error log is now written when an unsupported type is passed
6+
17
# v0.2.0
28
### Deprecations
39
* All old tools are deprecated starting with this version. They have been replaced by new tools based on static methods.
@@ -6,7 +12,7 @@
612
* There are new utility methods such as returning the index of the last row or the last column of a row.
713
### Fixes
814
* The FileInputStream was not closed when the Workbook was opened from a File
9-
### Cosmetics
15+
### Enchantment
1016
* Removed some checks and repetitive code
1117

1218
# v0.1.1

pom.xml

+1-1
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.2.0</version>
9+
<version>0.2.1</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>

src/main/java/samples/convertCsvFileToExcelFile/Main.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package samples.convertCsvFileToExcelFile;
22

33
import enums.Extension;
4-
import org.apache.poi.ss.usermodel.Workbook;
54
import tools.Converter;
6-
import tools.WorkbookUtility;
75

86
import java.io.File;
9-
import java.io.FileOutputStream;
107

118
public class Main {
129

@@ -19,12 +16,7 @@ public static void main(String[] args) {
1916
System.out.println("Start the conversion...");
2017
File excelFile = Converter.csvToExcel(csvFile, "./src/main/resources/", "employee_2", Extension.XLSX);
2118
System.out.println("First conversion completed...");
22-
23-
Workbook workbook = WorkbookUtility.open(excelFile);
24-
Converter.csvToExistingExcel(workbook, csvFile2);
25-
FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
26-
workbook.write(fileOutputStream);
27-
WorkbookUtility.close(workbook, fileOutputStream);
19+
Converter.csvToExistingExcel(excelFile, csvFile2);
2820
System.out.println("The file is ready. Path: " + excelFile.getAbsolutePath());
2921
} catch (Exception e) {
3022
System.err.println("There was an error. Check the console");

src/main/java/samples/convertExcelFileToObjectsSample/Main.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public static void main(String[] args) {
1313

1414
try {
1515
System.out.println("Start the conversion...");
16-
List<Car> employees = (List<Car>) Converter.excelToObjects(file, Car.class);
17-
System.out.println("The list is ready. List: " + employees.toString());
16+
List<Car> cars = (List<Car>) Converter.excelToObjects(file, Car.class);
17+
System.out.println("The list is ready. List: " + cars);
1818
} catch (Exception e) {
1919
System.err.println("There was an error. Check the console");
2020
throw new RuntimeException(e);

src/main/java/samples/convertObjectsToExcelFileSample/Main.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package samples.convertObjectsToExcelFileSample;
22

33
import enums.Extension;
4-
import org.apache.poi.ss.usermodel.Workbook;
54
import tools.Converter;
6-
import tools.WorkbookUtility;
75

86
import java.io.File;
9-
import java.io.FileOutputStream;
107
import java.time.LocalDate;
118
import java.time.LocalDateTime;
129
import java.util.ArrayList;
@@ -30,12 +27,7 @@ public static void main(String[] args) {
3027
System.out.println("Start the conversion...");
3128
File report = Converter.objectsToExcel(employees, Employee.class, "./src/main/resources/", "employee", Extension.XLSX, true);
3229
System.out.println("First conversion completed...");
33-
34-
Workbook workbook = WorkbookUtility.open(report);
35-
Converter.objectsToExistingExcel(workbook, offices, Office.class, true);
36-
FileOutputStream fileOutputStream = new FileOutputStream(report);
37-
workbook.write(fileOutputStream);
38-
WorkbookUtility.close(workbook, fileOutputStream);
30+
Converter.objectsToExistingExcel(report, offices, Office.class, true);
3931
System.out.println("The file is ready. Path: " + report.getAbsolutePath());
4032
} catch (Exception e) {
4133
System.err.println("There was an error. Check the console");

src/main/java/tools/Converter.java

+95-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import exceptions.*;
1111
import org.apache.commons.beanutils.PropertyUtils;
1212
import org.apache.commons.io.FilenameUtils;
13+
import org.apache.logging.log4j.LogManager;
14+
import org.apache.logging.log4j.Logger;
1315
import org.apache.poi.ss.usermodel.*;
1416

1517
import java.io.*;
@@ -26,6 +28,8 @@
2628
*/
2729
public class Converter {
2830

31+
private static Logger logger = LogManager.getLogger(Converter.class);
32+
2933
/**
3034
* Convert a list of objects into an Excel file<p>
3135
* Note: The type of the elements of the {@code objects} list must coincide with the type of {@code clazz}<p>
@@ -264,6 +268,47 @@ public static File objectsToExcel(List<?> objects, Class<?> clazz, String path,
264268
return file;
265269
}
266270

271+
/**
272+
* This method allows you to convert objects into a Sheet of a File that already exists.<p>
273+
* By default, the header is added if not specified
274+
* @param file The {@code File} to update
275+
* @param objects The list of objects that will be converted into an Excel file
276+
* @param clazz The class of the list elements
277+
* @throws OpenWorkbookException If an error occurred while opening the workbook
278+
* @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
279+
* @throws IOException If an I/O error has occurred
280+
* @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
281+
* @since 0.2.1
282+
*/
283+
public static void objectsToExistingExcel(File file, List<?> objects, Class<?> clazz) throws OpenWorkbookException, ExtensionNotValidException, IOException, IllegalAccessException {
284+
objectsToExistingExcel(file, objects, clazz, true);
285+
}
286+
287+
/**
288+
* This method allows you to convert objects into a Sheet of a File that already exists.
289+
* @param file The {@code File} to update
290+
* @param objects The list of objects that will be converted into an Excel file
291+
* @param clazz The class of the list elements
292+
* @param writeHeader If {@code true} it will write the header to the first line
293+
* @throws OpenWorkbookException If an error occurred while opening the workbook
294+
* @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
295+
* @throws IOException If an I/O error has occurred
296+
* @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
297+
* @since 0.2.1
298+
*/
299+
public static void objectsToExistingExcel(File file, List<?> objects, Class<?> clazz, Boolean writeHeader) throws OpenWorkbookException, ExtensionNotValidException, IOException, IllegalAccessException {
300+
/* Open workbook */
301+
Workbook workbook = WorkbookUtility.open(file);
302+
objectsToExistingExcel(workbook, objects, clazz, writeHeader);
303+
304+
/* Write file */
305+
FileOutputStream fileOutputStream = new FileOutputStream(file);
306+
workbook.write(fileOutputStream);
307+
308+
/* Close file */
309+
WorkbookUtility.close(workbook, fileOutputStream);
310+
}
311+
267312
/**
268313
* This method allows you to convert objects into a Sheet of a Workbook that already exists.<p>
269314
* Note: This method does not call the "write" method of the workbook.<p>
@@ -557,6 +602,52 @@ public static File csvToExcel(File fileInput, String path, String filename, Exte
557602
return outputFile;
558603
}
559604

605+
/**
606+
* Convert the CSV file into a new sheet of an existing File.
607+
* @param fileOutput The {@code File} to update
608+
* @param fileInput The input CSV file that will be converted into an Excel file
609+
* @throws OpenWorkbookException If an error occurred while opening the workbook
610+
* @throws ExtensionNotValidException If the input file extension does not belong to a CSV file
611+
* @throws IOException If an I/O error has occurred
612+
* @throws CsvValidationException If the CSV file has invalid formatting
613+
* @since 0.2.1
614+
*/
615+
public static void csvToExistingExcel(File fileOutput, File fileInput) throws OpenWorkbookException, ExtensionNotValidException, IOException, CsvValidationException {
616+
/* Open workbook */
617+
Workbook workbook = WorkbookUtility.open(fileOutput);
618+
csvToExistingExcel(workbook, fileInput);
619+
620+
/* Write file */
621+
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
622+
workbook.write(fileOutputStream);
623+
624+
/* Close file */
625+
WorkbookUtility.close(workbook, fileOutputStream);
626+
}
627+
628+
/**
629+
* Writes the data present in the CSVReader to a new sheet of an existing File.
630+
* @param fileOutput The {@code File} to update
631+
* @param csvReader The {@code CSVReader} of the CSV input file
632+
* @throws OpenWorkbookException If an error occurred while opening the workbook
633+
* @throws ExtensionNotValidException If the input file extension does not belong to a CSV file
634+
* @throws IOException If an I/O error has occurred
635+
* @throws CsvValidationException If the CSV file has invalid formatting
636+
* @since 0.2.1
637+
*/
638+
public static void csvToExistingExcel(File fileOutput, CSVReader csvReader) throws OpenWorkbookException, ExtensionNotValidException, IOException, CsvValidationException {
639+
/* Open workbook */
640+
Workbook workbook = WorkbookUtility.open(fileOutput);
641+
csvToExistingExcel(workbook, csvReader);
642+
643+
/* Write file */
644+
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
645+
workbook.write(fileOutputStream);
646+
647+
/* Close file */
648+
WorkbookUtility.close(workbook, fileOutputStream, csvReader);
649+
}
650+
560651
/**
561652
* Convert the CSV file into a new sheet of an existing Workbook.<p>
562653
* Note: This method does not call the "write" method of the workbook.
@@ -661,11 +752,14 @@ private static Object convertCellValuesToObject(Class<?> clazz, Row row, Field[]
661752
PropertyUtils.setSimpleProperty(obj, headerName, cell.getLocalDateTimeCellValue());
662753
} else if (LocalDate.class.equals(field.getType())) {
663754
PropertyUtils.setSimpleProperty(obj, headerName, cell.getLocalDateTimeCellValue().toLocalDate());
755+
} else {
756+
logger.error("{} type is not supported. It was not possible to write '{}'", field.getType(), headerName);
664757
}
665758

666759
}
667760
case BOOLEAN -> PropertyUtils.setSimpleProperty(obj, headerName, cell.getBooleanCellValue());
668-
default -> PropertyUtils.setSimpleProperty(obj, headerName, cell.getStringCellValue());
761+
case STRING -> PropertyUtils.setSimpleProperty(obj, headerName, cell.getStringCellValue());
762+
default -> logger.error("Cell type not supported. It was not possible to write '{}'", headerName);
669763
}
670764
}
671765
return obj;

0 commit comments

Comments
 (0)