88import com .opencsv .exceptions .CsvValidationException ;
99import io .github .mbenincasa .javaexcelutils .enums .Extension ;
1010import io .github .mbenincasa .javaexcelutils .exceptions .*;
11+ import io .github .mbenincasa .javaexcelutils .model .converter .ExcelToObject ;
1112import io .github .mbenincasa .javaexcelutils .model .converter .ObjectToExcel ;
1213import io .github .mbenincasa .javaexcelutils .model .excel .ExcelCell ;
1314import io .github .mbenincasa .javaexcelutils .model .excel .ExcelRow ;
2627import java .time .LocalDateTime ;
2728import java .util .*;
2829import java .util .concurrent .atomic .AtomicInteger ;
30+ import java .util .stream .Stream ;
2931
3032/**
3133 * {@code Converter} is the static class with implementations of conversion methods
@@ -373,6 +375,7 @@ public static OutputStream objectsToExcelStream(List<ObjectToExcel<?>> objectToE
373375 /**
374376 * This method allows you to convert objects into a Sheet of a File that already exists.<p>
375377 * By default, the header is added if not specified
378+ * @deprecated since version 0.4.0
376379 * @param file The {@code File} to update
377380 * @param objects The list of objects that will be converted into an Excel file
378381 * @param clazz The class of the list elements
@@ -382,12 +385,14 @@ public static OutputStream objectsToExcelStream(List<ObjectToExcel<?>> objectToE
382385 * @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
383386 * @since 0.2.1
384387 */
388+ @ Deprecated
385389 public static void objectsToExistingExcel (File file , List <?> objects , Class <?> clazz ) throws OpenWorkbookException , ExtensionNotValidException , IOException , IllegalAccessException {
386390 objectsToExistingExcel (file , objects , clazz , true );
387391 }
388392
389393 /**
390394 * This method allows you to convert objects into a Sheet of a File that already exists.
395+ * @deprecated since version 0.4.0
391396 * @param file The {@code File} to update
392397 * @param objects The list of objects that will be converted into an Excel file
393398 * @param clazz The class of the list elements
@@ -398,6 +403,7 @@ public static void objectsToExistingExcel(File file, List<?> objects, Class<?> c
398403 * @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
399404 * @since 0.2.1
400405 */
406+ @ Deprecated
401407 public static void objectsToExistingExcel (File file , List <?> objects , Class <?> clazz , Boolean writeHeader ) throws OpenWorkbookException , ExtensionNotValidException , IOException , IllegalAccessException {
402408 /* Open workbook */
403409 ExcelWorkbook excelWorkbook = ExcelWorkbook .open (file );
@@ -416,24 +422,28 @@ public static void objectsToExistingExcel(File file, List<?> objects, Class<?> c
416422 * This method allows you to convert objects into a Sheet of a Workbook that already exists.<p>
417423 * Note: This method does not call the "write" method of the workbook.<p>
418424 * By default, the header is added if not specified
425+ * @deprecated since version 0.4.0
419426 * @param workbook The {@code Workbook} to update
420427 * @param objects The list of objects that will be converted into an Excel file
421428 * @param clazz The class of the list elements
422429 * @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
423430 */
431+ @ Deprecated
424432 public static void objectsToExistingExcel (Workbook workbook , List <?> objects , Class <?> clazz ) throws IllegalAccessException {
425433 objectsToExistingExcel (workbook , objects , clazz , true );
426434 }
427435
428436 /**
429437 * This method allows you to convert objects into a Sheet of a Workbook that already exists.<p>
430438 * Note: This method does not call the "write" method of the workbook.
439+ * @deprecated since version 0.4.0
431440 * @param workbook The {@code Workbook} to update
432441 * @param objects The list of objects that will be converted into an Excel file
433442 * @param clazz The class of the list elements
434443 * @param writeHeader If {@code true} it will write the header to the first line
435444 * @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
436445 */
446+ @ Deprecated
437447 public static void objectsToExistingExcel (Workbook workbook , List <?> objects , Class <?> clazz , Boolean writeHeader ) throws IllegalAccessException {
438448 /* Create sheet */
439449 ExcelWorkbook excelWorkbook = new ExcelWorkbook (workbook );
@@ -460,6 +470,7 @@ public static void objectsToExistingExcel(Workbook workbook, List<?> objects, Cl
460470 * Convert an Excel file into a list of objects<p>
461471 * Note: The type of the elements of the return objects must coincide with the type of {@code clazz}<p>
462472 * By default, the first sheet is chosen
473+ * @deprecated since version 0.4.0
463474 * @param file The input Excel file that will be converted into a list of objects
464475 * @param clazz The class of the list elements
465476 * @return A list of objects that contains as many objects as there are lines in the input file
@@ -473,13 +484,15 @@ public static void objectsToExistingExcel(Workbook workbook, List<?> objects, Cl
473484 * @throws SheetNotFoundException If the sheet to open is not found
474485 * @throws HeaderNotPresentException If the first row is empty and does not contain the header
475486 */
487+ @ Deprecated
476488 public static List <?> excelToObjects (File file , Class <?> clazz ) throws ExtensionNotValidException , IOException , OpenWorkbookException , InstantiationException , IllegalAccessException , InvocationTargetException , NoSuchMethodException , SheetNotFoundException , HeaderNotPresentException {
477489 return excelToObjects (file , clazz , null );
478490 }
479491
480492 /**
481493 * Convert an Excel file into a list of objects<p>
482494 * Note: The type of the elements of the return objects must coincide with the type of {@code clazz}
495+ * @deprecated since version 0.4.0
483496 * @param file The input Excel file that will be converted into a list of objects
484497 * @param clazz The class of the list elements
485498 * @param sheetName The name of the sheet to open
@@ -494,6 +507,7 @@ public static List<?> excelToObjects(File file, Class<?> clazz) throws Extension
494507 * @throws SheetNotFoundException If the sheet to open is not found
495508 * @throws HeaderNotPresentException If the first row is empty and does not contain the header
496509 */
510+ @ Deprecated
497511 public static List <?> excelToObjects (File file , Class <?> clazz , String sheetName ) throws ExtensionNotValidException , IOException , OpenWorkbookException , InvocationTargetException , IllegalAccessException , NoSuchMethodException , InstantiationException , SheetNotFoundException , HeaderNotPresentException {
498512 /* Open file excel */
499513 ExcelWorkbook excelWorkbook = ExcelWorkbook .open (file );
@@ -523,6 +537,71 @@ public static List<?> excelToObjects(File file, Class<?> clazz, String sheetName
523537 return resultList ;
524538 }
525539
540+ public static Map <String , Stream <?>> excelByteToObjects (byte [] bytes , List <ExcelToObject <?>> excelToObjects ) throws OpenWorkbookException , SheetNotFoundException , ReadValueException , HeaderNotPresentException , InvocationTargetException , NoSuchMethodException , InstantiationException , IllegalAccessException {
541+ ByteArrayInputStream byteStream = new ByteArrayInputStream (bytes );
542+ return excelStreamToObjects (byteStream , excelToObjects );
543+ }
544+
545+ public static Map <String , Stream <?>> excelFileToObjects (File file , List <ExcelToObject <?>> excelToObjects ) throws IOException , OpenWorkbookException , SheetNotFoundException , ReadValueException , HeaderNotPresentException , InvocationTargetException , NoSuchMethodException , InstantiationException , IllegalAccessException {
546+ FileInputStream fileInputStream = new FileInputStream (file );
547+ Map <String , Stream <?>> map = excelStreamToObjects (fileInputStream , excelToObjects );
548+ fileInputStream .close ();
549+ return map ;
550+ }
551+
552+ public static Map <String , Stream <?>> excelStreamToObjects (InputStream inputStream , List <ExcelToObject <?>> excelToObjects ) throws OpenWorkbookException , SheetNotFoundException , HeaderNotPresentException , NoSuchMethodException , InvocationTargetException , InstantiationException , IllegalAccessException , ReadValueException {
553+ /* Open Workbook */
554+ ExcelWorkbook excelWorkbook = new ExcelWorkbook (inputStream );
555+ Map <String , Stream <?>> map = new HashMap <>();
556+
557+ /* Iterate all the sheets to convert */
558+ for (ExcelToObject <?> excelToObject : excelToObjects ) {
559+ Class <?> clazz = excelToObject .getClazz ();
560+ ExcelSheet excelSheet = excelWorkbook .getSheet (excelToObject .getSheetName ());
561+
562+ /* Retrieving header names */
563+ Field [] fields = clazz .getDeclaredFields ();
564+ setFieldsAccessible (fields );
565+ Map <Integer , String > headerMap = getHeaderNames (excelSheet , fields );
566+
567+ Stream .Builder <Object > streamBuilder = Stream .builder ();
568+
569+ /* Iterate all rows */
570+ for (ExcelRow excelRow : excelSheet .getRows ()) {
571+ if (excelRow .getRow () == null || excelRow .getIndex () == 0 ) {
572+ continue ;
573+ }
574+
575+ Object obj = clazz .getDeclaredConstructor ().newInstance ();
576+ /* Iterate all cells */
577+ for (ExcelCell excelCell : excelRow .getCells ()) {
578+ if (excelCell .getCell () == null ) {
579+ continue ;
580+ }
581+
582+ String headerName = headerMap .get (excelCell .getIndex ());
583+ if (headerName == null || headerMap .isEmpty ()) {
584+ continue ;
585+ }
586+
587+ /* Read the value in the cell */
588+ Optional <Field > fieldOptional = Arrays .stream (fields ).filter (f -> f .getName ().equalsIgnoreCase (headerName )).findFirst ();
589+ if (fieldOptional .isEmpty ()) {
590+ throw new RuntimeException ();
591+ }
592+ Field field = fieldOptional .get ();
593+ PropertyUtils .setSimpleProperty (obj , headerName , excelCell .readValue (field .getType ()));
594+ }
595+ /* Adds the object to the Stream after it has finished cycling through all cells */
596+ streamBuilder .add (obj );
597+ }
598+ /* inserts the Stream of all Sheet objects into the Map */
599+ map .put (excelSheet .getName (), streamBuilder .build ());
600+ }
601+
602+ return map ;
603+ }
604+
526605 /**
527606 * Convert an Excel file into a CSV file<p>
528607 * The default path is that of the temporary folder. By default, the first sheet is chosen and the filename will be the same as the input file if not specified
@@ -831,6 +910,27 @@ private static Map<Integer, String> getHeaderNames(Sheet sheet, Field[] fields)
831910 return headerMap ;
832911 }
833912
913+ private static Map <Integer , String > getHeaderNames (ExcelSheet excelSheet , Field [] fields ) throws HeaderNotPresentException {
914+ Map <String , String > fieldNames = new HashMap <>();
915+ for (Field field : fields ) {
916+ ExcelField excelField = field .getAnnotation (ExcelField .class );
917+ fieldNames .put (excelField == null ? field .getName () : excelField .name (), field .getName ());
918+ }
919+
920+ Row headerRow = excelSheet .getSheet ().getRow (0 );
921+ if (headerRow == null )
922+ throw new HeaderNotPresentException ("There is no header in the first row of the sheet." );
923+
924+ Map <Integer , String > headerMap = new TreeMap <>();
925+ for (Cell cell : headerRow ) {
926+ if (fieldNames .containsKey (cell .getStringCellValue ())) {
927+ headerMap .put (cell .getColumnIndex (), fieldNames .get (cell .getStringCellValue ()));
928+ }
929+ }
930+
931+ return headerMap ;
932+ }
933+
834934 private static Object convertCellValuesToObject (Class <?> clazz , Row row , Field [] fields , Map <Integer , String > headerMap ) throws InvocationTargetException , IllegalAccessException , NoSuchMethodException , InstantiationException {
835935 Object obj = clazz .getDeclaredConstructor ().newInstance ();
836936 for (Cell cell : row ) {
0 commit comments