33import annotations .ExcelBodyStyle ;
44import annotations .ExcelField ;
55import annotations .ExcelHeaderStyle ;
6+ import com .opencsv .CSVReader ;
67import com .opencsv .CSVWriter ;
7- import enums .ExcelExtension ;
8+ import com .opencsv .exceptions .CsvValidationException ;
9+ import enums .Extension ;
810import exceptions .*;
911import org .apache .commons .beanutils .PropertyUtils ;
12+ import org .apache .commons .io .FilenameUtils ;
1013import org .apache .poi .ss .usermodel .*;
1114import tools .interfaces .ExcelConverter ;
1215import tools .interfaces .ExcelSheetUtils ;
@@ -24,61 +27,61 @@ public class ExcelConverterImpl implements ExcelConverter {
2427
2528 @ Override
2629 public File objectsToExcel (List <?> objects , Class <?> clazz ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
27- return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), clazz .getSimpleName (), ExcelExtension .XLSX , true );
30+ return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), clazz .getSimpleName (), Extension .XLSX , true );
2831 }
2932
3033 @ Override
3134 public File objectsToExcel (List <?> objects , Class <?> clazz , String filename ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
32- return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), filename , ExcelExtension .XLSX , true );
35+ return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), filename , Extension .XLSX , true );
3336 }
3437
3538 @ Override
3639 public File objectsToExcel (List <?> objects , Class <?> clazz , String path , String filename ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
37- return objectsToExcel (objects , clazz , path , filename , ExcelExtension .XLSX , true );
40+ return objectsToExcel (objects , clazz , path , filename , Extension .XLSX , true );
3841 }
3942
4043 @ Override
4144 public File objectsToExcel (List <?> objects , Class <?> clazz , String path , String filename , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
42- return objectsToExcel (objects , clazz , path , filename , ExcelExtension .XLSX , writeHeader );
45+ return objectsToExcel (objects , clazz , path , filename , Extension .XLSX , writeHeader );
4346 }
4447
4548 @ Override
4649 public File objectsToExcel (List <?> objects , Class <?> clazz , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
47- return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), clazz .getSimpleName (), ExcelExtension .XLSX , writeHeader );
50+ return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), clazz .getSimpleName (), Extension .XLSX , writeHeader );
4851 }
4952
5053 @ Override
5154 public File objectsToExcel (List <?> objects , Class <?> clazz , String filename , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
52- return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), filename , ExcelExtension .XLSX , writeHeader );
55+ return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), filename , Extension .XLSX , writeHeader );
5356 }
5457
5558 @ Override
56- public File objectsToExcel (List <?> objects , Class <?> clazz , String path , String filename , ExcelExtension extension ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
59+ public File objectsToExcel (List <?> objects , Class <?> clazz , String path , String filename , Extension extension ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
5760 return objectsToExcel (objects , clazz , path , filename , extension , true );
5861 }
5962
6063 @ Override
61- public File objectsToExcel (List <?> objects , Class <?> clazz , ExcelExtension extension ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
64+ public File objectsToExcel (List <?> objects , Class <?> clazz , Extension extension ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
6265 return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), clazz .getSimpleName (), extension , true );
6366 }
6467
6568 @ Override
66- public File objectsToExcel (List <?> objects , Class <?> clazz , ExcelExtension extension , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
69+ public File objectsToExcel (List <?> objects , Class <?> clazz , Extension extension , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
6770 return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), clazz .getSimpleName (), extension , writeHeader );
6871 }
6972
7073 @ Override
71- public File objectsToExcel (List <?> objects , Class <?> clazz , String filename , ExcelExtension extension ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
74+ public File objectsToExcel (List <?> objects , Class <?> clazz , String filename , Extension extension ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
7275 return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), filename , extension , true );
7376 }
7477
7578 @ Override
76- public File objectsToExcel (List <?> objects , Class <?> clazz , String filename , ExcelExtension extension , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
79+ public File objectsToExcel (List <?> objects , Class <?> clazz , String filename , Extension extension , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
7780 return objectsToExcel (objects , clazz , System .getProperty ("java.io.tmpdir" ), filename , extension , writeHeader );
7881 }
7982
8083 @ Override
81- public File objectsToExcel (List <?> objects , Class <?> clazz , String path , String filename , ExcelExtension extension , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
84+ public File objectsToExcel (List <?> objects , Class <?> clazz , String path , String filename , Extension extension , Boolean writeHeader ) throws IllegalAccessException , IOException , FileAlreadyExistsException {
8285 /* Open file */
8386 String pathname = this .getPathname (path , filename , extension );
8487 File file = new File (pathname );
@@ -128,7 +131,7 @@ public List<?> excelToObjects(File file, Class<?> clazz) throws ExtensionNotVali
128131 public List <?> excelToObjects (File file , Class <?> clazz , String sheetName ) throws ExtensionNotValidException , IOException , OpenWorkbookException , InvocationTargetException , IllegalAccessException , NoSuchMethodException , InstantiationException , SheetNotFoundException , HeaderNotPresentException {
129132 /* Check extension */
130133 ExcelUtils excelUtils = new ExcelUtilsImpl ();
131- String extension = excelUtils .checkExtension (file .getName ());
134+ String extension = excelUtils .checkExcelExtension (file .getName ());
132135
133136 /* Open file excel */
134137 ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl ();
@@ -180,7 +183,7 @@ public File excelToCsv(File fileInput, String path, String filename) throws File
180183 public File excelToCsv (File fileInput , String path , String filename , String sheetName ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException , FileAlreadyExistsException {
181184 /* Check extension */
182185 ExcelUtils excelUtils = new ExcelUtilsImpl ();
183- String extension = excelUtils .checkExtension (fileInput .getName ());
186+ String extension = excelUtils .checkExcelExtension (fileInput .getName ());
184187
185188 /* Open file excel */
186189 ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl ();
@@ -192,7 +195,7 @@ public File excelToCsv(File fileInput, String path, String filename, String shee
192195 : excelSheetUtils .open (workbook , sheetName );
193196
194197 /* Create output file */
195- String pathname = this .getPathname (path , filename , "csv" );
198+ String pathname = this .getPathname (path , filename , Extension . CSV . getExt () );
196199 File csvFile = new File (pathname );
197200
198201 if (csvFile .exists ()) {
@@ -218,6 +221,74 @@ public File excelToCsv(File fileInput, String path, String filename, String shee
218221 return csvFile ;
219222 }
220223
224+ @ Override
225+ public File csvToExcel (File fileInput ) throws FileAlreadyExistsException , CsvValidationException , ExtensionNotValidException , IOException {
226+ return csvToExcel (fileInput , System .getProperty ("java.io.tmpdir" ), fileInput .getName ().split ("\\ ." )[0 ].trim (), Extension .XLSX );
227+ }
228+
229+ @ Override
230+ public File csvToExcel (File fileInput , String filename ) throws FileAlreadyExistsException , CsvValidationException , ExtensionNotValidException , IOException {
231+ return csvToExcel (fileInput , System .getProperty ("java.io.tmpdir" ), filename , Extension .XLSX );
232+ }
233+
234+ @ Override
235+ public File csvToExcel (File fileInput , String path , String filename ) throws FileAlreadyExistsException , CsvValidationException , ExtensionNotValidException , IOException {
236+ return csvToExcel (fileInput , path , filename , Extension .XLSX );
237+ }
238+
239+ @ Override
240+ public File csvToExcel (File fileInput , String path , String filename , Extension extension ) throws IOException , ExtensionNotValidException , CsvValidationException , FileAlreadyExistsException {
241+
242+ /* Check exension */
243+ String csvExt = FilenameUtils .getExtension (fileInput .getName ());
244+ this .isValidCsvExtension (csvExt );
245+
246+ /* Open CSV file */
247+ FileReader fileReader = new FileReader (fileInput );
248+ CSVReader csvReader = new CSVReader (fileReader );
249+
250+ /* Create output file */
251+ String pathname = this .getPathname (path , filename , extension );
252+ File outputFile = new File (pathname );
253+
254+ if (outputFile .exists ()) {
255+ throw new FileAlreadyExistsException ("There is already a file with this pathname: " + outputFile .getAbsolutePath ());
256+ }
257+
258+ /* Create workbook and sheet */
259+ ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl ();
260+ Workbook workbook = excelWorkbookUtils .create (extension );
261+ ExcelSheetUtils excelSheetUtils = new ExcelSheetUtilsImpl ();
262+ Sheet sheet = excelSheetUtils .create (workbook );
263+
264+ /* Read CSV file */
265+ String [] values ;
266+ int cRow = 0 ;
267+ while ((values = csvReader .readNext ()) != null ) {
268+ Row row = sheet .createRow (cRow );
269+ for (int j = 0 ; j < values .length ; j ++) {
270+ Cell cell = row .createCell (j );
271+ cell .setCellValue (values [j ]);
272+ sheet .autoSizeColumn (j );
273+ }
274+ cRow ++;
275+ }
276+
277+ /* Write file */
278+ FileOutputStream fileOutputStream = new FileOutputStream (outputFile );
279+ workbook .write (fileOutputStream );
280+
281+ /* Close file */
282+ excelWorkbookUtils .close (workbook , fileOutputStream );
283+
284+ return outputFile ;
285+ }
286+
287+ private void isValidCsvExtension (String extension ) throws ExtensionNotValidException {
288+ if (!extension .equalsIgnoreCase (Extension .CSV .getExt ()))
289+ throw new ExtensionNotValidException ("Pass a file with the CSV extension" );
290+ }
291+
221292 private Map <Integer , String > getHeaderNames (Sheet sheet , Field [] fields ) throws HeaderNotPresentException {
222293 Map <String , String > fieldNames = new HashMap <>();
223294 for (Field field : fields ) {
@@ -380,7 +451,7 @@ private void setAutoSizeColumn(Sheet sheet, Field[] fields, Class<?> clazz) {
380451 }
381452 }
382453
383- private String getPathname (String path , String filename , ExcelExtension extension ) {
454+ private String getPathname (String path , String filename , Extension extension ) {
384455 return getPathname (path , filename , extension .getExt ());
385456 }
386457
0 commit comments