@@ -252,6 +252,42 @@ public static File objectsToExcel(List<?> objects, Class<?> clazz, String path,
252
252
253
253
/* Create workbook and sheet */
254
254
Workbook workbook = WorkbookUtility .create (extension );
255
+ objectsToExistingExcel (workbook , objects , clazz , writeHeader );
256
+
257
+ /* Write file */
258
+ FileOutputStream fileOutputStream = new FileOutputStream (file );
259
+ workbook .write (fileOutputStream );
260
+
261
+ /* Close file */
262
+ WorkbookUtility .close (workbook , fileOutputStream );
263
+
264
+ return file ;
265
+ }
266
+
267
+ /**
268
+ * This method allows you to convert objects into a Sheet of a Workbook that already exists.<p>
269
+ * Note: This method does not call the "write" method of the workbook.<p>
270
+ * By default, the header is added if not specified
271
+ * @param workbook The {@code Workbook} to update
272
+ * @param objects The list of objects that will be converted into an Excel file
273
+ * @param clazz The class of the list elements
274
+ * @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
275
+ */
276
+ public static void objectsToExistingExcel (Workbook workbook , List <?> objects , Class <?> clazz ) throws IllegalAccessException {
277
+ objectsToExistingExcel (workbook , objects , clazz , true );
278
+ }
279
+
280
+ /**
281
+ * This method allows you to convert objects into a Sheet of a Workbook that already exists.<p>
282
+ * Note: This method does not call the "write" method of the workbook.
283
+ * @param workbook The {@code Workbook} to update
284
+ * @param objects The list of objects that will be converted into an Excel file
285
+ * @param clazz The class of the list elements
286
+ * @param writeHeader If {@code true} it will write the header to the first line
287
+ * @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
288
+ */
289
+ public static void objectsToExistingExcel (Workbook workbook , List <?> objects , Class <?> clazz , Boolean writeHeader ) throws IllegalAccessException {
290
+ /* Create sheet */
255
291
Sheet sheet = SheetUtility .create (workbook , clazz .getSimpleName ());
256
292
257
293
Field [] fields = clazz .getDeclaredFields ();
@@ -269,15 +305,6 @@ public static File objectsToExcel(List<?> objects, Class<?> clazz, String path,
269
305
CellStyle bodyCellStyle = createBodyStyle (workbook , clazz );
270
306
writeExcelBody (workbook , sheet , fields , object , cRow ++, bodyCellStyle , clazz );
271
307
}
272
-
273
- /* Write file */
274
- FileOutputStream fileOutputStream = new FileOutputStream (file );
275
- workbook .write (fileOutputStream );
276
-
277
- /* Close file */
278
- WorkbookUtility .close (workbook , fileOutputStream );
279
-
280
- return file ;
281
308
}
282
309
283
310
/**
@@ -518,6 +545,50 @@ public static File csvToExcel(File fileInput, String path, String filename, Exte
518
545
519
546
/* Create workbook and sheet */
520
547
Workbook workbook = WorkbookUtility .create (extension );
548
+ csvToExistingExcel (workbook , csvReader );
549
+
550
+ /* Write file */
551
+ FileOutputStream fileOutputStream = new FileOutputStream (outputFile );
552
+ workbook .write (fileOutputStream );
553
+
554
+ /* Close file */
555
+ WorkbookUtility .close (workbook , fileOutputStream , csvReader );
556
+
557
+ return outputFile ;
558
+ }
559
+
560
+ /**
561
+ * Convert the CSV file into a new sheet of an existing Workbook.<p>
562
+ * Note: This method does not call the "write" method of the workbook.
563
+ * @param workbook The {@code Workbook} to update
564
+ * @param fileInput The input CSV file that will be converted into an Excel file
565
+ * @throws IOException If an I/O error has occurred
566
+ * @throws CsvValidationException If the CSV file has invalid formatting
567
+ * @throws ExtensionNotValidException If the input file extension does not belong to a CSV file
568
+ */
569
+ public static void csvToExistingExcel (Workbook workbook , File fileInput ) throws IOException , CsvValidationException , ExtensionNotValidException {
570
+ /* Check exension */
571
+ String csvExt = FilenameUtils .getExtension (fileInput .getName ());
572
+ isValidCsvExtension (csvExt );
573
+
574
+ /* Open CSV file */
575
+ FileReader fileReader = new FileReader (fileInput );
576
+ CSVReader csvReader = new CSVReader (fileReader );
577
+ csvToExistingExcel (workbook , csvReader );
578
+
579
+ /* Close CSV reader */
580
+ csvReader .close ();
581
+ }
582
+
583
+ /**
584
+ * Writes the data present in the CSVReader to a new sheet of an existing Workbook.<p>
585
+ * Note: This method does not call the "write" method of the workbook.
586
+ * @param workbook The {@code Workbook} to update
587
+ * @param csvReader The {@code CSVReader} of the CSV input file
588
+ * @throws CsvValidationException If the CSV file has invalid formatting
589
+ * @throws IOException If an I/O error has occurred
590
+ */
591
+ public static void csvToExistingExcel (Workbook workbook , CSVReader csvReader ) throws CsvValidationException , IOException {
521
592
Sheet sheet = SheetUtility .create (workbook );
522
593
523
594
/* Read CSV file */
@@ -532,15 +603,6 @@ public static File csvToExcel(File fileInput, String path, String filename, Exte
532
603
}
533
604
cRow ++;
534
605
}
535
-
536
- /* Write file */
537
- FileOutputStream fileOutputStream = new FileOutputStream (outputFile );
538
- workbook .write (fileOutputStream );
539
-
540
- /* Close file */
541
- WorkbookUtility .close (workbook , fileOutputStream , csvReader );
542
-
543
- return outputFile ;
544
606
}
545
607
546
608
private static void isValidCsvExtension (String extension ) throws ExtensionNotValidException {
0 commit comments