univocity-parsers列写内容缺失前缀0
请教下输入数字或者金额的列怎么设置文本格式,使其原样写入csv文件中
Object[] r = {"'0000001111","3000.00"};
写入后的csv想要的效果是
真实结果:
h1 h2
1111 3000
1111 3000
想要的结果为:
结果:
h1 h2
0000001111 3000.00
0000001111 3000.00
以下是测试是源码
public static void createCSVFile(String[] heads, List<Object[]> rows, String outPutPath)
{
// CsvWriter (and all other file writers) work with an instance of
// java.io.Writer
File csvFile = new File(outPutPath);
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists())
{
parent.mkdirs();
}
try
{
csvFile.createNewFile();
// By default, only values that contain a field separator are enclosed within quotes.
// If quotes are part of the value, they are escaped automatically as well. Empty rows are discarded automatically.
// Set the field delimiter to ';', the default value is ','
CsvWriterSettings settings = new CsvWriterSettings();
CsvFormat format = settings.getFormat();
format.setDelimiter(',');
CsvWriter writer = new CsvWriter(csvFile, "GBK", settings);
// Write the record headers of this file
writer.writeHeaders(heads);
// Write contents and close the given output Writer instance.
writer.writeRowsAndClose(rows);
} catch (Exception e)
{
e.printStackTrace();
log.error(e.getMessage());
}
}
public static void main(String[] args)throws Exception {
String filePath = "C:/test/test.csv";
String[] headers = {"h1","h2"};
List<Object[]> rows = new ArrayList<Object[]>();
for (int i=0; i <2 ;i++) {
Object[] r = {"'0000001111","3000.00"};
rows.add(r);
}
try {
List<Map<String,Object>> results = null;
createCSVFile(headers, rows, filePath);
} catch (Exception e) {
e.printStackTrace();
}
}