2017-10-20 65 views
0

我正在使用Java和opencsv(2.3)创建csv文件。opencsv内容 - 逗号后的值

正确创建。但是当我打开文件时,我看到所有数据都出现在单列中。

为了在Excel

的数据标签的值对准到单独的列

1.I选择“文本到列”

2.And我选择分隔符为“;”

  • 我看到所有的值被分裂成正常separte列,但逗号后的值越来越消失
  • CSVWriter我用它来创建CSV文件:

    File file = new File(fileName); 
        CSVWriter writer = new CSVWriter(new FileWriter(fileName, true), ';'); 
        String[] col= new String[4]; 
    
        for(Customer c : CustomerList) { 
         col[0] = c.getCustomerName(); 
         col[1] = c.getCustomerId(); 
         col[2] = c.getCustomerBirthDate(); 
         col[3] = c.getRegFee(); /** 145,65**/ 
         col[4] = c.getRegPlace(); 
         writer.writeNext(col); 
        } 
    
        writer.close(); 
    

    CSV文件 - 实际内容:

    "Micky";"1";"19901220";"455,56";"Place1" 
    
    "Grace";"2";"199";"465,87";"Place2" 
    

    CSV文件 - 在使用Excel中打开:

    "Micky";"1";"19901220";"455" // , 56 and Place1 are vanished 
    
        "Grace";"2";"199";"465" // , 87 and Place2 are vanished 
    

    回答

    0

    我认为这个问题是你它导入到Excel的方式做。

    使用上面的示例,我创建了一个CSV文件并在记事本中打开它以验证内容。

    enter image description here

    如果你双击一个CSV文件(并已与该文件类型相关联的Excel),它会在Excel中打开,它看起来像Excel中尝试使用逗号作为默认分隔符。它显示2列的数据。

    enter image description here

    如果您打开Excel,然后导入CSV文件,你可以告诉你的文件分隔,并且分号为分隔符的Excel。使用从文本菜单项从数据标签导入:

    enter image description here

    然后它会显示正确:

    enter image description here

    +0

    不,我选择分号作为分隔符,可以将其与被链接数字格式,我的意思是千位和小数点分隔符? –

    +0

    @AlagammalP我不这么认为。我将Excel配置为使用千位分隔符和逗号作为小数点分隔符。我仍然有和上面截图一样的结果。 –