writeAll(ResultSet res,Boolean b)opencsv方法在数据周围添加双引号

时间:2011-07-27 08:42:34

标签: java opencsv

当我使用此函数写入csv文件时,所有数据都嵌入双引号中。

有没有办法在没有双引号的情况下写入csv文件?

CSVWriter writer = new CSVWriter(new FileWriter(table+".csv"), '\t');
            writer.writeAll(rset, true);
            writer.close();

该文件包含

形式的数据
"EMPNO" "ENAME" "JOB"   "MGR"   "HIREDATE"  "SAL"   "COMM"  "DEPTNO"    "TAG"   "LOOKUP"
"7369"  "SMITH" "CLERK" "7902"  "17-Dec-1980"   "800"   "2" "20"    "E" "1"
"7499"  "ALLEN" "SALESMAN"  "7698"  "20-Feb-1981"   "1600"  "2" "30"    "E" "2"
"7521"  "WARD"  "SALESMAN"  "7698"  "22-Feb-1981"   "1250"  "2" "30"    "E" "3"
"7566"  "JONES" "MANAGER"   "7839"  "02-Apr-1981"   "2975"  "2" "20"    "E" "2"

2 个答案:

答案 0 :(得分:17)

CSVWriter writer = new CSVWriter(new FileWriter(table+".csv"), '\t', CSVWriter.NO_QUOTE_CHARACTER);
            writer.writeAll(rset, true);
            writer.close();

参考:opencsv CSVWriter JavaDoc

答案 1 :(得分:0)

我想在@ user591593中添加上一个答案(没有足够的要点来添加评论) - 在使用NO_QUOTE_CHARACTER构造函数时,您应该自己转义这些值。见StringEscapeUtils

更重要的是,最好指定一个正确的编码,例如UTF-8,而不是假设系统默认是正确的编码:

CSVWriter writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), '\t', CSVWriter.NO_QUOTE_CHARACTER);

String s = "some fun \t string with \" double quotes and \n new lines";

writer.writeNext(StringEscapeUtils.escapeCsv(s));