Pregunta

So i've changed a csv to xls/xlsx but i'm getting one character per cell. I've used pipe(|) as a delimiter in my csv. Here is one line from the csv: 4.0|sdfa@sdf.nb|plplplp|plplpl|plplp|1988-11-11|M|asdasd@sdf.ghgh|sdfsadfasdfasdfasdfasdf|asdfasdf|3.4253242E7|234234.0|true|true|

But in excel i'm getting as 4 . 0 | s d f a

Here's the code:

    try {
        String csvFileAddress = "manage_user_info.csv"; //csv file address
        String xlsxFileAddress = "manage_user_info.xls"; //xls file address
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet("sheet1");
        String currentLine=null;
        int RowNum=0;
        BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
        while ((currentLine = br.readLine()) != null) {
            String str[] = currentLine.split("|");
            RowNum++;
            HSSFRow currentRow=sheet.createRow(RowNum);
            for(int i=0;i<str.length;i++){
                currentRow.createCell(i).setCellValue(str[i]);
            }
        }

        FileOutputStream fileOutputStream =  new FileOutputStream(xlsxFileAddress);
        workBook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("Done");
    } catch (Exception ex) {
        System.out.println(ex.getMessage()+"Exception in try");
    }
¿Fue útil?

Solución

The pipe symbol must be escaped in a regular expression:

String str[] = currentLine.split("\\|");

It is a logical operator (quote from the Javadoc of java.util.regex.Pattern):

X|Y Either X or Y

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top