0

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. I do not want date formatted and want to treat all cells as String. How can I do this?

4

1 回答 1

0

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

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

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

如果您希望 Excel 将所有单元格视为字符串,而不是自动格式化日期,以便您拥有自己的格式“yyyy-MM-dd”,您应该看看这个

于 2018-03-21T15:25:07.940 回答