Recent

Author Topic: Converting excel to csv -> date format  (Read 1085 times)

wp

  • Hero Member
  • *****
  • Posts: 12920
Re: Converting excel to csv -> date format
« Reply #15 on: July 08, 2025, 11:29:51 pm »
Quote
Depending on what you want to do with the worksheets before and after the CSV exports, you could also try to modify the workbook's format settings.

Unfortunately, I did already try this using this code:
Code: Pascal  [Select][+][-]
  1.   CSVParams.FormatSettings.DateSeparator:= '/';
  2.   CSVParams.FormatSettings.ShortDateFormat:= 'dd/mm/yyyy';
  3.   CSVParams.FormatSettings.ShortTimeFormat:= 'hh:mm';
  4.  
  5.   outworkbook:= TsWorkbook.Create;
  6.   outworkbook.FormatSettings:= CSVParams.FormatSettings;
When you do it this way, the other fields of the CSVParams.FormatSettings and (since you assign them to the workbook's FormatSettings) are empty and you may get unexpected behaviour. The CSVParams should be changed only when the workbook exists because the empty fields of the CSVParams.FormatSettings will be filled by the workbook's FormatSettings fields.

Better: Either initialize all fields of the CSVParams.FormatSettings at the beginnung (CSVParams.FormatSettings := DefaultFormatSettings), or leave the CSVParams alone and only set the workbook's FormatSettings:
Code: Pascal  [Select][+][-]
  1.   b := TsWorkbook.Create;
  2.   try
  3.     b.FormatSettings := CSVParams.FormatSettings;
  4.     b.FormatSettings.LongDateFormat := 'dddd, dd/mm/yyyy';
  5.     b.FormatSettings.ShortDateFormat := 'dd/mm/yyyy';
  6.     b.FormatSettings.DateSeparator := '/';  

andresayang

  • Full Member
  • ***
  • Posts: 124
Re: Converting excel to csv -> date format
« Reply #16 on: July 09, 2025, 01:44:28 am »
@wp,

Many thanks for your codes samples.
I did manage to obtain what I wanted using this example
Code: Pascal  [Select][+][-]
  1. sh.WriteDateTime(2, 0, 44000, 'dd"/"mm"/"yyyy');
which seems to be the only one which works on both linux and windows.
I did apply same code sample for times.

Cheers

Quote
The CSVParams should be changed only when the workbook exists because the empty fields of the CSVParams.FormatSettings will be filled by the workbook's FormatSettings fields.
I guess this was my mistakes.

Well, for the moment, I have a working solution.
I will try to review the code once I have some spare time (I do this code to reduce my daily working load, this simple code save me 1 or 2 hours a day)

And again, many thanks for you help.



« Last Edit: July 09, 2025, 01:49:12 am by andresayang »
Linux, Debian 12
Lazarus: always latest release

 

TinyPortal © 2005-2018