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

查看:27
本文介绍了如何在 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:

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

首先,像这样注册 Active X 控件:

First, register the Active X control like this:

在 Delphi 中,选择组件 -> 导入组件

In Delp choose Component -> Import Component

点击类型库",点击下一步

Click on "Type Library", click Next

选择Crystal ActiveX 报告查看器库 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天全站免登陆