0

因此,我已将 csv 更改为 xls/xlsx,但每个单元格只有一个字符。我在我的 csv 中使用了 pipe(|) 作为分隔符。这是csv中的一行:4.0|sdfa@sdf.nb|plplplp|plplpl|plplp|1988-11-11|M|asdasd@sdf.ghgh|sdfsadfasdfasdfasdfasdf|asdfasdf|3.4253242E7|234234.0|true|true|

但在 excel 中,我得到了 4 。0 | sdfa

这是代码:

    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");
    }
4

1 回答 1

2

管道符号必须在正则表达式中转义:

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

它是一个逻辑运算符(引用java.util.regex.Pattern 的 Javadoc):

X|Y X 或 Y

于 2013-08-07T10:54:43.357 回答