有关报表生成器的难点问题(200分)

  • 主题发起人 主题发起人 石光亮
  • 开始时间 开始时间

石光亮

Unregistered / Unconfirmed
GUEST, unregistred user!
我想自己做个报表生成器,要求根据用户选择的字段,能够自动生成报表,问题是我不知道<br>如何自动画表格,请各位高手指教!!!
 
就是画线不知道该怎么画,如果可以参照EXCEL也可以<br>帮帮忙吧!
 
我想用excel来实现动态制作报表,用户选择好要打印得字段后,报表自动生成,要求报表<br>得字段可能是多层得,各位大侠给点意见吧!
 
无论如何需要做成Wizard形式的.<br>最好是做成相Crystal Report形式拖放字段,不过有效性检验是个麻烦事情.
 
用excel生成报表,表的名称像“ 行业用电分类表 ”<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2000年12月<br>如何实现呢? &nbsp;
 
请问有没有关于用excel动态生成报表的例子阿,或者自己画线生成报表的例子<br>我都急坏了,我可以再加200分的!
 
以下是本人做的一个自定义查询(可自定义查询SQL,动态参数,字段格式,字段汇总方式),自动生成报表(可自定义报表的纸张,边距,ColumnsCount,是否分组等)<br>的部分代码,看是否有邦助。(自定义项目以数据表方式保存,可在界面中调用,提供为可任意扩展的功能)<br>//////////////////////////////////////////////////////////<br>unit fFreeQuery;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; ExtCtrls, ComCtrls, ToolWin, Grids, DBGrids, Db, DBTables, ImgList,<br>&nbsp; StdCtrls, DBCtrls, Buttons, QuickRpt, Qrctrls, Printers;<br><br>type<br>&nbsp; TFreeQuery = class(TForm)<br>&nbsp; &nbsp; Query1: TQuery;<br>&nbsp; &nbsp; StatusBar1: TStatusBar;<br>&nbsp; &nbsp; CoolBar1: TCoolBar;<br>&nbsp; &nbsp; ToolBar1: TToolBar;<br>&nbsp; &nbsp; ToolButton1: TToolButton;<br>&nbsp; &nbsp; ToolButton2: TToolButton;<br>&nbsp; &nbsp; ToolButton3: TToolButton;<br>&nbsp; &nbsp; ToolButton4: TToolButton;<br>&nbsp; &nbsp; ToolBar2: TToolBar;<br>&nbsp; &nbsp; ComboBox1: TComboBox;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; ToolButton5: TToolButton;<br>&nbsp; &nbsp; GroupBox1: TGroupBox;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; SpeedButton1: TSpeedButton;<br>&nbsp; &nbsp; ToolButton6: TToolButton;<br>&nbsp; &nbsp; QuickRep1: TQuickRep;<br>&nbsp; &nbsp; QRBand1: TQRBand;<br>&nbsp; &nbsp; QR_title: TQRLabel;<br>&nbsp; &nbsp; QRBand2: TQRBand;<br>&nbsp; &nbsp; QRSysData2: TQRSysData;<br>&nbsp; &nbsp; QRBand3: TQRBand;<br>&nbsp; &nbsp; QRBand4: TQRBand;<br>&nbsp; &nbsp; ToolButton7: TToolButton;<br>&nbsp; &nbsp; ToolButton8: TToolButton;<br>&nbsp; &nbsp; DataSource1: TDataSource;<br>&nbsp; &nbsp; QRBand5: TQRBand;<br>&nbsp; &nbsp; QRLabel1: TQRLabel;<br>&nbsp; &nbsp; QRLabStat: TQRLabel;<br>&nbsp; &nbsp; QR_SubTitle: TQRLabel;<br>&nbsp; &nbsp; ToolButton9: TToolButton;<br>&nbsp; &nbsp; QRGroup1: TQRGroup;<br>&nbsp; &nbsp; QRGroupField: TQRDBText;<br>&nbsp; &nbsp; QRDBText1: TQRDBText;<br>&nbsp; &nbsp; QRLabel2: TQRLabel;<br>&nbsp; &nbsp; DBGrid1: TDBGrid;<br>&nbsp; &nbsp; QRLabel3: TQRLabel;<br>&nbsp; &nbsp; QRLabel4: TQRLabel;<br>&nbsp; &nbsp; QRLabel5: TQRLabel;<br>&nbsp; &nbsp; procedure ToolButton4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure ToolButton1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure SpeedButton1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure ComboBox1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure ToolButton2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure QR_titlePrint(sender: TObject; var Value: String);<br>&nbsp; &nbsp; procedure ToolButton3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure ToolButton7Click(Sender: TObject);<br>&nbsp; &nbsp; procedure QuickRep1BeforePrint(Sender: TCustomQuickRep;<br>&nbsp; &nbsp; &nbsp; var PrintReport: Boolean);<br>&nbsp; &nbsp; procedure QR_SubTitlePrint(sender: TObject; var Value: String);<br>&nbsp; &nbsp; procedure ToolButton9Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormDestroy(Sender: TObject);<br>&nbsp; &nbsp; procedure Query1AfterClose(DataSet: TDataSet);<br>&nbsp; &nbsp; procedure QRLabel4Print(sender: TObject; var Value: String);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; Para_Label: array[0..4] of TLabel;<br>&nbsp; &nbsp; para_edit: array[0..4] of TEdit;<br>&nbsp; &nbsp; para_type: array[0..4] of string;<br><br>&nbsp; &nbsp; StatFieldCount: integer; &nbsp; &nbsp; &nbsp; &nbsp;//汇总字段数变量<br>&nbsp; &nbsp; XLApp: Variant;<br><br>&nbsp; &nbsp; procedure ExecQuery;<br>&nbsp; &nbsp; procedure SelfDefineControl_Free();<br>&nbsp; &nbsp; procedure ParamsGroupBoxPosition;<br>&nbsp; &nbsp; function GetWidth(N: SmallInt): SmallInt;<br>&nbsp; &nbsp; Function Statistics: String;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; FreeQuery: TFreeQuery;<br><br>implementation<br><br>uses ComObj, fFreeQueryEdit, fFreeQuery_DM, ffind, fPublic_ImageList, func_Public,<br>&nbsp; XLConst;<br><br>{$R *.DFM}<br>////////////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ParamsGroupBox_Position<br>////////////////////////////////////////////////////////////<br>procedure TFreeQuery.ParamsGroupBoxPosition;<br>begin<br>GroupBox1.Left := (Width - GroupBox1.Width) div 2;<br>GroupBox1.Top := (Height - GroupBox1.Height) div 2;<br>end;<br>////////////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Create &amp; Close<br>////////////////////////////////////////////////////////////<br>procedure TFreeQuery.FormCreate(Sender: TObject);<br>begin<br>with FreeQuery_DM.Query_master do begin &nbsp; //写入查询名称列表<br>Open;<br>First;<br>combobox1.items.clear;<br>while not eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; combobox1.Items.add(FieldByName('Queryname').asstring);<br>&nbsp; &nbsp; next;<br>&nbsp; &nbsp; end; &nbsp; &nbsp;//end while<br>end; &nbsp; &nbsp;//end with<br>Quickrep1.Visible := False;<br>SpeedButton1.Height := 24;<br>FreeQuery_DM.Query_Parameter.open;<br>FreeQuery_DM.Query_DisplayFormat.open;<br>FreeQuery_DM.Query_ReportSet.open;<br>end;<br><br>procedure TFreeQuery.ToolButton4Click(Sender: TObject);<br>begin<br>Close;<br>end;<br><br>procedure TFreeQuery.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>FreeQuery_DM.Query_DisplayFormat.Close;<br>FreeQuery_DM.Query_Parameter.close;<br>FreeQuery_DM.Query_ReportSet.Close;<br>FreeQuery_DM.Query_Master.close;<br>SelfDefineControl_Free; &nbsp; &nbsp; &nbsp; &nbsp;//释放动态定义控件<br>Query1.close;<br>if Query1.Prepared then Query1.UnPrepare;<br>end;<br>////////////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;自定义 执行处理<br>////////////////////////////////////////////////////////////<br>procedure TFreeQuery.ExecQuery;<br>var tmpSQL: string;<br>&nbsp; &nbsp; PCount,Fcount,i: SmallInt;<br>begin<br>with FreeQuery_DM do begin<br>Query1.close;<br>Query1.SQL.clear;<br>tmpSQL := Query_master.FieldByName('Querystring').asstring;<br>Query1.SQL.add(tmpSQL);<br>try<br>//******************* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 检测参数并赋值<br>PCount := Query1.params.Count;<br>if PCount &gt; 0 then<br>&nbsp; &nbsp; for i:=0 to PCount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; if (para_type='String') or (para_type='DateTime') then Query1.params.Value := para_edit.text<br>&nbsp; &nbsp; &nbsp; &nbsp; else Query1.params.Value := para_edit.text;<br>//******************** &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;执行SQL<br>Query1.open;<br>except<br>&nbsp; &nbsp; messagedlg('查询SQL语句可能存在错误,不能完成本次查询&amp;统计!请检查SQL语句或与系统管理员联系。',mterror,[mbok],0);<br>end; &nbsp; &nbsp;//end try;<br>//******************* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 检测显示格式<br>Fcount := Query_DisplayFormat.RecordCount;<br>if Fcount &gt; 0 then<br>&nbsp; begin<br>&nbsp; Query_DisplayFormat.First;<br>&nbsp; while not Query_DisplayFormat.Eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; DBGrid1.Columns[Query_DisplayFormat['NO']].Title.Caption := Query_DisplayFormat.FieldByName('dispname').asstring;<br>&nbsp; &nbsp; DBGrid1.Columns[Query_DisplayFormat['NO']].Width := Query_DisplayFormat.FieldByName('dispwidth').asinteger;<br>&nbsp; &nbsp; DBGrid1.Columns[Query_DisplayFormat['NO']].Visible := Query_DisplayFormat.FieldByName('dispVisible').value;<br>&nbsp; &nbsp; if Query_DisplayFormat['dispformat'] &lt;&gt; Null &nbsp;then<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; if (Query1.Fields[Query_DisplayFormat['NO']] is TFloatField) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Query1.Fields[Query_DisplayFormat['NO']] as TFloatField).DisplayFormat := Query_DisplayFormat.FieldByName('dispformat').AsString<br>&nbsp; &nbsp; &nbsp; &nbsp; else if (Query1.Fields[Query_DisplayFormat['NO']] is TDateTimeField) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Query1.Fields[Query_DisplayFormat['NO']] as TDateTimeField).DisplayFormat := Query_DisplayFormat.FieldByName('dispformat').AsString;<br>&nbsp; &nbsp; &nbsp; except Messagedlg('查询的显示格式'+Query_DisplayFormat.FieldByName('NO').AsString+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '设置错误!',mtError,[mbok],0);<br>&nbsp; &nbsp; &nbsp; end; &nbsp;//end try<br>&nbsp; &nbsp; Query_DisplayFormat.Next;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end; &nbsp; &nbsp;//end with<br>end;<br><br>////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;自定义计算表列宽函数<br>////////////////////////////////////////////////////<br>function TFreeQuery.GetWidth(N: SmallInt): SmallInt;<br>var sumwidth,i,ColCount: SmallInt;<br>begin<br>sumwidth := 0;<br>Colcount := DBGrid1.columns.count;<br>{计算DBGrid之可视字段的总宽度:sumwidth}<br>for i:=0 to Colcount-1 do<br>&nbsp; &nbsp; if DBGrid1.Columns.Visible then sumwidth := sumwidth + DBGrid1.Columns.Width;<br>{按比例计算当前字段的报表宽度}<br>Result := Trunc((QRBand1.Width-Colcount)/FreeQuery_DM.Query_ReportSet['PageColumnsNumber']*DBGrid1.columns[N].Width/sumwidth)<br>end;<br>/////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;释放动态定义控件处理<br>/////////////////////////////////////////////<br>procedure TFreeQuery.SelfDefineControl_Free;<br>var i: SmallInt;<br>begin<br>for i:=0 to 4 do<br>&nbsp; &nbsp; begin Para_Label.Free; &nbsp; para_edit.free; end;<br>end;<br>//////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;自定义统计处理<br>//////////////////////////////////////////////////////<br>Function TFreeQuery.Statistics: String;<br>var DA_FieldIndex: array of integer;//动态汇总字段index数组<br>&nbsp; &nbsp; DA_FieldName: array of string; &nbsp;//动态汇总字段Name数组<br>&nbsp; &nbsp; DA_DispFormat: array of string; //动态汇总字段数据显示格式<br>&nbsp; &nbsp; DA_Sum: array of double; &nbsp; &nbsp; &nbsp; &nbsp;//动态累加器数组<br>&nbsp; &nbsp; DA_Function: Array of string; &nbsp; //动态汇总函数数组<br>&nbsp; &nbsp; i,k: integer;<br>&nbsp; &nbsp; RCount: integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//查询数据记录数<br>&nbsp; &nbsp; StatInfoStr: String;<br>begin<br>StatFieldCount := 0;<br>with FreeQuery_DM.Query_DisplayFormat do begin<br>//获取汇总字段数 (StatFieldCount)<br>First;<br>while not Eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; if FieldByName('StatYesNo').value then inc(StatFieldCount);<br>&nbsp; &nbsp; next;<br>&nbsp; &nbsp; end;<br>//判断是否可汇总<br>if StatFieldCount = 0 then result := ''<br>else begin<br>//设置动态数组长度<br>SetLength(DA_FieldIndex,StatFieldCount);<br>SetLength(DA_FieldName,StatFieldCount);<br>SetLength(DA_Sum,StatFieldCount);<br>SetLength(DA_Function,StatFieldCount);<br>SetLength(DA_DispFormat,StatFieldCount);<br>//获取汇总字段的Index (DA_FieldIndex)<br>RecordCount;<br>First;<br>k := 0;<br>for i:=0 to RecordCount-1 do<br>&nbsp; begin<br>&nbsp; if FieldByName('StatYesNo').value then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; DA_FieldIndex[k] := FieldByName('NO').Value;<br>&nbsp; &nbsp; DA_FieldName[k] := FieldByName('DispName').AsString;<br>&nbsp; &nbsp; DA_Function[k] := UpperCase(FieldByName('StatFunction').AsString);<br>&nbsp; &nbsp; DA_DispFormat[k] := FieldByName('DispFormat').AsString;<br>&nbsp; &nbsp; inc(k);<br>&nbsp; &nbsp; end;<br>&nbsp; next;<br>&nbsp; end;<br>end; &nbsp; &nbsp;//end with<br>//进行汇总处理<br>with Query1 do begin<br>DisableControls;<br>RCount := RecordCount;<br>First;<br>while Not Eof do<br>&nbsp; begin<br>&nbsp; for i:=0 to StatFieldCount-1 do<br>&nbsp; &nbsp; if DA_Function &lt;&gt; 'COUNT' then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; DA_Sum := DA_Sum + Fields[DA_FieldIndex].asfloat;<br>&nbsp; &nbsp; &nbsp; end; &nbsp; &nbsp;//end for<br>&nbsp; Next;<br>&nbsp; end; &nbsp; &nbsp;//end while<br>EnableControls;<br>end; &nbsp; &nbsp;//end with<br>//进行汇总显示<br>for i:=0 to StatFieldCount-1 do<br>&nbsp; begin<br>&nbsp; if DA_Function='SUM' then StatInfoStr := StatInfoStr+DA_FieldName+'('+DA_Function+') : '+FormatFloat(DA_DispFormat,DA_Sum)+#13+#13;<br>&nbsp; if DA_Function='COUNT' then StatInfoStr := StatInfoStr+DA_FieldName+'('+DA_Function+') : '+IntToStr(RCount)+#13+#13;<br>&nbsp; if DA_Function='AVG' then StatInfoStr := StatInfoStr+DA_FieldName+'('+DA_Function+') : '+FormatFloat(DA_DispFormat,DA_Sum/RCount)+#13+#13;<br>&nbsp; end; &nbsp; &nbsp;//end case<br>Result := StatInfoStr; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//返回值<br>end;<br>end;<br>//===================================================================<br>procedure TFreeQuery.ToolButton1Click(Sender: TObject);<br>var FreeQueryEdit: TFreeQueryEdit;<br>begin<br>if TmpUser = 'SYSTEM' then<br>begin<br>FreeQueryEdit := TFreeQueryEdit.Create(self);<br>FreeQueryEdit.showmodal;<br>FreeQueryEdit.free;<br>end<br>else Messagedlg('本功能只有系统管理员才能使用,您无权使用!',mtwarning,[mbOK],0);<br>end;<br>////////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;执行查询处理<br>////////////////////////////////////////////////////////<br>procedure TFreeQuery.SpeedButton1Click(Sender: TObject);<br>var PCount,i: SmallInt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//PCount 动态参数数量<br>begin<br>PCount := FreeQuery_DM.Query_parameter.RecordCount;<br>FreeQuery_DM.Query_parameter.First;<br>if PCount = 0 then ExecQuery<br>else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; SelfDefineControl_Free; &nbsp; &nbsp; &nbsp; &nbsp; //释放动态定义控件,重新定义<br>&nbsp; &nbsp; for i:=0 to PCount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Para_Label := TLabel.create(self);<br>&nbsp; &nbsp; &nbsp; &nbsp; Para_Label.Parent := Groupbox1;<br>&nbsp; &nbsp; &nbsp; &nbsp; Para_Label.Height := 15;<br>&nbsp; &nbsp; &nbsp; &nbsp; Para_Label.Width := 90;<br>&nbsp; &nbsp; &nbsp; &nbsp; Para_Label.Left := 40;<br>&nbsp; &nbsp; &nbsp; &nbsp; Para_Label.Top := 35 + 32*i;<br>&nbsp; &nbsp; &nbsp; &nbsp; Para_Label.Caption := FreeQuery_DM.Query_parameter['parameterDes'];<br><br>&nbsp; &nbsp; &nbsp; &nbsp; para_type := FreeQuery_DM.Query_parameter['parametertype'];<br><br>&nbsp; &nbsp; &nbsp; &nbsp; para_edit := TEdit.create(self);<br>&nbsp; &nbsp; &nbsp; &nbsp; para_edit.Parent := Groupbox1;<br>&nbsp; &nbsp; &nbsp; &nbsp; para_edit.Height := 23;<br>&nbsp; &nbsp; &nbsp; &nbsp; para_edit.Width := 110;<br>&nbsp; &nbsp; &nbsp; &nbsp; para_edit.Left := 140;<br>&nbsp; &nbsp; &nbsp; &nbsp; para_edit.Top := 32 + 31*i;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; FreeQuery_DM.Query_parameter.next;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; Button1.Top := 76 + 32*(PCount-1);<br>&nbsp; &nbsp; Button2.Top := 76 + 32*(PCount-1);<br>&nbsp; &nbsp; GroupBox1.Height := 121 + 32*(PCount-1);<br>&nbsp; &nbsp; ParamsGroupBoxPosition;<br>&nbsp; &nbsp; groupbox1.Visible := true;<br>&nbsp; &nbsp; end;<br>end;<br>/////////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;处理StatusBar提示文字<br>/////////////////////////////////////////////////////////<br>procedure TFreeQuery.ComboBox1Click(Sender: TObject);<br>begin<br>FreeQuery_DM.Query_master.FindKey([combobox1.text]);<br>statusbar1.Panels[1].Text := FreeQuery_DM.Query_master.FieldByName('QueryDes').asstring;<br>end;<br><br>procedure TFreeQuery.Button2Click(Sender: TObject);<br>begin<br>GroupBox1.Visible := false;<br>end;<br><br>procedure TFreeQuery.Button1Click(Sender: TObject);<br>begin<br>GroupBox1.Visible := false;<br>ExecQuery;<br>end;<br><br>procedure TFreeQuery.ToolButton2Click(Sender: TObject);<br>var find: Tfind;<br>begin<br>find := Tfind.create(self);<br>find.finDataSet := DBGrid1.DataSource.DataSet;<br>find.showmodal;<br>find.free;<br>end;<br>/////////////////////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;打印相关处理<br>/////////////////////////////////////////////////////////////////////<br>procedure TFreeQuery.QR_titlePrint(sender: TObject; var Value: String);<br>begin<br>value := combobox1.text;<br>end;<br><br>procedure TFreeQuery.ToolButton3Click(Sender: TObject);<br>/////**************<br>var FC &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: SmallInt; &nbsp; &nbsp; &nbsp; //fieldcount<br>&nbsp; &nbsp; ColumnWidth : Integer; &nbsp; &nbsp; &nbsp; &nbsp;//列宽变量<br>&nbsp; &nbsp; i, pos &nbsp; : integer;<br>&nbsp; &nbsp; Headers &nbsp; &nbsp; : array of TQRLabel; &nbsp; &nbsp; &nbsp; &nbsp;//表头数组<br>&nbsp; &nbsp; Fields &nbsp; &nbsp; &nbsp;: array of TQRDBText; &nbsp; &nbsp; &nbsp; &nbsp;//明细变量<br>&nbsp; &nbsp; ShapesHeader: array of TQRShape; &nbsp; //表头框数组<br>&nbsp; &nbsp; ShapesDetail: array of TQRShape; &nbsp; //明细框数组<br>begin<br>{设置纸张类型/布局 and 报表分组项目}<br>With Quickrep1 do begin<br>&nbsp; &nbsp; with FreeQuery_DM do begin<br>&nbsp; &nbsp; {纸张类型/布局}<br>&nbsp; &nbsp; if Query_ReportSet['PageOrientation'] = 'poPortrait' then<br>&nbsp; &nbsp; &nbsp; &nbsp; Page.Orientation := poPortrait<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; Page.Orientation := poLandscape;<br>&nbsp; &nbsp; Page.Length := Query_ReportSet['PageHeigth'];<br>&nbsp; &nbsp; Page.Width := Query_ReportSet['PageWidth'];<br>&nbsp; &nbsp; Page.TopMargin := Query_ReportSet['PageMarginsTop'];<br>&nbsp; &nbsp; Page.BottomMargin := Query_ReportSet['PageMarginsBottom'];<br>&nbsp; &nbsp; Page.LeftMargin := Query_ReportSet['PageMarginsLeft'];<br>&nbsp; &nbsp; Page.RightMargin := Query_ReportSet['PageMarginsRigth'];<br>&nbsp; &nbsp; Page.Columns := Query_ReportSet['PageColumnsNumber'];<br>&nbsp; &nbsp; {报表分组项目}<br>&nbsp; &nbsp; if Query_ReportSet['GroupYN'] then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; QRBand3.Enabled := False;<br>&nbsp; &nbsp; &nbsp; &nbsp; QRGroup1.Enabled := True;<br>&nbsp; &nbsp; &nbsp; &nbsp; QRGroup1.Expression := Query_ReportSet['GroupExpression'];<br>&nbsp; &nbsp; &nbsp; &nbsp; QRGroupField.DataField := Query_ReportSet['GroupDisplayField'];<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; QRBand3.Enabled := True;<br>&nbsp; &nbsp; &nbsp; &nbsp; QRGroup1.Enabled := False;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end; &nbsp; &nbsp;//end with<br>end; &nbsp; &nbsp;//end with<br>{以下根据Query1生成报表格式}<br>FC := DBGrid1.Columns.Count;<br>SetLength(Headers,fc);<br>SetLength(Fields,fc);<br>SetLength(ShapesHeader,fc);<br>SetLength(ShapesDetail,fc);<br>pos := 0;<br>for i:=0 to fc-1 do<br>&nbsp; &nbsp; if DBGrid1.Columns.Visible then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; ShapesHeader := TQRShape.create(self);<br>&nbsp; &nbsp; ShapesDetail := TQRShape.create(self);<br>&nbsp; &nbsp; Headers := TQRLabel.create(self);<br>&nbsp; &nbsp; Fields := TQRDBText.create(self);<br><br>&nbsp; &nbsp; if FreeQuery_DM.Query_ReportSet['GroupYN'] then &nbsp;//如果分组<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Headers.parent := QRGroup1;<br>&nbsp; &nbsp; &nbsp; &nbsp; Headers.Top := 8+25;<br>&nbsp; &nbsp; &nbsp; &nbsp; ShapesHeader.parent := QRGroup1;<br>&nbsp; &nbsp; &nbsp; &nbsp; ShapesHeader.Top := 0+25;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Headers.parent := QRBand3;<br>&nbsp; &nbsp; &nbsp; &nbsp; Headers.Top := 8;<br>&nbsp; &nbsp; &nbsp; &nbsp; ShapesHeader.parent := QRBand3;<br>&nbsp; &nbsp; &nbsp; &nbsp; ShapesHeader.Top := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; ShapesDetail.parent := QRBand4;<br>&nbsp; &nbsp; Fields.parent := QRBand4;<br>&nbsp; &nbsp; ShapesDetail.Top := -1;<br>&nbsp; &nbsp; Fields.Top := 8;<br><br>&nbsp; &nbsp; ShapesHeader.height := ShapesHeader.Parent.Height + 1;<br>&nbsp; &nbsp; ShapesDetail.height := ShapesDetail.Parent.Height + 1;<br><br>&nbsp; &nbsp; ShapesHeader.left := pos;<br>&nbsp; &nbsp; ShapesDetail.left := pos;<br>&nbsp; &nbsp; Headers.left := pos + 2;<br>&nbsp; &nbsp; Fields.left := pos + 2;<br><br>&nbsp; &nbsp; ColumnWidth := GetWidth(i);<br>&nbsp; &nbsp; ShapesHeader.width := ColumnWidth;<br>&nbsp; &nbsp; ShapesDetail.width := ColumnWidth;<br>&nbsp; &nbsp; Headers.width := ColumnWidth - 2;<br>&nbsp; &nbsp; Fields.width := ColumnWidth - 2;<br><br>&nbsp; &nbsp; Headers.Bringtofront;<br>&nbsp; &nbsp; Fields.Bringtofront;<br><br>&nbsp; &nbsp; Headers.Caption := DBGrid1.columns.Title.Caption;<br>&nbsp; &nbsp; Fields.DataSet := query1;<br>&nbsp; &nbsp; Fields.datafield := DBGrid1.columns.FieldName;<br>&nbsp; &nbsp; pos := pos + ColumnWidth - 1;<br>&nbsp; &nbsp; end; &nbsp; &nbsp;//end for/if<br>//-------------------------以下打印报表<br>QuickRep1.Print;<br>//-------------------------打印完毕后,需释放动态建交的组件<br>for i:=0 to DBGrid1.columns.Count-1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; Headers.Free;<br>&nbsp; &nbsp; Fields.Free;<br>&nbsp; &nbsp; ShapesHeader.free;<br>&nbsp; &nbsp; ShapesDetail.Free;<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TFreeQuery.ToolButton7Click(Sender: TObject);<br>var TmpStr: string;<br>begin<br>TmpStr := Statistics;<br>MessageDlg('★★★汇总信息★★★'+#13+#13+TmpStr,mtInformation,[mbok],0);<br>end;<br><br>procedure TFreeQuery.QuickRep1BeforePrint(Sender: TCustomQuickRep;<br>&nbsp; var PrintReport: Boolean);<br>var Tmpstr: String;<br>begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//写统计信息<br>TmpStr := Statistics;<br>if TmpStr = '' then QRLabStat.Parent.Enabled := false<br>else begin<br>&nbsp; &nbsp; QRLabStat.Parent.Enabled := true;<br>&nbsp; &nbsp; QRLabStat.Caption := TmpStr;<br>&nbsp; &nbsp; QRLabStat.Parent.Height := QRLabStat.Height;<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TFreeQuery.QR_SubTitlePrint(sender: TObject; var Value: String);<br>var PCount,i: integer;<br>begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//写副标题<br>value := '';<br>PCount := Query1.params.Count;<br>if PCount &gt; 0 then<br>&nbsp; &nbsp; for i:=0 to PCount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; value := value+' '+Para_Label.Caption+': '+para_edit.text+' '<br>end;<br>////////////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;导出数据TO EXCEL处理<br>////////////////////////////////////////////////////////<br>procedure TFreeQuery.ToolButton9Click(Sender: TObject);<br>var RecordCount, FieldCount, Col, Row, i: Integer;<br>&nbsp; &nbsp; Sheet: Variant;<br>&nbsp; &nbsp; EX_DisplayFormat: Array of String; &nbsp; &nbsp; &nbsp;//各字段显示格式数组<br>begin<br>XLApp:= CreateOleObject('Excel.Application');<br>XLApp.Workbooks.Add(xlWBatWorkSheet);<br>XLApp.Workbooks[1].Sheets.add(,,1,xlworksheet);<br>XLApp.Workbooks[1].Sheets[1].Name := ComBoBox1.Text;<br>//*****导出数据<br>Query1.First;<br>Sheet := XLApp.Workbooks[1].Sheets[1];<br>RecordCount := Query1.RecordCount;<br>FieldCount := DBGrid1.Columns.Count;<br><br>With FreeQuery_DM.Query_DisplayFormat do<br>&nbsp; begin &nbsp; &nbsp; &nbsp;//各字段显示格式数组<br>&nbsp; SetLength(EX_DisplayFormat,Query1.FieldCount);<br>&nbsp; First;<br>&nbsp; for i:=0 to Recordcount-1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; EX_DisplayFormat[FieldByName('NO').AsInteger] := FieldByName('DispFormat').AsString;<br>&nbsp; &nbsp; &nbsp; Next;<br>&nbsp; &nbsp; end;<br>&nbsp; end; &nbsp; &nbsp;//end with<br><br>for Col:=1 to FieldCount do Sheet.Cells[1,Col] := DBGrid1.Columns[Col-1].Title.Caption; &nbsp; &nbsp; //写入列标题<br>Query1.DisableControls; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//写入数据<br>for Row:=1 to RecordCount do<br>&nbsp; begin<br>&nbsp; &nbsp; for Col:=1 to FieldCount do<br>&nbsp; &nbsp; &nbsp; if Pos('#',EX_DisplayFormat[Col-1])+Pos('0',EX_DisplayFormat[Col-1])+Pos('%',EX_DisplayFormat[Col-1]) &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; //如果格式符中包含有'#','0','%',则认为是数值型数据<br>&nbsp; &nbsp; &nbsp; &nbsp; Sheet.Cells[Row+1, Col] := FormatFloat(EX_DisplayFormat[Col-1],Query1.FieldByName(DBGrid1.Columns[Col-1].FieldName).AsFloat)<br>&nbsp; &nbsp; &nbsp; else if Pos('Y',EX_DisplayFormat[Col-1])+Pos('M',EX_DisplayFormat[Col-1])+Pos('D',EX_DisplayFormat[Col-1]) &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; //如果格式符中包含有'Y','M','D',则认为是日期型数据<br>&nbsp; &nbsp; &nbsp; &nbsp; Sheet.Cells[Row+1, Col] := FormatDateTime(EX_DisplayFormat[Col-1],Query1.FieldByName(DBGrid1.Columns[Col-1].FieldName).AsDateTime)<br>&nbsp; &nbsp; &nbsp; else Sheet.Cells[Row+1, Col] := Query1.FieldByName(DBGrid1.Columns[Col-1].FieldName).AsString;<br>&nbsp; &nbsp; Query1.Next;<br>&nbsp; end;<br>Query1.EnableControls;<br><br>for Col:=1 to FieldCount do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//调整电子表格中各列的列宽<br>&nbsp; &nbsp; Sheet.Columns[Col].ColumnWidth := DBGrid1.Columns[Col-1].Width div 7;<br>XLApp.Visible := True;<br>end;<br>//////////////////////////////////////////////////<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;释放XLAPP对象<br>//////////////////////////////////////////////////<br>procedure TFreeQuery.FormDestroy(Sender: TObject);<br>begin<br>if not VarIsEmpty(XLApp) then begin<br>&nbsp; &nbsp; XLApp.DisplayAlerts := False; &nbsp;// Discard unsaved files....<br>&nbsp; &nbsp; XLApp.Quit;<br>end;<br>end;<br><br>procedure TFreeQuery.Query1AfterClose(DataSet: TDataSet);<br>begin<br>FormDestroy(DataSet);<br>end;<br><br>procedure TFreeQuery.QRLabel4Print(sender: TObject; var Value: String);<br>begin<br>Value := TmpUser;<br>end;<br><br>end.<br><br>
 
可现在老板要我自己画表格,即不使用这些控件,各位给我点思路吧,要求动态生成报表,<br>而且是行标题和竖标题都是带分组的,我不知道该建立那些类,该如何实现!
 

Similar threads

D
回复
0
查看
930
DelphiTeacher的专栏
D
D
回复
0
查看
871
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
776
DelphiTeacher的专栏
D
D
回复
0
查看
945
DelphiTeacher的专栏
D
后退
顶部