2013-08-07 61 views
0

因此,我已将csv更改为xls/xlsx,但每个单元格都获得一个字符。我在我的csv中使用了pipe(|)作为分隔符。 这是一行csv: 4.0 | [email protected] | plplplp | plplpl | plplp | 1988-11-11 | M | [email protected] | sdfsadfasdfasdfasdfasdf | asdfasdf | 3.4253242E7 | 234234.0 | true | true |在从csv转换它的同时在xls/xlsx中获取错误的输出

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

回答

2

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

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

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

X | Y X或Y

+0

多么愚蠢的我,,感谢男人还是应该说铍,, :) – v0ld3m0rt

+0

@ASHWANI这个答案已被接受的答案之前,现在它已被拒绝。但我没有看到它的原因(如错误的答案,更好的答案)? – Beryllium