I have a datetime column in dbgrd in the datetime format I need so that when export to excel it displays the date and time column only the date

enter image description here

enter image description here

  • it is necessary that when exporting to excel it displays the "date and time" column only the date. Specify - in Excel DATA should have a date with time, but only the date is displayed, or the data itself should not contain a time component? The first is solved by formatting the cells, the second is by trimming data during transmission. - Akina
  • Excel should have only a date - delphi
  • So, it is necessary to comply with. data conversion during export (well, or before export). Say, using RecodeTime ... - Akina

3 answers 3

Is export to Excel carried out manually or with the help of some component / routine?

If the first - then there is no problem: in the comments have already proposed solutions. Could be so:

DateToStr(StrToDateTime(yourdatevar)); 

If the latter is possible, the following options are possible: an invisible copy of the dbgrid, in which the column format is changed (if the dbgrid is exported entirely), an invisible copy of the dbgrid, in which only one column (if exported by columns). This invisible copy and give away for export.

The second option is suitable for the "lazy": when you do not want to rewrite or understand the code. Although this approach is flawed.

    Find in which column the date is saved and apply to it:

     FormatDateTime('dd.mm.yy', StrToDateTime(mydate)); 
    • ExcelApp.WorkBooks [1] .WorkSheets ['Posishaimost']. Columns [2] .ColumnWidth: = 15; which part you need to insert Formatdatetime ('dd.mm.yy', strtodatatime (mydate)); ??? - delphi
    • This width is given - croll
    • ExcelApp.WorkBooks[1].WorkSheets['Posishaimost'].Columns[2].NumberFormat = "dd/mm/yyyy;@" But you need to understand that time will still be present in the data - it just does not appear. This may affect the calculations (for example, when grouped in a pivot table). - Akina
    • He said above that it does not suit him - croll
    • Write a loop to select a dataset - croll
      if(dbgrd1.Columns[col].Visible=True) then begin if dbgrd1.Columns[col].FieldName='DEALDATE' then sline := sline + FormatDateTime('dd/MM/yyyy', qry1.FieldByName(dbgrd1.Fields[col].FieldName).AsDateTime) + #9 else sline := sline + qry1.FieldByName(dbgrd1.Fields[col].FieldName).AsString + #9; end;