簡體   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