[英]CSV file genereated using java when opened in excel sheet formats date for only few cells
I have written following code: 我写了以下代码:
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator("\n").withEscape('\\');
FileWriter fileWriter = new FileWriter(csvFile);
CSVPrinter csvFilePrinter = new CSVPrinter(fileWriter,csvFileFormat);
function printCSV(String fieldValue,String dataType,CSVPrinter csvFilePrinter){
if(dataType!=null && "date".equals(dataType)){
fieldValue = this.dateFormatter.format(new SimpleDateFormat("yyyy-MM-dd").parse(fieldValue));
}
csvFilePrinter.printRecord(fieldValue);
}
When the csv file is opened in excel sheet, only few cells are date formatted. 在excel工作表中打开csv文件时,只有很少的单元格具有日期格式。 I do not want date formatted and want to treat all cells as String.
我不想格式化日期,并希望将所有单元格都视为String。 How can I do this?
我怎样才能做到这一点?
The if
block from printCSV(..)
method formats the date. printCSV(..)
方法的if
块格式化日期。 You could remove it: 您可以删除它:
function printCSV(String fieldValue, String dataType, CSVPrinter csvFilePrinter)
{
csvFilePrinter.printRecord(fieldValue);
}
And, as you can see, you can remove the printCSV(..)
method and use directly csvFilePrinter.printRecord(fieldValue);
而且,如您所见,您可以删除
printCSV(..)
方法并直接使用csvFilePrinter.printRecord(fieldValue);
If you want Excel to treat all cells as string, and not auto formatting date so you have your own formatting "yyyy-MM-dd", you should take a look at this 如果您希望Excel把所有的细胞作为字符串,而不是自动格式化日期,以便你有自己的格式“YYYY-MM-DD”,你应该看看这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.