2013-08-07 22 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