code_junkie

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

Nov 13th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. ...
  2. uses
  3. CrystalActiveXReportViewerLib11_5_TLB, OleAuto;
  4. ...
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. var
  8. cry : TCrystalActiveXReportViewer;
  9. oRpt, oApp : variant;
  10. i : integer;
  11. frm : TForm;
  12. begin
  13. cry := TCrystalActiveXReportViewer.Create(Self);
  14. oApp := CreateOleObject('CrystalRuntime.Application');
  15. oRpt := oApp.OpenReport('c:my_report.rpt',1);
  16. for i := 1 to oRpt.Database.Tables.Count do begin
  17. oRpt.Database.Tables[i].ConnectionProperties.Item['User ID'] := 'username';
  18. oRpt.Database.Tables[i].ConnectionProperties.Item['Password'] := 'password';
  19. end;
  20.  
  21. frm := TForm.Create(Self);
  22. try
  23. cry.Parent := frm;
  24. cry.Align := alClient;
  25. cry.ReportSource := oRpt;
  26. cry.ViewReport;
  27. frm.Position := poOwnerFormCenter;
  28. frm.ShowModal;
  29. finally
  30. FreeAndNil(frm);
  31. end; //try-finally
  32. end;
  33.  
  34. procedure TForm1.btnExportClick(Sender: TObject);
  35. var
  36. cry : TCrystalActiveXReportViewer;
  37. oRpt, oApp : variant;
  38. i : integer;
  39. begin
  40. //Export the report to a file
  41. cry := TCrystalActiveXReportViewer.Create(Self);
  42. oApp := CreateOleObject('CrystalRuntime.Application');
  43. oRpt := oApp.OpenReport(c_DBRpt,1);
  44. for i := 1 to oRpt.Database.Tables.Count do begin
  45. oRpt.Database.Tables[i].ConnectionProperties.Item['User ID'] := 'username';
  46. oRpt.Database.Tables[i].ConnectionProperties.Item['Password'] := 'password';
  47. end;
  48.  
  49. oRpt.ExportOptions.FormatType := 29; //excel 8
  50. oRpt.ExportOptions.DiskFileName := 'c:output.xls';
  51. oRpt.ExportOptions.DestinationType := 1; //file destination
  52. //Export(False) => do NOT prompt.
  53. //Export(True) will give runtime prompts for export options.
  54. oRpt.Export(False);
  55. end;
  56.  
  57. {
  58. Class to facilitate the display of Crystal 11 Reports.
  59.  
  60. The Crystal 11 VCL component does not seem to work with Delphi 2007.
  61. As a result, we have to use ActiveX objects, which make deployment messy.
  62.  
  63. This class is similar to CrystalReporter, but it works for Crystal 11.
  64. However, it lacks some of the features of the old CrystalReporter.
  65.  
  66. Refer to the crystal reports activex technical reference to duplicate the
  67. missing functionality.
  68.  
  69. Example usage is at the bottom of this unit.
  70. //}
  71. unit CrystalReporter11;
  72.  
  73. interface
  74.  
  75. uses
  76. CrystalActiveXReportViewerLib11_5_TLB, OleAuto, Classes, Controls;
  77.  
  78. type
  79. TCryExportFormat = (
  80. XLS
  81. ,PDF
  82. );
  83.  
  84. type
  85. TCrystalReporter11 = class
  86. private
  87. FCryRpt : TCrystalActiveXReportViewer;
  88. FRpt, FApp : variant;
  89. FReportFile, FUsername, FPassword, FServer, FFilters : string;
  90. FOwner : TComponent;
  91. procedure SetLoginInfo(const username, password, server : string);
  92. function GetFilterConds: string;
  93. procedure SetFilterConds(const Value: string);
  94. public
  95. property FilterConditions : string read GetFilterConds write SetFilterConds;
  96.  
  97. procedure ExportToFile(ExportFileName : string;
  98. FileExportFmt : TCryExportFormat; PromptForOptions : boolean);
  99. procedure Display;
  100.  
  101. constructor Create(AOwner : TComponent; ReportFile : string); overload;
  102. constructor Create(AOwner : TComponent; ReportFile,
  103. Username, Password, Server : string); overload;
  104. end;
  105.  
  106. implementation
  107.  
  108. uses
  109. SysUtils, Forms;
  110.  
  111. const
  112. //these are taken from pgs 246 and 247 of the technical reference
  113. c_FmtCode_Excel = 29;
  114. c_FmtCode_PDF = 31;
  115.  
  116. constructor TCrystalReporter11.Create(AOwner: TComponent; ReportFile: string);
  117. begin
  118. inherited Create;
  119. try
  120. FReportFile := ReportFile;
  121. if FileExists(FReportFile) then begin
  122. FOwner := AOwner;
  123. FCryRpt := TCrystalActiveXReportViewer.Create(AOwner);
  124. FApp := CreateOleObject('CrystalRuntime.Application');
  125. FRpt := FApp.OpenReport(FReportFile,1);
  126. FFilters := FRpt.RecordSelectionFormula;
  127. end
  128. else begin
  129. raise Exception.Create('Report file ' + ReportFile + ' not found!');
  130. end;
  131. except on e : exception do
  132. raise;
  133. end; //try-except
  134. end;
  135.  
  136. constructor TCrystalReporter11.Create(AOwner: TComponent; ReportFile, Username,
  137. Password, Server: string);
  138. begin
  139. Create(AOwner,ReportFile);
  140. FUsername := Username;
  141. FPassword := Password;
  142. FServer := Server;
  143. SetLoginInfo(FUsername,FPassword,FServer);
  144. end;
  145.  
  146. procedure TCrystalReporter11.Display;
  147. var
  148. rptForm : TForm;
  149. begin
  150. SetLoginInfo(FUsername,FPassword,FServer);
  151. FCryRpt.ReportSource := FRpt;
  152. rptForm := TForm.Create(FOwner);
  153. try
  154. FCryRpt.Parent := rptForm;
  155. FCryRpt.Align := alClient;
  156. FCryRpt.ViewReport;
  157. rptForm.Position := poOwnerFormCenter;
  158. rptForm.WindowState := wsMaximized;
  159. rptForm.Caption := ExtractFileName(FReportFile);
  160. rptForm.ShowModal;
  161. finally
  162. FreeAndNil(rptForm);
  163. end; //try-finally
  164. end;
  165.  
  166. procedure TCrystalReporter11.ExportToFile(ExportFileName : string;
  167. FileExportFmt : TCryExportFormat; PromptForOptions : boolean);
  168. begin
  169. case FileExportFmt of
  170. XLS : FRpt.ExportOptions.FormatType := c_FmtCode_Excel;
  171. PDF : FRpt.ExportOptions.FormatType := c_FmtCode_PDF;
  172. end; //case
  173.  
  174. FRpt.ExportOptions.DiskFileName := ExportFileName;
  175. FRpt.ExportOptions.DestinationType := 1; //file destination
  176. FCryRpt.ReportSource := FRpt;
  177. FRpt.Export(PromptForOptions);
  178. end;
  179.  
  180. function TCrystalReporter11.GetFilterConds: string;
  181. begin
  182. Result := FFilters;
  183. end;
  184.  
  185. procedure TCrystalReporter11.SetFilterConds(const Value: string);
  186. begin
  187. FFilters := Value;
  188. if 0 < Length(Trim(FFilters)) then begin
  189. FRpt.RecordSelectionFormula := Value;
  190. end;
  191. end;
  192.  
  193. procedure TCrystalReporter11.SetLoginInfo(const username, password,
  194. server : string);
  195. var
  196. i : integer;
  197. begin
  198. //set user name and password
  199. //crystal only accepts these values if they are CONST params
  200. for i := 1 to FRpt.Database.Tables.Count do begin
  201. FRpt.Database.Tables[i].ConnectionProperties.Item['User ID'] := username;
  202. FRpt.Database.Tables[i].ConnectionProperties.Item['Password'] := password;
  203. try
  204. {
  205. Some reports use direct connections, and others use an ODBC Data Source.
  206. Crystal XI uses a different label to refer to the database name in each
  207. method.
  208. I don't know how to determine in advance which method is being used, so:
  209. First, we try the direct connection.
  210. If that fails, we try the "data source" method.
  211.  
  212. Reference: "Crystal Reports XI Technical Reference", pages 41 thru 46;
  213. "Common ConnectionProperties"
  214. }
  215. FRpt.Database.Tables[i].ConnectionProperties.Item['Server'] := server;
  216. except on E: Exception do
  217. FRpt.Database.Tables[i].ConnectionProperties.Item['Data Source'] := server;
  218. end;
  219. end;
  220. end;
  221. {
  222. Example usage:
  223.  
  224. procedure TForm1.btnShowRptDBClick(Sender: TObject);
  225. var
  226. cry : TCrystalReporter11;
  227. begin
  228. cry := TCrystalReporter11.Create(Self,'c:my_report.rpt','username',
  229. 'password','server.domain.com');
  230. try
  231. cry.Display;
  232. finally
  233. FreeAndNil(cry);
  234. end;
  235. end;
  236. }
  237. end.
Add Comment
Please, Sign In to add comment