how to convert xlsx file to csv using java?

后端 未结 2 1326
盖世英雄少女心
盖世英雄少女心 2021-02-04 22:45

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

相关标签:
2条回答
  • 2021-02-04 23:22

    Try using POI API. Here is the reference link

    Java - Apache POI - Convert XLS/XLSX to CSV

    0 讨论(0)
  • 2021-02-04 23:30

    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.

    0 讨论(0)
提交回复
热议问题