Currently I am using below code to convert Xlsx file to csv using java.Its taking more time because below code uses iterator to do this process. but I need any other better solu
Simple way to convert xls/xlsx into csv by using apache POI.
public class XLSXToCSVConverter {
public InputStream convertxlstoCSV(InputStream inputStream) throws IOException, InvalidFormatException {
Workbook wb = WorkbookFactory.create(inputStream);
return csvConverter(wb.getSheetAt(0));
}
private InputStream csvConverter(Sheet sheet) {
Row row = null;
String str = new String();
for (int i = 0; i < sheet.getLastRowNum()+1; i++) {
row = sheet.getRow(i);
String rowString = new String();
for (int j = 0; j < 3; j++) {
if(row.getCell(j)==null) {
rowString = rowString + Utility.BLANK_SPACE + Utility.COMMA;
}
else {
rowString = rowString + row.getCell(j)+ Utility.COMMA;
}
}
str = str + rowString.substring(0,rowString.length()-1)+ Utility.NEXT_LINE_OPERATOR;
}
System.out.println(str);
return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
}
}
Hope this will help you , Thanks.