如何做一个通用的报表打印模块!(100分)

  • 主题发起人 主题发起人 Bkhswrp
  • 开始时间 开始时间
B

Bkhswrp

Unregistered / Unconfirmed
GUEST, unregistred user!
把数据(用Select查询的数据集)传给该模块,它能自动生成报表或者运行的时候动态设计格式、做相关的调整。
听说可以用第三方控件实现,请问用哪一种比较好,(界面应该是全中文的)
 
ehLib 即可
 
别人写的,可以把dbgrid的内容打印出来,根据你的情况改改就行了
const
LeftBlank=1;
//定义页边距,单位厘米
RightBlank=1;
TopBlank=1;
BottomBlank=1;
var
PointX,PointY:integer;
PointScale,PrintStep:integer;
s:string;
x,y:integer;
i:integer;
begin
//获取当前打印机的分辨率
PointX:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54);
PointY:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54);
//根据打印机和屏幕的分辨率计算出从屏幕转换到打印机的比例 
PointScale:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/Screen.PixelsPerInch+0.5);
//横向打印
// printer.Orientation:=poLandscape;
//打印的字体和大小
printer.Canvas.Font.Name:='宋体';
printer.canvas.Font.Size:=10;
//根据字体的大小确定每行的高度
s:='漳州市刑警支队';
PrintStep:=printer.canvas.TextHeight(s)+16;
//打印的起点位置
x:=PointX*LeftBlank;
y:=PointY*TopBlank;
//DataSource1是DBGrid1所连接的数据源
if ((DataSource1.DataSet).Active=true) and ((DataSource1.DataSet).RecordCount>0) then
begin
(DataSource1.DataSet).First;
try
printer.begin
Doc;
printer.Canvas.TextOut((x+DBGrid1.Width*Pointx) div 2-((printer.Canvas.TextWidth(s)-8)*pointscale),y,s);
while not (DataSource1.DataSet).Eofdo
begin
//打印DBGrid中的所有列
for i:=0 to DBGrid1.FieldCount-1do
begin
//假如所要打印的列超出了打印范围,则忽略该列
if (x+DBGrid1.Columns.Items.Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then
begin
//画表格线
//每页的第一行打印表头
Printer.Canvas.Rectangle(x,y,x+DBGrid1.Columns.Items.Width*PointScale,y+PrintStep);
if y=PointY*TopBlank then
Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Columns.Title.Caption)
else
Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Fields.asString);
end;
//计算下一列的横坐标
x:=x+DBGrid1.Columns.Items.Width*PointScale;
end;

if not (y=PointY*TopBlank) then
(DataSource1.DataSet).next;
x:=PointX*LeftBlank;
y:=y+PrintStep;
//换页
if (y+PrintStep)>(Printer.PageHeight-PointY*BottomBlank) then
begin
Printer.NewPage;
y:=PointY*TopBlank;
end;
end;
finally
printer.EndDoc;
end;
(DataSource1.DataSet).First;
Application.MessageBox('打印完成','打印',32);
end;
end;
 
自己编当然是最好的,可是没那个能力,编不好,而且要花很多时间。
还使用控件比较省事[:)]
 
Sorry,这么晚才结分
 
