Skip to content

Commit e12c5d5

Browse files
committed
Added new methods to convert CSV to Excel
1 parent 14f14ce commit e12c5d5

File tree

3 files changed

+134
-5
lines changed

3 files changed

+134
-5
lines changed

src/main/java/io/github/mbenincasa/javaexcelutils/samples/convertCsvFileToExcelFile/Main.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ public class Main {
99

1010
public static void main(String[] args) {
1111

12-
File csvFile = new File("./src/main/resources/employee.csv");
13-
File csvFile2 = new File("./src/main/resources/employee_2.csv");
12+
File csvFile = new File("./src/main/resources/Employee.csv");
1413

1514
try {
1615
System.out.println("Start the conversion...");
17-
File excelFile = Converter.csvToExcel(csvFile, "./src/main/resources/", "employee_2", Extension.XLSX);
18-
System.out.println("First conversion completed...");
19-
Converter.csvToExistingExcel(excelFile, csvFile2);
16+
File excelFile = Converter.csvToExcelFile(csvFile, "Employee", "./src/main/resources/employee_2", Extension.XLSX);
17+
System.out.println("... completed");
2018
System.out.println("The file is ready. Path: " + excelFile.getAbsolutePath());
2119
} catch (Exception e) {
2220
System.err.println("There was an error. Check the console");

src/main/java/io/github/mbenincasa/javaexcelutils/tools/Converter.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,8 @@ public static Map<String, OutputStream> excelToCsvStream(InputStream excelStream
752752
/* Iterate all the Sheets */
753753
for (ExcelSheet excelSheet : excelSheets) {
754754
OutputStream outputStream = new ByteArrayOutputStream();
755+
756+
/* Open CSV Writer */
755757
Writer writer = new OutputStreamWriter(outputStream);
756758
CSVWriter csvWriter = new CSVWriter(writer);
757759

@@ -776,20 +778,23 @@ public static Map<String, OutputStream> excelToCsvStream(InputStream excelStream
776778
/**
777779
* Convert a CSV file into an Excel file<p>
778780
* The default path is that of the temporary folder. By default, the filename will be the same as the input file if not specified and the extension is XLSX
781+
* @deprecated since version 0.4.0
779782
* @param fileInput The input CSV file that will be converted into an Excel file
780783
* @return An Excel file that contains the same lines as the CSV file
781784
* @throws FileAlreadyExistsException If the destination file already exists
782785
* @throws CsvValidationException If the CSV file has invalid formatting
783786
* @throws ExtensionNotValidException If the input file extension does not belong to a CSV file
784787
* @throws IOException If an I/O error has occurred
785788
*/
789+
@Deprecated
786790
public static File csvToExcel(File fileInput) throws FileAlreadyExistsException, CsvValidationException, ExtensionNotValidException, IOException {
787791
return csvToExcel(fileInput, System.getProperty("java.io.tmpdir"), fileInput.getName().split("\\.")[0].trim(), Extension.XLSX);
788792
}
789793

790794
/**
791795
* Convert a CSV file into an Excel file<p>
792796
* The default path is that of the temporary folder. By default, the extension is XLSX
797+
* @deprecated since version 0.4.0
793798
* @param fileInput The input CSV file that will be converted into an Excel file
794799
* @param filename The name of the output file without the extension
795800
* @return An Excel file that contains the same lines as the CSV file
@@ -798,13 +803,15 @@ public static File csvToExcel(File fileInput) throws FileAlreadyExistsException,
798803
* @throws ExtensionNotValidException If the input file extension does not belong to a CSV file
799804
* @throws IOException If an I/O error has occurred
800805
*/
806+
@Deprecated
801807
public static File csvToExcel(File fileInput, String filename) throws FileAlreadyExistsException, CsvValidationException, ExtensionNotValidException, IOException {
802808
return csvToExcel(fileInput, System.getProperty("java.io.tmpdir"), filename, Extension.XLSX);
803809
}
804810

805811
/**
806812
* Convert a CSV file into an Excel file<p>
807813
* By default, the extension is XLSX
814+
* @deprecated since version 0.4.0
808815
* @param fileInput The input CSV file that will be converted into an Excel file
809816
* @param path The destination path of the output file
810817
* @param filename The name of the output file without the extension
@@ -814,12 +821,14 @@ public static File csvToExcel(File fileInput, String filename) throws FileAlread
814821
* @throws ExtensionNotValidException If the input file extension does not belong to a CSV file
815822
* @throws IOException If an I/O error has occurred
816823
*/
824+
@Deprecated
817825
public static File csvToExcel(File fileInput, String path, String filename) throws FileAlreadyExistsException, CsvValidationException, ExtensionNotValidException, IOException {
818826
return csvToExcel(fileInput, path, filename, Extension.XLSX);
819827
}
820828

821829
/**
822830
* Convert a CSV file into an Excel file
831+
* @deprecated since version 0.4.0
823832
* @param fileInput The input CSV file that will be converted into an Excel file
824833
* @param path The destination path of the output file
825834
* @param filename The name of the output file without the extension
@@ -830,6 +839,7 @@ public static File csvToExcel(File fileInput, String path, String filename) thro
830839
* @throws ExtensionNotValidException If the input file extension does not belong to a CSV file
831840
* @throws IOException If an I/O error has occurred
832841
*/
842+
@Deprecated
833843
public static File csvToExcel(File fileInput, String path, String filename, Extension extension) throws IOException, ExtensionNotValidException, CsvValidationException, FileAlreadyExistsException {
834844
/* Check exension */
835845
String csvExt = FilenameUtils.getExtension(fileInput.getName());
@@ -862,8 +872,61 @@ public static File csvToExcel(File fileInput, String path, String filename, Exte
862872
return outputFile;
863873
}
864874

875+
public static byte[] csvToExcelByte(byte[] bytes, String sheetName, Extension extension) throws CsvValidationException, ExtensionNotValidException, IOException {
876+
InputStream inputStream = new ByteArrayInputStream(bytes);
877+
ByteArrayOutputStream baos = (ByteArrayOutputStream) csvToExcelStream(inputStream, sheetName, extension);
878+
return baos.toByteArray();
879+
}
880+
881+
public static File csvToExcelFile(File fileInput, String sheetName, String pathname, Extension extension) throws IOException, CsvValidationException, ExtensionNotValidException {
882+
InputStream inputStream = new FileInputStream(fileInput);
883+
ByteArrayOutputStream baos = (ByteArrayOutputStream) csvToExcelStream(inputStream, sheetName, extension);
884+
pathname = pathname + "." + extension.getExt();
885+
FileOutputStream fileOutputStream = new FileOutputStream(pathname);
886+
fileOutputStream.write(baos.toByteArray());
887+
File file = new File(pathname);
888+
fileOutputStream.close();
889+
890+
return file;
891+
}
892+
893+
public static OutputStream csvToExcelStream(InputStream inputStream, String sheetName, Extension extension) throws ExtensionNotValidException, CsvValidationException, IOException {
894+
/* Check the extension */
895+
if (!ExcelUtility.isValidExcelExtension(extension.getExt())) {
896+
throw new ExtensionNotValidException("Pass a file with the XLS or XLSX extension");
897+
}
898+
899+
/* Open CSV Reader */
900+
Reader reader = new InputStreamReader(inputStream);
901+
CSVReader csvReader = new CSVReader(reader);
902+
903+
ExcelWorkbook excelWorkbook = new ExcelWorkbook(extension);
904+
ExcelSheet excelSheet = excelWorkbook.createSheet(sheetName);
905+
906+
/* Read CSV file */
907+
String[] values;
908+
int cRow = 0;
909+
while ((values = csvReader.readNext()) != null) {
910+
ExcelRow excelRow = excelSheet.createRow(cRow);
911+
for (int j = 0; j < values.length; j++) {
912+
ExcelCell excelCell = excelRow.createCell(j);
913+
excelCell.writeValue(values[j]);
914+
excelSheet.getSheet().autoSizeColumn(j);
915+
}
916+
cRow++;
917+
}
918+
919+
/* Write and close the Workbook */
920+
OutputStream outputStream = new ByteArrayOutputStream();
921+
excelWorkbook.writeAndClose(outputStream);
922+
csvReader.close();
923+
924+
return outputStream;
925+
}
926+
865927
/**
866928
* Convert the CSV file into a new sheet of an existing File.
929+
* @deprecated since version 0.4.0
867930
* @param fileOutput The {@code File} to update
868931
* @param fileInput The input CSV file that will be converted into an Excel file
869932
* @throws OpenWorkbookException If an error occurred while opening the workbook
@@ -872,6 +935,7 @@ public static File csvToExcel(File fileInput, String path, String filename, Exte
872935
* @throws CsvValidationException If the CSV file has invalid formatting
873936
* @since 0.2.1
874937
*/
938+
@Deprecated
875939
public static void csvToExistingExcel(File fileOutput, File fileInput) throws OpenWorkbookException, ExtensionNotValidException, IOException, CsvValidationException {
876940
/* Open workbook */
877941
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(fileOutput);
@@ -888,6 +952,7 @@ public static void csvToExistingExcel(File fileOutput, File fileInput) throws Op
888952

889953
/**
890954
* Writes the data present in the CSVReader to a new sheet of an existing File.
955+
* @deprecated since version 0.4.0
891956
* @param fileOutput The {@code File} to update
892957
* @param csvReader The {@code CSVReader} of the CSV input file
893958
* @throws OpenWorkbookException If an error occurred while opening the workbook
@@ -896,6 +961,7 @@ public static void csvToExistingExcel(File fileOutput, File fileInput) throws Op
896961
* @throws CsvValidationException If the CSV file has invalid formatting
897962
* @since 0.2.1
898963
*/
964+
@Deprecated
899965
public static void csvToExistingExcel(File fileOutput, CSVReader csvReader) throws OpenWorkbookException, ExtensionNotValidException, IOException, CsvValidationException {
900966
/* Open workbook */
901967
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(fileOutput);
@@ -913,12 +979,14 @@ public static void csvToExistingExcel(File fileOutput, CSVReader csvReader) thro
913979
/**
914980
* Convert the CSV file into a new sheet of an existing Workbook.<p>
915981
* Note: This method does not call the "write" method of the workbook.
982+
* @deprecated since version 0.4.0
916983
* @param workbook The {@code Workbook} to update
917984
* @param fileInput The input CSV file that will be converted into an Excel file
918985
* @throws IOException If an I/O error has occurred
919986
* @throws CsvValidationException If the CSV file has invalid formatting
920987
* @throws ExtensionNotValidException If the input file extension does not belong to a CSV file
921988
*/
989+
@Deprecated
922990
public static void csvToExistingExcel(Workbook workbook, File fileInput) throws IOException, CsvValidationException, ExtensionNotValidException {
923991
/* Check exension */
924992
String csvExt = FilenameUtils.getExtension(fileInput.getName());
@@ -936,11 +1004,13 @@ public static void csvToExistingExcel(Workbook workbook, File fileInput) throws
9361004
/**
9371005
* Writes the data present in the CSVReader to a new sheet of an existing Workbook.<p>
9381006
* Note: This method does not call the "write" method of the workbook.
1007+
* @deprecated since version 0.4.0
9391008
* @param workbook The {@code Workbook} to update
9401009
* @param csvReader The {@code CSVReader} of the CSV input file
9411010
* @throws CsvValidationException If the CSV file has invalid formatting
9421011
* @throws IOException If an I/O error has occurred
9431012
*/
1013+
@Deprecated
9441014
public static void csvToExistingExcel(Workbook workbook, CSVReader csvReader) throws CsvValidationException, IOException {
9451015
ExcelWorkbook excelWorkbook = new ExcelWorkbook(workbook);
9461016
ExcelSheet excelSheet = excelWorkbook.createSheet();

src/test/java/io/github/mbenincasa/javaexcelutils/tools/ConverterTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,4 +668,65 @@ void excelToCsvStream() throws IOException, OpenWorkbookException, CsvValidation
668668
csvFile.delete();
669669
fileInputStream.close();
670670
}
671+
672+
@Test
673+
void csvToExcelByte() throws IOException, CsvValidationException, ExtensionNotValidException, OpenWorkbookException, SheetNotFoundException {
674+
byte[] bytes = Files.readAllBytes(csvFile.toPath());
675+
byte[] bytesResult = Converter.csvToExcelByte(bytes, "Test", Extension.XLSX);
676+
File excelFile = new File("./test.xlsx");
677+
FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
678+
fileOutputStream.write(bytesResult);
679+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
680+
ExcelSheet excelSheet = excelWorkbook.getSheet("Test");
681+
ExcelRow excelRow = excelSheet.getRows().get(0);
682+
Row row = excelRow.getRow();
683+
Assertions.assertEquals("LAST NAME", row.getCell(0).getStringCellValue());
684+
Assertions.assertEquals("NAME", row.getCell(1).getStringCellValue());
685+
Assertions.assertEquals("AGE", row.getCell(2).getStringCellValue());
686+
row = excelSheet.getRows().get(1).getRow();
687+
Assertions.assertEquals("Rossi", row.getCell(0).getStringCellValue());
688+
Assertions.assertEquals("Mario", row.getCell(1).getStringCellValue());
689+
Assertions.assertEquals("20", row.getCell(2).getStringCellValue());
690+
fileOutputStream.close();
691+
excelFile.delete();
692+
}
693+
694+
@Test
695+
void csvToExcelFile() throws CsvValidationException, ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
696+
File excelFile = Converter.csvToExcelFile(csvFile, "Test", "./test", Extension.XLSX);
697+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
698+
ExcelSheet excelSheet = excelWorkbook.getSheet("Test");
699+
ExcelRow excelRow = excelSheet.getRows().get(0);
700+
Row row = excelRow.getRow();
701+
Assertions.assertEquals("LAST NAME", row.getCell(0).getStringCellValue());
702+
Assertions.assertEquals("NAME", row.getCell(1).getStringCellValue());
703+
Assertions.assertEquals("AGE", row.getCell(2).getStringCellValue());
704+
row = excelSheet.getRows().get(1).getRow();
705+
Assertions.assertEquals("Rossi", row.getCell(0).getStringCellValue());
706+
Assertions.assertEquals("Mario", row.getCell(1).getStringCellValue());
707+
Assertions.assertEquals("20", row.getCell(2).getStringCellValue());
708+
excelFile.delete();
709+
}
710+
711+
@Test
712+
void csvToExcelStream() throws IOException, CsvValidationException, ExtensionNotValidException, OpenWorkbookException, SheetNotFoundException {
713+
FileInputStream fileInputStream = new FileInputStream(csvFile);
714+
ByteArrayOutputStream baos = (ByteArrayOutputStream) Converter.csvToExcelStream(fileInputStream, "Test", Extension.XLSX);
715+
FileOutputStream fileOutputStream = new FileOutputStream("./test.xlsx");
716+
fileOutputStream.write(baos.toByteArray());
717+
File excelFile = new File("./test.xlsx");
718+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
719+
ExcelSheet excelSheet = excelWorkbook.getSheet("Test");
720+
ExcelRow excelRow = excelSheet.getRows().get(0);
721+
Row row = excelRow.getRow();
722+
Assertions.assertEquals("LAST NAME", row.getCell(0).getStringCellValue());
723+
Assertions.assertEquals("NAME", row.getCell(1).getStringCellValue());
724+
Assertions.assertEquals("AGE", row.getCell(2).getStringCellValue());
725+
row = excelSheet.getRows().get(1).getRow();
726+
Assertions.assertEquals("Rossi", row.getCell(0).getStringCellValue());
727+
Assertions.assertEquals("Mario", row.getCell(1).getStringCellValue());
728+
Assertions.assertEquals("20", row.getCell(2).getStringCellValue());
729+
fileOutputStream.close();
730+
excelFile.delete();
731+
}
671732
}

0 commit comments

Comments
 (0)