繁体   English   中英

当以excel表格格式打开时,使用Java生成的CSV文件的日期只有很少的单元格

[英]CSV file genereated using java when opened in excel sheet formats date for only few cells

我写了以下代码:

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);
}

在excel工作表中打开csv文件时,只有很少的单元格具有日期格式。 我不想格式化日期,并希望将所有单元格都视为String。 我怎样才能做到这一点?

printCSV(..)方法的if块格式化日期。 您可以删除它:

function printCSV(String fieldValue, String dataType, CSVPrinter csvFilePrinter)
{
    csvFilePrinter.printRecord(fieldValue);
}

而且,如您所见,您可以删除printCSV(..)方法并直接使用csvFilePrinter.printRecord(fieldValue);

如果您希望Excel把所有的细胞作为字符串,而不是自动格式化日期,以便你有自己的格式“YYYY-MM-DD”,你应该看看这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM