1

I create a csv with c# csv.AppendLine($"{line.Time.Value.ToString("dd/MM/yyyy HH:mm:ss")} when open the excel it shows me 07/02/23 09:31 instead of 07/02/23 09:31:20

I used a trick that makes it work but shows 2 spaces. csv.AppendLine($"{line.Time.Value.ToString(" dd/MM/yyyy HH:mm:ss ")} Is there some other way to make it without spaces and trick?

csv.AppendLine($"{line.Time.Value.ToString(" dd/MM/yyyy HH:mm:ss ")}

Liron
  • 31
  • 2
  • In Excel: Is the Cell-Type a date or a string? Date cells in Excel are just a float number, actually. What gets to be displayed is all in the formatting. See also: https://support.microsoft.com/en-us/office/format-a-date-the-way-you-want-8e10019e-d5d8-47a1-ba95-db95123d273e Adding the spaces might make the import tool type the cell as string instead of date, which is _probably_ not what you want, right? – Fildor Feb 07 '23 at 07:41
  • doesnt help me because i dont want the user to deal with the excel formats. i want to give him that ready in c# – Liron Feb 07 '23 at 07:45
  • Then you have two options: Force the Cell to type string or deal with it. An alternative option would be to export to excel, directly. – Fildor Feb 07 '23 at 07:47
  • _OR_ you can do both: make it two columns: Date as string and Date as date. BTW if I remeber correctly, you can force string by prepending a "'" to the value. Like `'01/01/2023 13:45:20` – Fildor Feb 07 '23 at 07:49
  • For the date as date, I'd recommend using an unambiguous format like ISO. If you are interested in the direct export: [Ref1](https://stackoverflow.com/questions/3486278/c-sharp-export-to-excel), [Ref2](https://stackoverflow.com/questions/6840099/how-can-i-export-data-to-an-excel-file) – Fildor Feb 07 '23 at 07:51
  • For what it's worth, the data is still there in Excel, it just isn't displayed using the default date format. If you look at the "value" in the edit box at the top of the spreadsheet, when the cell is selected, you'll see the seconds component. You would need to apply formatting to the cells to force the seconds component to display. However, CSV doesn't have the ability to "persist" the format selection when you save. – MarkPflug Feb 07 '23 at 17:47

0 Answers0