Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

How can I display Crystal XI reports inside a Delphi 2007 application?

The most recent Crystal XI component for Delphi was released for Delphi 7. That VCL component compiles in D2007, but gives me errors at runtime. What is the best way to display a database-connected Crystal Report in a Delphi 2007 application?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the solution I've found, using ActiveX:

First, register the Active X control like this:

In Delphi, choose Component -> Import Component

Click on "Type Library", click Next

Choose "Crystal ActiveX Report Viewer Library 11.5"

Pick whatever Palette Page you want (I went with "Data Access")

Choose an import location

Exit out of the wizard

Add the location you chose to your project Search Path

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) reference will be helpful, especially since Intellisense doesn't work on Ole objects like these.

Edit: The original link to the reference broke, so I changed it to point to a new one (valid as of Dec 15 2009). If that new one breaks, then Google should be able to find it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...