当我使用此函数写入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"
CSVWriter writer = new CSVWriter(new FileWriter(table+".csv"), 't', CSVWriter.NO_QUOTE_CHARACTER);
writer.writeAll(rset, true);
writer.close();
参考:opencsv CSVWriter JavaDoc
我想通过@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));