This is the solution I found using ActiveX:
First register the Active X element as follows:
In Delphi, select Component → Import Component
Click "Type Library", click "Next"
Select "Crystal ActiveX Report Viewer Library 11.5"
Select any page of the palette that you want (I went to "Data Access")
Choose an import location
Exit the wizard
Add the location that you selected in your path to the project.
Now this code should work:
... uses CrystalActiveXReportViewerLib11_5_TLB, OleAuto; ... procedure TForm1.Button1Click(Sender: TObject); var cry : TCrystalActiveXReportViewer; oRpt, oApp : variant; i : integer; frm : TForm; begin cry := TCrystalActiveXReportViewer.Create(Self); oApp := CreateOleObject('CrystalRuntime.Application'); oRpt := oApp.OpenReport('c:\my_report.rpt',1); for i := 1 to oRpt.Database.Tables.Count do begin oRpt.Database.Tables[i].ConnectionProperties.Item['User ID'] := 'username'; oRpt.Database.Tables[i].ConnectionProperties.Item['Password'] := 'password'; end; frm := TForm.Create(Self); try cry.Parent := frm; cry.Align := alClient; cry.ReportSource := oRpt; cry.ViewReport; frm.Position := poOwnerFormCenter; frm.ShowModal; finally FreeAndNil(frm); end; //try-finally end; procedure TForm1.btnExportClick(Sender: TObject); var cry : TCrystalActiveXReportViewer; oRpt, oApp : variant; i : integer; begin //Export the report to a file cry := TCrystalActiveXReportViewer.Create(Self); oApp := CreateOleObject('CrystalRuntime.Application'); oRpt := oApp.OpenReport(c_DBRpt,1); for i := 1 to oRpt.Database.Tables.Count do begin oRpt.Database.Tables[i].ConnectionProperties.Item['User ID'] := 'username'; oRpt.Database.Tables[i].ConnectionProperties.Item['Password'] := 'password'; end; oRpt.ExportOptions.FormatType := 29; //excel 8 oRpt.ExportOptions.DiskFileName := 'c:\output.xls'; oRpt.ExportOptions.DestinationType := 1; //file destination //Export(False) => do NOT prompt. //Export(True) will give runtime prompts for export options. oRpt.Export(False); end;
If you use this method, then this (rather dense) link will be useful, especially since Intellisense does not work on Ole objects like them.
Edit: The original link to the link has broken, so I changed it to a new one (valid from December 15, 2009). If this new one breaks, then Google should find it .
source share