View Single Post
  #3  
Old 03-26-2019, 04:49 PM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 299
Default

Dear Norrit,

Actually I have the code below to export adoDataSet Recordset into Excel. Now, I want to format the DATE column in excel with some datemask like 'yyyy-mm-dd'. So, how shall i check if the field of Recordset is Date. Kindly suggest.

HTML Code:
procedure TformPreviewReport.ExportRecordsetToMSExcel(Data: _Recordset);
var
  ovExcelApp: OleVariant;
  ovExcelWorkbook: OleVariant;
  ovWS: OleVariant;
  ovRange,ovColRange1,ovColRange2,ovColRange3,ovColRange4: OleVariant;
 iCol,jRow : Integer;
  v   : Variant;
begin
ovExcelApp := CreateOleObject('Excel.Application'); //If Excel isnt installed will raise an exception
        ovExcelApp.Visible:=False;
  try
    ovExcelWorkbook   := ovExcelApp.WorkBooks.Add;
    ovWS := ovExcelWorkbook.Worksheets.Item[1]; // go to first worksheet
    ovWS.Activate;
    ovWS.Select;
    ovRange := ovWS.Range['A1', 'A1']; //go to first cell
     
                ovWS.Cells[1, 1] := fReportHeader;
                ovRange := ovWS.Range['A2', 'A2']; //go to first cell  of second row
                jRow := 2;  


        ovRange.Resize[Data.RecordCount+1, Data.Fields.Count];

        for iCol := 1 to Data.Fields.Count do
        begin
                v := Data.Fields[iCol-1].Name;
               ovWS.Cells[jRow, iCol] := v;
        end;

                ovRange := ovWS.Range['A3', 'A3'] //go to first cell of third row
     
    ovRange.CopyFromRecordset(Data, Data.RecordCount, Data.Fields.Count); //this copy the entire recordset to the selected range in excel
     ovWS.SaveAs(GetSharedDir+'\MBNepalReports\'+fReportName+'.xls', 1, '', '', False, False);

  finally
    ovExcelWorkbook.Close(SaveChanges := False);
    ovWS := Unassigned;
    ovExcelWorkbook := Unassigned;
    ovExcelApp := Unassigned;
    MessageDlg('Export Completed. Please check '+GetSharedDir+'\MBNepalReports\'+fReportName+'.xls',mtInformation,[mbOk],0);
        Close;
  end;

end;
thanks...
__________________
Rmt
Reply With Quote