如何在Delphi 2007应用程序中显示Crystal XI报告? [英] How can I display Crystal XI reports inside a Delphi 2007 application?

查看:388
本文介绍了如何在Delphi 2007应用程序中显示Crystal XI报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi最新的Crystal XI组件是针对Delphi 7发布的。该VCL组件在D2007中编译,但在运行时给出错误。在Delphi 2007应用程序中显示数据库连接的Crystal Report的最好方法是什么?

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?

推荐答案

使用ActiveX:

首先,注册Active X控件,如下所示:

First, register the Active X control like this:

组件 - >导入组件

点击类型库,点击下一步

Click on "Type Library", click Next

选择Crystal ActiveX Report Viewer Library 11.5

Choose "Crystal ActiveX Report Viewer Library 11.5"

选择您想要的调色板页面(我使用数据访问)

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

导入位置

退出向导

添加您选择的位置到您的项目搜索路径

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;

如果使用此方法,则这个(相当稠密的)引用将会有所帮助,特别是因为Intellisense不支持Ole对象。

If you use this method, then this (rather dense) reference will be helpful, especially since Intellisense doesn't work on Ole objects like these.

编辑:参考文献的原始链接破坏了,所以我改变它指向一个新的(从2009年12月15日有效)。如果该新版本发生中断,则 Google应该能够找到它

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.

这篇关于如何在Delphi 2007应用程序中显示Crystal XI报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