|
3 | 3 | import annotations.ExcelBodyStyle; |
4 | 4 | import annotations.ExcelField; |
5 | 5 | import annotations.ExcelHeaderStyle; |
| 6 | +import com.opencsv.CSVWriter; |
6 | 7 | import enums.ExcelExtension; |
7 | 8 | import exceptions.*; |
8 | 9 | import org.apache.commons.beanutils.PropertyUtils; |
@@ -160,6 +161,63 @@ public List<?> excelToObjects(File file, Class<?> clazz, String sheetName) throw |
160 | 161 | return resultList; |
161 | 162 | } |
162 | 163 |
|
| 164 | + @Override |
| 165 | + public File excelToCsv(File fileInput) throws FileAlreadyExistsException, OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException { |
| 166 | + return excelToCsv(fileInput, System.getProperty("java.io.tmpdir"), fileInput.getName().split("\\.")[0].trim(), null); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public File excelToCsv(File fileInput, String sheetName) throws FileAlreadyExistsException, OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException { |
| 171 | + return excelToCsv(fileInput, System.getProperty("java.io.tmpdir"), fileInput.getName().split("\\.")[0].trim(), sheetName); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public File excelToCsv(File fileInput, String path, String filename) throws FileAlreadyExistsException, OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException { |
| 176 | + return excelToCsv(fileInput, path, filename, null); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public File excelToCsv(File fileInput, String path, String filename, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException, FileAlreadyExistsException { |
| 181 | + /* Check extension */ |
| 182 | + ExcelUtils excelUtils = new ExcelUtilsImpl(); |
| 183 | + String extension = excelUtils.checkExtension(fileInput.getName()); |
| 184 | + |
| 185 | + /* Open file excel */ |
| 186 | + ExcelWorkbookUtils excelWorkbookUtils = new ExcelWorkbookUtilsImpl(); |
| 187 | + FileInputStream fileInputStream = new FileInputStream(fileInput); |
| 188 | + Workbook workbook = excelWorkbookUtils.open(fileInputStream, extension); |
| 189 | + ExcelSheetUtils excelSheetUtils = new ExcelSheetUtilsImpl(); |
| 190 | + Sheet sheet = (sheetName == null || sheetName.isEmpty()) |
| 191 | + ? excelSheetUtils.open(workbook) |
| 192 | + : excelSheetUtils.open(workbook, sheetName); |
| 193 | + |
| 194 | + /* Create output file */ |
| 195 | + String pathname = this.getPathname(path, filename, "csv"); |
| 196 | + File csvFile = new File(pathname); |
| 197 | + |
| 198 | + if (csvFile.exists()) { |
| 199 | + throw new FileAlreadyExistsException("There is already a file with this pathname: " + csvFile.getAbsolutePath()); |
| 200 | + } |
| 201 | + |
| 202 | + /* Write output file */ |
| 203 | + FileWriter fileWriter = new FileWriter(csvFile); |
| 204 | + CSVWriter csvWriter = new CSVWriter(fileWriter); |
| 205 | + |
| 206 | + DataFormatter formatter = new DataFormatter(true); |
| 207 | + for (Row row : sheet) { |
| 208 | + List<String> data = new LinkedList<>(); |
| 209 | + for (int i = 0; i < row.getLastCellNum(); i++) { |
| 210 | + data.add(formatter.formatCellValue(row.getCell(i))); |
| 211 | + } |
| 212 | + csvWriter.writeNext(data.toArray(data.toArray(new String[0]))); |
| 213 | + } |
| 214 | + |
| 215 | + /* Close file */ |
| 216 | + excelWorkbookUtils.close(workbook, fileInputStream, csvWriter); |
| 217 | + |
| 218 | + return csvFile; |
| 219 | + } |
| 220 | + |
163 | 221 | private Map<Integer, String> getHeaderNames(Sheet sheet, Field[] fields) throws HeaderNotPresentException { |
164 | 222 | Map<String, String> fieldNames = new HashMap<>(); |
165 | 223 | for (Field field : fields) { |
@@ -323,11 +381,15 @@ private void setAutoSizeColumn(Sheet sheet, Field[] fields, Class<?> clazz) { |
323 | 381 | } |
324 | 382 |
|
325 | 383 | private String getPathname(String path, String filename, ExcelExtension extension) { |
| 384 | + return getPathname(path, filename, extension.getExt()); |
| 385 | + } |
| 386 | + |
| 387 | + private String getPathname(String path, String filename, String extension) { |
326 | 388 | path = path.replaceAll("\\\\", "/"); |
327 | 389 | if (path.charAt(path.length() - 1) != '/') { |
328 | 390 | path += '/'; |
329 | 391 | } |
330 | 392 |
|
331 | | - return path + filename + '.' + extension.getExt(); |
| 393 | + return path + filename + '.' + extension; |
332 | 394 | } |
333 | 395 | } |
0 commit comments