打印一个ADOStoredProc的数据集
unit common_report;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, QuickRpt,adodb, QRCtrls,QRExport,Math,QRPrntr,
printers,TSDBGrid;
const
c_edt_str_width = 80;
// &amp;acute;&amp;reg;±à&amp;frac14;&amp;shy;-&amp;iquest;í&amp;para;&amp;Egrave;
c_edt_str_height = 20;
// &amp;acute;&amp;reg;±à&amp;frac14;&amp;shy;-&amp;cedil;&amp;szlig;&amp;para;&amp;Egrave;
c_edt_dat_width = 40;
// &amp;Ecirc;&amp;yacute;&amp;frac34;&amp;Yacute;±à&amp;frac14;&amp;shy;-&amp;iquest;í&amp;para;&amp;Egrave;
c_edt_dat_height = 20;
// &amp;Ecirc;&amp;yacute;&amp;frac34;&amp;Yacute;±à&amp;frac14;&amp;shy;-&amp;cedil;&amp;szlig;&amp;para;&amp;Egrave;
c_chk_width = 45;
// &amp;frac14;ì&amp;sup2;é&amp;iquest;ò-&amp;iquest;í&amp;para;&amp;Egrave;
c_chk_height = 17;
// &amp;frac14;ì&amp;sup2;é&amp;iquest;ò-&amp;cedil;&amp;szlig;&amp;para;&amp;Egrave;
c_cmb_width = 32;
// ×é&amp;ordm;&amp;Iuml;&amp;iquest;ò-&amp;iquest;í&amp;para;&amp;Egrave;
c_cmb_height = 20;
// ×é&amp;ordm;&amp;Iuml;&amp;iquest;ò-&amp;cedil;&amp;szlig;&amp;para;&amp;Egrave;
c_char_height = 12;
// ±ê&amp;Ccedil;&amp;copy;-&amp;cedil;&amp;szlig;&amp;Iuml;&amp;macr;
c_width_per_char = 6;
// ±ê&amp;Ccedil;&amp;copy;-&amp;Atilde;&amp;iquest;&amp;cedil;&amp;ouml;×&amp;Ouml;·&amp;ucirc;&amp;iquest;í&amp;para;&amp;Egrave;
c_grp_hdr = 8;
// ×é&amp;ordm;&amp;Iuml;&amp;iquest;ò&amp;micro;&amp;Auml;&amp;Iacute;·&amp;cedil;&amp;szlig;
c_margin = 4;
// &amp;Ocirc;&amp;ordf;&amp;Euml;&amp;Oslash;&amp;Auml;&amp;Uacute;&amp;Aacute;&amp;ETH;&amp;frac34;à&amp;ordm;&amp;Iacute;&amp;micro;&amp;yen;&amp;Ocirc;&amp;ordf;&amp;Auml;&amp;Uacute;&amp;ETH;&amp;ETH;&amp;frac34;à
c_ele_gap = 8;
// &amp;Ocirc;&amp;ordf;&amp;Euml;&amp;Oslash;&amp;frac14;&amp;auml;&amp;Aacute;&amp;ETH;&amp;frac34;à
c_unit_gap = 8;
// &amp;micro;&amp;yen;&amp;Ocirc;&amp;ordf;&amp;frac14;&amp;auml;&amp;ETH;&amp;ETH;&amp;frac34;à
c_column_gap = 24;
// &amp;Agrave;&amp;cedil;&amp;frac14;&amp;auml;&amp;frac34;à
c_band_width = 60;
// ±¨±í&amp;Iuml;&amp;icirc;-&amp;iquest;í&amp;para;&amp;Egrave;
c_band_height = 20;
// ±¨±í&amp;Iuml;&amp;icirc;-&amp;cedil;&amp;szlig;&amp;para;&amp;Egrave;
c_band_margin = 5;
// ±¨±í&amp;Auml;&amp;Uacute;&amp;Iuml;&amp;szlig;&amp;Oacute;&amp;euml;&amp;Ecirc;&amp;yacute;&amp;frac34;&amp;Yacute;&amp;micro;&amp;Auml;&amp;frac34;à&amp;Agrave;&amp;euml;
c_page_hor_margin = 10;
// &amp;Ograve;&amp;sup3;±&amp;szlig;&amp;frac34;à-&amp;ordm;á&amp;Iuml;ò
c_page_vert_margin = 10;
// &amp;Ograve;&amp;sup3;±&amp;szlig;&amp;frac34;à-×&amp;Yacute;&amp;Iuml;ò
c_page_width = 210;
// &amp;Ograve;&amp;sup3;-&amp;iquest;í&amp;para;&amp;Egrave;
c_page_height = 297;
// &amp;Ograve;&amp;sup3;-&amp;cedil;&amp;szlig;&amp;para;&amp;Egrave;
c_pixel_per_mm = 23;
// &amp;Atilde;&amp;iquest;&amp;ordm;&amp;Aacute;&amp;Atilde;×&amp;Iuml;ó&amp;Euml;&amp;Oslash;&amp;Ecirc;&amp;yacute;
c_segbar_width = 1;
// ·&amp;Ouml;&amp;cedil;&amp;ocirc;&amp;Iuml;&amp;szlig;&amp;acute;&amp;Ouml;&amp;Iuml;&amp;cedil;
c_report_title = 'none';
// ±¨±í±ê&amp;Igrave;&amp;acirc;
c_file_id = 'pjYan report';
// &amp;Icirc;&amp;Auml;&amp;frac14;&amp;thorn;±ê&amp;Ouml;&amp;frac34;
c_file_name = 'report0';
// &amp;Icirc;&amp;Auml;&amp;frac14;&amp;thorn;&amp;Atilde;&amp;ucirc;
c_file_ext = '.ypj';
// &amp;Icirc;&amp;Auml;&amp;frac14;&amp;thorn;&amp;ordm;ó×&amp;ordm;&amp;Atilde;&amp;ucirc;
type
Tcommon_rep = class(TForm)
c_rep: TQuickRep;
procedure FormCreate(Sender: TObject);
private
c_band:Tqrband;
c_shape:Tqrshape;
c_label:Tqrlabel;
c_dbtext:Tqrdbtext;
m_preview:boolean;
c_sys:Tqrsysdata;
pageset:boolean;
{ Private declarations }
public
procedure buildrepsp(adosp:TADOStoredProc;headtitle:string;reptitle:string);
{ Public declarations }
end;

var
common_rep: Tcommon_rep;
implementation
{$R *.dfm}
procedure Tcommon_rep.buildrepsp(adosp:TADOStoredProc;headtitle:string;reptitle:string);
var
I,j: Integer;
m:integer;
begin
m_preview:=false;
adosp.close;
if reptitle<>'' then
begin
c_band:=Tqrband.Create(self);
c_band.Parent:=c_rep;
c_band.BandType:=rbpageheader;
c_band.Height:=60;
c_label:=Tqrlabel.Create(self);
c_label.Parent:=c_band;
C_label.Font.Size:=24;
c_label.font.Name:='&amp;Euml;&amp;Icirc;&amp;Igrave;&amp;aring;';
c_label.Font.Style:=[fsbold];
c_label.Caption:=reptitle;
c_label.Left:=(c_band.Width-c_label.Width) div 2;
c_label.Top:=(c_band.Height-c_label.Height) div 2;
end;
c_rep.DataSet:=adosp;
if headtitle<>'' then
begin
//&amp;acute;&amp;acute;&amp;frac12;¨±ê&amp;Igrave;&amp;acirc;&amp;Iuml;&amp;Ocirc;&amp;Ecirc;&amp;frac34;&amp;Ccedil;&amp;oslash;
c_band:=Tqrband.Create(self);
c_band.Parent:=c_rep;
c_band.BandType:=rbcolumnheader;
c_band.Height:=30;
c_band.Frame.DrawTop:=true;
c_band.Frame.DrawBottom:=true;
c_band.Frame.DrawLeft:=true;
c_band.Frame.DrawRight:=true;
j:=0;
m:=0;
for I := 0 to length(headtitle)do
// Iterate
begin
if copy(headtitle,i,1)='/' then
begin
c_label:=Tqrlabel.Create(self);
c_label.AutoSize:=false;
c_label.Parent:=c_band;
c_label.Caption:=copy(headtitle,m+1,i-1-m);
c_label.Left:=5+j*60;
c_label.Width:=60;
//c_label.Height:=(c_band.Height-c_label.Height) div 2;
c_label.Top:=(c_band.Height-c_label.Height) div 2;
c_shape:=Tqrshape.Create(self);
c_shape.Parent:=c_band;
c_shape.Width:=1;
c_shape.Height:=c_band.Height;
c_shape.Left:=60*(j+1)+1;
m:=i;
j:=j+1;
//showmessage(c_label.Caption);
end;
end;
// for
end;
adosp.Open;
if adosp.RecordCount>0 then
begin
//&amp;acute;&amp;acute;&amp;frac12;¨&amp;Ecirc;&amp;yacute;&amp;frac34;&amp;Yacute;&amp;Iuml;&amp;Ocirc;&amp;Ecirc;&amp;frac34;&amp;acute;&amp;oslash;&amp;Ccedil;&amp;oslash;
c_band:=Tqrband.Create(self);
c_band.Parent:=c_rep;
c_band.BandType:=rbdetail;
c_band.Height:=18;
//c_band.Frame.DrawTop:=true;
//c_band.Frame.DrawBottom:=true;
c_band.Frame.DrawLeft:=true;
c_band.Frame.DrawRight:=true;
c_shape:=Tqrshape.Create(self);
c_shape.Parent:=c_band;
c_shape.Width:=c_band.Width;
c_shape.Height:=1;
c_shape.Left:=0;
c_shape.Top:=-1;
c_shape:=Tqrshape.Create(self);
c_shape.Parent:=c_band;
c_shape.Width:=c_band.Width;
c_shape.Height:=1;
c_shape.Left:=0;
c_shape.Top:=c_band.Height;
for I := 0 to adosp.FieldCount-1do
// Iterate
begin
c_dbtext:=Tqrdbtext.Create(self);
c_dbtext.AutoSize:=false;
c_dbtext.font.Size:=8;
c_dbtext.font.Name:='&amp;Euml;&amp;Icirc;&amp;Igrave;&amp;aring;';
c_dbtext.Parent:=c_band;
c_dbtext.DataSet:=adosp;
c_dbtext.DataField:=adosp.Fields.FieldName;
c_dbtext.Left:=5+i*60;
c_dbtext.Width:=60;
c_dbtext.top:=(c_band.Height-c_dbtext.Height) div 2;
c_shape:=Tqrshape.Create(self);
c_shape.Parent:=c_band;
c_shape.Width:=1;
c_shape.Height:=c_band.Height;
c_shape.Left:=60*(i+1)+1;
end;
// for
adosp.Close;
//&amp;acute;&amp;acute;&amp;frac12;¨&amp;Ograve;&amp;sup3;&amp;Icirc;&amp;sup2;&amp;Iuml;&amp;Ocirc;&amp;Ecirc;&amp;frac34;&amp;Ograve;&amp;sup3;&amp;Ecirc;&amp;yacute;
c_band:=Tqrband.Create(self);
c_band.Parent:=c_rep;
c_band.BandType:=rbpagefooter;
c_band.Height:=20;
//c_band.Frame.DrawTop:=true;
//c_band.Frame.DrawBottom:=true;
//c_band.Frame.DrawLeft:=true;
//c_band.Frame.DrawRight:=true;
c_sys:=Tqrsysdata.Create(self);
c_sys.Parent:=c_band;
c_sys.Data:=qrsPageNumber;
c_sys.Height:=20;
c_sys.Width:=40;
c_sys.Left:=(c_band.Width-c_sys.Width) ;
c_sys.Top:=(c_band.Height-c_sys.Height) div 2;
c_sys:=Tqrsysdata.Create(self);
c_sys.Parent:=c_band;
c_sys.Data:=qrsdate;
c_sys.Height:=20;
c_sys.Width:=40;
c_sys.Left:=0;
c_sys.Top:=(c_band.Height-c_sys.Height) div 2;
adosp.Open;
c_rep.PreviewModal;
m_preview:=false;
adosp.Close;
end
else
begin
adosp.Close;
end;
end;

end.
 
http://www.51delphi.com/delphi/soft?cx=Fastreport+%B6%AF%CC%AC%C1%AC%BD%D3%BF%E2
Fastreport 动态连接库
http://www.51delphi.com/delphi/soft?cx=KingReport
KingReport
这些都是人家做的通用模版,都带源码的,下回来自己研究一下吧
 
后退
顶部