|
10 | 10 | import exceptions.*;
|
11 | 11 | import org.apache.commons.beanutils.PropertyUtils;
|
12 | 12 | import org.apache.commons.io.FilenameUtils;
|
| 13 | +import org.apache.logging.log4j.LogManager; |
| 14 | +import org.apache.logging.log4j.Logger; |
13 | 15 | import org.apache.poi.ss.usermodel.*;
|
14 | 16 |
|
15 | 17 | import java.io.*;
|
|
26 | 28 | */
|
27 | 29 | public class Converter {
|
28 | 30 |
|
| 31 | + private static Logger logger = LogManager.getLogger(Converter.class); |
| 32 | + |
29 | 33 | /**
|
30 | 34 | * Convert a list of objects into an Excel file<p>
|
31 | 35 | * 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,
|
264 | 268 | return file;
|
265 | 269 | }
|
266 | 270 |
|
| 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 | + |
267 | 312 | /**
|
268 | 313 | * This method allows you to convert objects into a Sheet of a Workbook that already exists.<p>
|
269 | 314 | * 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
|
557 | 602 | return outputFile;
|
558 | 603 | }
|
559 | 604 |
|
| 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 | + |
560 | 651 | /**
|
561 | 652 | * Convert the CSV file into a new sheet of an existing Workbook.<p>
|
562 | 653 | * 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[]
|
661 | 752 | PropertyUtils.setSimpleProperty(obj, headerName, cell.getLocalDateTimeCellValue());
|
662 | 753 | } else if (LocalDate.class.equals(field.getType())) {
|
663 | 754 | 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); |
664 | 757 | }
|
665 | 758 |
|
666 | 759 | }
|
667 | 760 | 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); |
669 | 763 | }
|
670 | 764 | }
|
671 | 765 | return obj;
|
|
0 commit comments