Skip to content

Commit dd06e1f

Browse files
committed
I added a method that converts a CSV file to Excel
1 parent 2f525e5 commit dd06e1f

File tree

11 files changed

+179
-72
lines changed

11 files changed

+179
-72
lines changed

src/main/java/enums/ExcelExtension.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main/java/enums/Extension.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package enums;
2+
3+
import exceptions.ExtensionNotValidException;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
7+
import java.util.Arrays;
8+
import java.util.Optional;
9+
10+
@Getter
11+
@AllArgsConstructor
12+
public enum Extension {
13+
14+
XLS("xls", "EXCEL"),
15+
XLSX("xlsx", "EXCEL"),
16+
CSV("csv", "CSV");
17+
18+
private final String ext;
19+
private final String type;
20+
21+
public static Extension getExcelExtension(String ext) throws ExtensionNotValidException {
22+
Optional<Extension> extensionOptional = Arrays.stream(Extension.values()).filter(e -> ext.equalsIgnoreCase(e.getExt()) && e.getType().equals("EXCEL")).findFirst();
23+
if (extensionOptional.isEmpty()) {
24+
throw new ExtensionNotValidException();
25+
}
26+
return extensionOptional.get();
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package samples.convertCsvFileToExcelFile;
2+
3+
import enums.Extension;
4+
import tools.implementations.ExcelConverterImpl;
5+
import tools.interfaces.ExcelConverter;
6+
7+
import java.io.File;
8+
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
13+
ExcelConverter excelConverter = new ExcelConverterImpl();
14+
File csvFile = new File("./src/main/resources/employee.csv");
15+
16+
try {
17+
System.out.println("Start the conversion...");
18+
File excelFile = excelConverter.csvToExcel(csvFile, "./src/main/resources/", "employee_2", Extension.XLSX);
19+
System.out.println("The file is ready. Path: " + excelFile.getAbsolutePath());
20+
} catch (Exception e) {
21+
System.err.println("There was an error. Check the console");
22+
throw new RuntimeException(e);
23+
}
24+
}
25+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package samples.convertObjectsToExcelFileSample;
22

3-
import enums.ExcelExtension;
3+
import enums.Extension;
44
import tools.interfaces.ExcelConverter;
55
import tools.implementations.ExcelConverterImpl;
66

@@ -22,7 +22,7 @@ public static void main(String[] args) {
2222

2323
try {
2424
System.out.println("Start the conversion...");
25-
File report = excelConverter.objectsToExcel(employees, Employee.class, "./src/main/resources/", "employee", ExcelExtension.XLSX, true);
25+
File report = excelConverter.objectsToExcel(employees, Employee.class, "./src/main/resources/", "employee", Extension.XLSX, true);
2626
System.out.println("The file is ready. Path: " + report.getAbsolutePath());
2727
} catch (Exception e) {
2828
System.err.println("There was an error. Check the console");

src/main/java/tools/implementations/ExcelConverterImpl.java

Lines changed: 88 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import annotations.ExcelBodyStyle;
44
import annotations.ExcelField;
55
import annotations.ExcelHeaderStyle;
6+
import com.opencsv.CSVReader;
67
import com.opencsv.CSVWriter;
7-
import enums.ExcelExtension;
8+
import com.opencsv.exceptions.CsvValidationException;
9+
import enums.Extension;
810
import exceptions.*;
911
import org.apache.commons.beanutils.PropertyUtils;
12+
import org.apache.commons.io.FilenameUtils;
1013
import org.apache.poi.ss.usermodel.*;
1114
import tools.interfaces.ExcelConverter;
1215
import 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

src/main/java/tools/implementations/ExcelSheetUtilsImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ExcelSheetUtilsImpl implements ExcelSheetUtils {
2222
public Integer countAll(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException {
2323
/* Check extension */
2424
ExcelUtils excelUtils = new ExcelUtilsImpl();
25-
String extension = excelUtils.checkExtension(file.getName());
25+
String extension = excelUtils.checkExcelExtension(file.getName());
2626

2727
/* Open file excel */
2828
ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl();
@@ -41,7 +41,7 @@ public Integer countAll(File file) throws ExtensionNotValidException, IOExceptio
4141
public List<String> getAllNames(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException {
4242
/* Check extension */
4343
ExcelUtils excelUtils = new ExcelUtilsImpl();
44-
String extension = excelUtils.checkExtension(file.getName());
44+
String extension = excelUtils.checkExcelExtension(file.getName());
4545

4646
/* Open file excel */
4747
ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl();
@@ -66,7 +66,7 @@ public List<String> getAllNames(File file) throws ExtensionNotValidException, IO
6666
public Integer getIndex(File file, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
6767
/* Check extension */
6868
ExcelUtils excelUtils = new ExcelUtilsImpl();
69-
String extension = excelUtils.checkExtension(file.getName());
69+
String extension = excelUtils.checkExcelExtension(file.getName());
7070

7171
/* Open file excel */
7272
ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl();
@@ -88,7 +88,7 @@ public Integer getIndex(File file, String sheetName) throws ExtensionNotValidExc
8888
public String getNameByIndex(File file, Integer position) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
8989
/* Check extension */
9090
ExcelUtils excelUtils = new ExcelUtilsImpl();
91-
String extension = excelUtils.checkExtension(file.getName());
91+
String extension = excelUtils.checkExcelExtension(file.getName());
9292

9393
/* Open file excel */
9494
ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl();
@@ -117,7 +117,7 @@ public Sheet create(File file) throws ExtensionNotValidException, IOException, O
117117
public Sheet create(File file, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException {
118118
/* Check extension */
119119
ExcelUtils excelUtils = new ExcelUtilsImpl();
120-
String extension = excelUtils.checkExtension(file.getName());
120+
String extension = excelUtils.checkExcelExtension(file.getName());
121121

122122
/* Open file excel */
123123
ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl();
@@ -147,7 +147,7 @@ public Sheet open(File file) throws ExtensionNotValidException, IOException, Ope
147147
public Sheet open(File file, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
148148
/* Check extension */
149149
ExcelUtils excelUtils = new ExcelUtilsImpl();
150-
String extension = excelUtils.checkExtension(file.getName());
150+
String extension = excelUtils.checkExcelExtension(file.getName());
151151

152152
/* Open file excel */
153153
ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl();
@@ -165,7 +165,7 @@ public Sheet open(File file, String sheetName) throws ExtensionNotValidException
165165
public Sheet open(File file, Integer position) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
166166
/* Check extension */
167167
ExcelUtils excelUtils = new ExcelUtilsImpl();
168-
String extension = excelUtils.checkExtension(file.getName());
168+
String extension = excelUtils.checkExcelExtension(file.getName());
169169

170170
/* Open file excel */
171171
ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl();

0 commit comments

Comments
 (0)