📄 reportdemo.pas
字号:
//欢迎使用,成都 李泽伦
unit REportDemo;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ReportControl, StdCtrls, Db, DBTables, Grids, DBGrids, printers, Buttons,
ExtCtrls, ExtDlgs, DBGridEh;
type
TCReportDemoForm = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Button3: TButton;
Button4: TButton;
Button5: TButton;
CheckBox1: TCheckBox;
ReportRunTime2: TReportRunTime;
ReportControl1: TReportControl;
Table1: TTable;
Table2: TTable;
DataSource1: TDataSource;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
opbm1: TOpenPictureDialog;
ReportRunTime1: TReportRunTime;
Button2: TButton;
DataSource2: TDataSource;
Panel3: TPanel;
Panel6: TPanel;
DBGrid1: TDBGrid;
Panel7: TPanel;
SpeedButton3: TSpeedButton;
DBGrid2: TDBGrid;
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure ReportRunTime1SetCellFont(Sender: TObject);
procedure CheckBox3Click(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
CReportDemoForm: TCReportDemoForm;
implementation
{$R *.DFM}
procedure TCReportDemoForm.Button4Click(Sender: TObject);
begin
close;
end;
procedure TCReportDemoForm.Button5Click(Sender: TObject);
begin
//ReportRunTime1.SetDataSet('t1',table1);
//ReportRunTime1.SetDataSet('t2',table2);
Table1.DisableControls;
ReportRunTime1.ReportFile := ExtractFilepath(application.ExeName) + '演示报表模版.ept'; //动态填写模版内容选creport_demo1.ept
ReportRunTime1.Setvarvalue('jgtw', '金额单位:元');
ReportRunTime1.Setvarvalue('head', '二OO三年度预算分配明细表');
ReportRunTime1.Setvarvalue('Twmc', '单位:财政局');
ReportRunTime1.PrintPreview(true); //true参数表示预览不显示非打印表格线
Table1.EnableControls;
end;
procedure TCReportDemoForm.Button3Click(Sender: TObject);
begin
Table1.DisableControls;
ReportRunTime1.ReportFile := ExtractFilepath(application.ExeName) + '演示报表模版.ept'; //动态填写模版内容选creport_demo1.ept
ReportRunTime1.Setvarvalue('jgtw', '金额单位:元');
ReportRunTime1.Setvarvalue('head', '二OO三年度预算分配明细表');
ReportRunTime1.Setvarvalue('name', '单位:万元');
ReportRunTime1.Print(false); //false 代表选择打印页,true则直接打印
Table1.EnableControls;
end;
procedure TCReportDemoForm.FormCreate(Sender: TObject);
begin
if not table1.Active then
begin
table1.DatabaseName := ExtractFilepath(application.ExeName);
table1.open;
end;
if not table2.Active then
begin
table2.DatabaseName := ExtractFilepath(application.ExeName);
table2.open;
end;
end;
procedure TCReportDemoForm.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then
ReportRunTime1.AddSpace := true
else
ReportRunTime1.AddSpace := false;
end;
procedure TCReportDemoForm.FormPaint(Sender: TObject);
begin
CheckBox1.Checked := ReportRunTime1.AddSpace;
end;
procedure TCReportDemoForm.SpeedButton3Click(Sender: TObject);
var
j: integer;
strFileDir: string;
CellFont: TLogFont;
cf: TFont;
begin
cf := Tfont.Create;
strFileDir := ExtractFileDir(Application.ExeName);
with ReportControl1 do
begin
SetWndSize(1058, 748); // 设置纸张大小
NewTable(dbgrid1.Columns.Count, 5); // 创建表格
SetCellSFocus(0, 0, 0, dbgrid1.Columns.Count - 1); //选取0行
CombineCell; //合并0行单元格
setLineHegit(0, 80); //设定0行的高度
SetCellLines(false, false, false, true, 1, 1, 1, 1,0,0,0,0); //去掉表格线
SetCallText(0, 0, '二00三年度财政收支统计表'); //填0行的内容
SetCellAlign(1, 1); //将选中行的文字居中
cf.Name := '楷体_GB2312';
cf.Size := 22;
cf.style := cf.style + [fsBold]; // 斜体[fsItalic] 下划线[fsunderline]
GetObject(cf.Handle, SizeOf(CellFont), @CellFont);
SetCellFont(CellFont); //设定字体
setLineHegit(1, 40); //设定第2行字段名的高度
RemoveAllSelectedCell; //取销选中单元格
SetCellSFocus(1, 0, 1, dbgrid1.Columns.Count - 1); //选择第2行进行操作
SetCellAlign(1, 1); //将选中行的文字居中
cf.Name := '仿宋_GB2312';
cf.Size := 16;
cf.Style := [];
GetObject(cf.Handle, SizeOf(CellFont), @CellFont);
SetCellFont(CellFont);//设定字体
SetCellColor(clRed, clWhite); //白底红字
for j := 0 to dbgrid1.Columns.Count - 1 do //填字段名及明细表字段
begin
SetCallText(1, j, dbgrid1.Columns[j].FieldName);
SetCallText(2, j, '#T1.' + dbgrid1.Columns[j].FieldName);
RemoveAllSelectedCell;
SetCellFocus(2, j); //
if dbgrid1.DataSource.DataSet.FieldByName(dbgrid1.Columns[j].FieldName) is tnumericField then //如果是数值字段
begin
SetCellAlign(2, 1); //右对齐
SetCellDispFormt('0,.00');
end else SetCellAlign(3, 1);//左对齐
end; // for j := 0 to dbgrid1.Columns.Count - 1 do //填字段名及明细表字段
setLineHegit(2, 20); //设定第3行的高度
RemoveAllSelectedCell; //取销选中单元格
SetCellSFocus(3, 0, 3, dbgrid1.Columns.Count - 1); //选择第4行进行操作
CombineCell; //合并单元格
SetCallText(3, 0, '@T2.bmp');
setLineHegit(3, 250); //设定第4行的高度
RemoveAllSelectedCell;
SetCellSFocus(4, 0, 4, dbgrid1.Columns.Count - 1); //选择第5行
CombineCell; //合并单元格
SetCellLines(false, false, false, false, 1, 1, 1, 1,0,0,0,0); //去掉表格线
setLineHegit(4, 50); //设定第5行的高度
SetCallText(4, 0, '`PageNum/'); //本行内容为"第?/?页"样式的页码
SetCellAlign(1, 1); //居中
SaveToFile(strFileDir + '\' + '动态生成的报表.ept'); //保存模板文件
ResetContent;
cf.Free;
end;
//预览
Table1.DisableControls;
ReportRunTime2.CreportButt:= CheckBox3.Checked;
ReportRunTime2.EditEpt:= CheckBox2.Checked;
ReportRunTime2.ReportFile := strFileDir + '\' + '动态生成的报表.ept';
ReportRunTime2.PrintPreview(true); //true参数表示预览不显示非打印表格线
Table1.EnableControls;
end;
procedure TCReportDemoForm.ReportRunTime1SetCellFont(Sender: TObject);
begin
with ReportRunTime1 do
begin
if (UPPERCASE(Rcelltext)='#T1.NAME') and (Table1.FieldByName('no').Value='3') then
// RCellFont:=setFont('楷体_GB2312',ReCellFontsize(RCellFont),true); //字体楷体_GB2312 字号不变,加粗
RCellFont:=setFont(RCellFont.lfFaceName,22,true); //字体不变 字号22,加粗
end;
end;
procedure TCReportDemoForm.CheckBox3Click(Sender: TObject);
begin
if CheckBox3.Checked then
ReportRunTime1.CreportButt := true
else
ReportRunTime1.CreportButt :=false;
end;
procedure TCReportDemoForm.CheckBox2Click(Sender: TObject);
begin
if CheckBox2.Checked then
ReportRunTime1.EditEpt := true
else
ReportRunTime1.EditEpt := false;
end;
procedure TCReportDemoForm.Button2Click(Sender: TObject);
var
Fpicture: Tpicture;
FBmp: TBitmap;
begin
table2.First;
if opbm1.Execute then
begin
Fpicture := Tpicture.Create;
Fpicture.LoadFromFile(opbm1.filename);
fbmp := TBitmap.Create;
if not (Fpicture.Graphic is Ticon) then
fbmp.Assign(Fpicture.Graphic)
else
begin
fbmp.Width := Fpicture.Icon.Width;
fbmp.Height := Fpicture.Icon.Height;
fbmp.Canvas.Draw(0, 0, Fpicture.Icon);
end;
Table2.Edit;
Table2.FieldByName('bmp').Assign(fbmp);
Table2.post;
Fpicture.Free;
fbmp.Free;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -