动态创建FastReport4报表2 - 由cxGrid动态创建报表

本文介绍了一种使用Delphi动态创建FastReport4报表的方法,包括如何设置报表页面、标题、页眉等,并通过循环遍历表格视图的列来自动生成报表数据区域及表头。

动态创建FastReport4报表2

这次在第一版的基础上增加了对数据集的支持


unit Unit1;
  
interface
  
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxContainer, cxEdit, cxTextEdit, cxMaskEdit, cxButtonEdit, cxStyles,
  cxCustomData, cxFilter, cxData, cxDataStorage, DB, cxDBData,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ComCtrls, frxClass,
  ToolWin, ADODB, cxGridLevel, cxClasses, cxGridCustomView, cxGrid, frxDesgn,
  frxDBSet, frxDMPExport, frxDCtrl, frxBarcode;
  
type
  TForm1 = class(TForm)
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    ADOConnection1: TADOConnection;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    cxGrid1DBTableView1OID: TcxGridDBColumn;
    cxGrid1DBTableView1AID: TcxGridDBColumn;
    cxGrid1DBTableView1Name: TcxGridDBColumn;
    cxGrid1DBTableView1Code: TcxGridDBColumn;
    cxGrid1DBTableView1Description: TcxGridDBColumn;
    cxGrid1DBTableView1Address: TcxGridDBColumn;
    cxGrid1DBTableView1Logo: TcxGridDBColumn;
    cxGrid1DBTableView1Level: TcxGridDBColumn;
    cxGrid1DBTableView1CodeB: TcxGridDBColumn;
    frxDesigner1: TfrxDesigner;
    frxDBDataset1: TfrxDBDataset;
    frxDialogControls1: TfrxDialogControls;
    frxReport1: TfrxReport;
    frxBarCodeObject1: TfrxBarCodeObject;
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  
var
  Form1: TForm1;
  
implementation
  
{$R *.dfm}
  
procedure TForm1.ToolButton1Click(Sender: TObject);
var
  vPage: TfrxReportPage;
  vTitle: TfrxReportTitle;
  vHeader: TfrxPageHeader;
  vView: TfrxMemoView;
  vData: TfrxMasterData;
  vDataView: TfrxMemoView;
  vHeadView: TfrxMemoView;
  i: integer;
  vLeft: integer;
begin
  frxReport1.Clear;
  frxReport1.DataSets.Add(frxDBDataset1);
  vPage := TfrxReportPage.Create(frxReport1);
  vPage.CreateUniqueName;
  vPage.LeftMargin := 10;
  vPage.RightMargin := 10;
  vPage.TopMargin := 10;
  vPage.BottomMargin := 10;
  vTitle := TfrxReportTitle.Create(vPage);
  vTitle.CreateUniqueName;
  vTitle.Height := 48;
  vTitle.Top := 0;
  
  vView := TfrxMemoView.Create(vTitle);
  vView.CreateUniqueName;
  vView.Top := 0;
  vView.Left := 0;
  vView.Width := vPage.Width - Round(vPage.LeftMargin * 2.8) - Round(vPage.RightMargin* 2.8);
  vView.Height := vTitle.Height;
  vView.Text := 'This is a title';
  vView.Font.Size := 24;
  vView.Font.Style := vView.Font.Style + [fsBold];
  vView.HAlign := frxClass.haCenter;
  
  vHeader := TfrxPageHeader.Create(vPage);
  vHeader.CreateUniqueName;
  vHeader.Top := 0;
  vHeader.Height := 20;
  
  vData := TfrxMasterData.Create(vPage);
  vData.CreateUniqueName;
  vData.Height := 20;
  vData.Top := 0;
  vData.DataSet := frxDBDataset1;
  vLeft := 0;
  for i := 0 to cxGrid1DBTableView1.ColumnCount - 1 do
  begin
    vDataView := TfrxMemoView.Create(vData);
    vDataview.WordBreak := True;
    vDataView.Frame.Typ := [ftLeft, ftRight, ftTop, ftBottom];
    vDataView.VAlign := frxClass.vaCenter;
    vDataView.Top := 0;
    vDataView.Left := vLeft;
    vDataView.Height := vData.Height;
    vDataView.Font.Name := 'Tahoma';
    vDataView.Font.Size := 8;
  
    vHeadView := TfrxMemoView.Create(vHeader);
    vHeadView.CreateUniqueName;
    vHeadView.WordBreak := True;
    vHeadView.Frame.Typ := [ftLeft, ftRight, ftTop, ftBottom];
    vHeadView.VAlign := frxClass.vaCenter;
    vHeadView.Left := vLeft;
    vHeadView.Height := vData.Height;
    vHeadView.Font.Name := 'Tahoma';
    vHeadView.Font.Size := 8;
  
    vDataView.Width := cxGrid1DBTableView1.Columns[i].Width;
  
    vHeadView.Width :=cxGrid1DBTableView1.Columns[i].Width;
    vHeadView.Text := cxGrid1DBTableView1.Columns[i].Caption;
    vDataView.Text := Format('[frxDBDataset1."%s"]', [cxGrid1DBTableView1.Columns[i].DataBinding.FieldName]);
    vleft := vLeft + cxGrid1DBTableView1.Columns[i].Width;
  end;
  
//  frxReport1.PrepareReport();
  frxReport1.ShowReport();
//  frxReport1.DesignReport();
end;
  
  
  
end.



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值