1.打印单条记录(当前记录),frxDataSet 设置RangeBegin和RangeEnd为Current 2.打印选中记录,基本思路:按照数据集过滤格式获取选中行的主键组合字串,过滤数据集,尔后打印。代码可依照如下(未测试) uses StrUtils;const filter:string = 'id in (%s)';//id相当于主键 var i: Integer; tmpStr,sql:string; begin with dbgrd1 do begin for i:=0 to SelectedRows.Count-1 do begin //假设Grid中第1列为主键所在列 tmpStr:=tmpStr+quotedStr(SelectedRows.Items[i][0])+','; end; if AnsiEndsStr(',',tmpStr) then begin tmpStr=AnsiLeftStr(tmpStr,Length(tmpStr)-1); end; DataSet.filtered:=false; DataSet.filter := Format(filter,[tmpStr]); DataSet.filtered:= True; //打印 end; end;
你要在FR的getvalue中作代碼處理,必須得到選中的數據記錄 procedure TRes_Tryuser_F.frxReport_OtherGetValue(const VarName: String; var Value: Variant); var i:Integer; begin inherited; IF VarName='account_id' then begin Value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[0]; fitext:=fitext+ VarToStr(Value)+','; end; if VarName='full_name' then Value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[1]; if VarName='BM' then value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[8]; if VarName='KB' then value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[9]; if VarName='jcrq' then value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]; if VarName='kh1' then begin if VarToStr(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[15])= '' then Value:='V' else Value:=''; end; if VarName='kh2' then begin if VarToStr(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[15])= '' then Value:='' else Value:='V'; end; IF VarName='sxr' then value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[19]; if varname='YY_JCRQ' then Value:=FormatDateTime('YYYY',VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12])); if varname='MM_JCRQ' then Value:=FormatDateTime('MM',VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12])); if varname='DD_JCRQ' then Value:=FormatDateTime('DD',VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12])); if varname='YY' then begin if VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]) >StrToDateTime('2010/10/1') then Value:=FormatDateTime('YYYY',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),6)) else Value:=FormatDateTime('YYYY',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),3)); end; if varname='MM' then begin IF VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]) >StrToDateTime('2010/10/1') then Value:=FormatDateTime('MM',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),6)) else Value:=FormatDateTime('MM',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),3)); end; if varname='DD' then begin IF VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]) >StrToDateTime('2010/10/1') then Value:=FormatDateTime('DD',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),6)) else Value:=FormatDateTime('DD',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),3)); end;end;
我记得fastreport可以在运行期设置报表显示的格式
Procedure TCustomQueryForm.FtRpt0BeforePrint(Sender: TfrxReportComponent); Var AInt, AInt1, AWidths, APaperW: Integer; AColm: TcxGridDBColumn; AFtRpt: TfrxReport; Begin Inherited; If FPrint Then Exit; //FPrint 公用变量,初始化为false AFtRpt := FtRpt1; //一个新TfrxReport控件,放了一个表头,变量'Var_Title' //表尾'Var_Unit',放置了MasterData1和DetailData1 With AFtRpt Do Begin Variables['Var_Title'] := QuotedStr(cxTextEdit511.Text + ' '); //为表头赋值 Variables['Var_Unit'] := QuotedStr(cxTextEdit611.Text)//为表尾赋值 End; APaperW := 1050; //设置报表页面宽度(取得) For AInt := 0 To cxGrid2DBTableView1.VisibleColumnCount - 1 Do Begin //获得view控件总宽度 AWidths := AWidths + cxGrid2DBTableView1.VisibleColumns[AInt].Width;// End; For AInt := 1 To cxGrid2DBTableView1.VisibleColumnCount Do Begin AInt1 := cxGrid2DBTableView1.VisibleColumnCount - AInt; AColm := TcxGridDBColumn(cxGrid2DBTableView1.VisibleColumns[AInt1]); GreateFirMemo((AColm.Width * APaperW) Div AWidths, 16, 'FstMemo111' + inttostr(AInt), AColm.Caption, 'MasterData1'); GreateFirMemo((AColm.Width * APaperW) Div AWidths, 18, 'FstMemo111' + inttostr(AInt), '[FrxDtset_Print."' + AColm.DataBinding.FieldName + '"]', 'DetailData1'); End; FPrint := True; End;Function TCustomQueryForm.GreateFirMemo(Int_Width, Int_Heigth: Integer; Str_Name, Str_Text, Str_Parent: String): Boolean; Var FastMemo: TfrxSysMemoView; AFtRpt: TfrxReport; Begin Result := true; AFtRpt := FtRpt1; Try FastMemo := TfrxSysMemoView.Create(AFtRpt); With FastMemo Do Begin Top := 0; Height := Int_Heigth; Width := Int_Width; Align := baLeft; Text := Str_Text; //设定连接数据库字段 Frame.Typ := [ftLeft, ftRight, ftTop, ftBottom]; //设定显示四个边 StretchMode := smMaxHeight; //设定自动伸缩 Visible := true; Printable := true; ParentFont := True; // If Str_Parent = 'DetailData1' Then Parent := TfrxDetailData(AFtRpt.FindObject(Str_Parent)) Else Parent := TfrxMasterData(AFtRpt.FindObject(Str_Parent)) End; Except Result := false; End; // End;
以前写的,根据grid打印显示列,删除了些必要代码,未测试
if dbgInfo.DataSource.DataSet.RecordCount <> dbgInfo.SelectedRows.Count then begin for i := 0 to dbgInfo.SelectedRows.Count - 1 do begin GotoBook(pointer(dbgInfo.SelectedRows.Items[i])); adrqPrint.Append; adrqPrint.FieldByName('USERID').Value := dbgInfo.Fields[0].AsString; adrqPrint.FieldByName('USERNAME').Value := dbgInfo.Fields[1].AsString; adrqPrint.FieldByName('METERID').Value := dbgInfo.Fields[2].AsString; adrqPrint.FieldByName('AREANAME').Value := dbgInfo.Fields[3].AsString; adrqPrint.FieldByName('HOUSENAME').Value := dbgInfo.Fields[4].AsString; adrqPrint.FieldByName('HOUSENO').Value := dbgInfo.Fields[5].AsString; adrqPrint.FieldByName('TOTALHEAT').Value := dbgInfo.Fields[6].AsFloat; adrqPrint.FieldByName('TOTALHEATMONEY').Value := dbgInfo.Fields[7].AsCurrency; adrqPrint.FieldByName('LASTHEAT').Value := dbgInfo.Fields[8].AsFloat; adrqPrint.FieldByName('CURRENTHEAT').Value := dbgInfo.Fields[9].AsFloat; adrqPrint.FieldByName('TOTALCOLD').Value := dbgInfo.Fields[10].AsFloat; adrqPrint.FieldByName('TOTALCOLDMONEY').Value := dbgInfo.Fields[11].AsCurrency; adrqPrint.FieldByName('CURRENTCOLD').Value := dbgInfo.Fields[12].AsFloat; adrqPrint.FieldByName('LASTCOLD').Value := dbgInfo.Fields[13].AsFloat; adrqPrint.Post; end; frxDBDataset1.DataSet := adrqPrint; end;我现在用这种笨方法把选中的记录加到另外一个数据集,但是一编译就提示GotoBook未定义,请问这是怎么回事,我平时不用这个函数的
2.打印选中记录,基本思路:按照数据集过滤格式获取选中行的主键组合字串,过滤数据集,尔后打印。代码可依照如下(未测试)
uses StrUtils;const filter:string = 'id in (%s)';//id相当于主键
var
i: Integer;
tmpStr,sql:string;
begin
with dbgrd1 do
begin
for i:=0 to SelectedRows.Count-1 do
begin
//假设Grid中第1列为主键所在列
tmpStr:=tmpStr+quotedStr(SelectedRows.Items[i][0])+',';
end;
if AnsiEndsStr(',',tmpStr) then
begin
tmpStr=AnsiLeftStr(tmpStr,Length(tmpStr)-1);
end;
DataSet.filtered:=false;
DataSet.filter := Format(filter,[tmpStr]);
DataSet.filtered:= True;
//打印
end;
end;
procedure TRes_Tryuser_F.frxReport_OtherGetValue(const VarName: String;
var Value: Variant);
var i:Integer;
begin
inherited;
IF VarName='account_id' then
begin
Value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[0];
fitext:=fitext+ VarToStr(Value)+',';
end;
if VarName='full_name' then
Value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[1];
if VarName='BM' then
value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[8];
if VarName='KB' then
value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[9];
if VarName='jcrq' then
value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12];
if VarName='kh1' then
begin
if VarToStr(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[15])= '' then
Value:='V'
else
Value:='';
end;
if VarName='kh2' then
begin
if VarToStr(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[15])= '' then
Value:=''
else
Value:='V';
end;
IF VarName='sxr' then
value:=cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[19];
if varname='YY_JCRQ' then
Value:=FormatDateTime('YYYY',VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]));
if varname='MM_JCRQ' then
Value:=FormatDateTime('MM',VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]));
if varname='DD_JCRQ' then
Value:=FormatDateTime('DD',VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]));
if varname='YY' then
begin
if VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]) >StrToDateTime('2010/10/1') then
Value:=FormatDateTime('YYYY',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),6))
else
Value:=FormatDateTime('YYYY',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),3));
end;
if varname='MM' then
begin
IF VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]) >StrToDateTime('2010/10/1') then
Value:=FormatDateTime('MM',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),6))
else
Value:=FormatDateTime('MM',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),3));
end;
if varname='DD' then
begin
IF VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]) >StrToDateTime('2010/10/1') then
Value:=FormatDateTime('DD',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),6))
else
Value:=FormatDateTime('DD',IncMonth(VarToDateTime(cxGrid2DBBandedTableView1.ViewData.Records[frxDBDataset1.RecNo].Values[12]),3));
end;end;
Var
AInt, AInt1, AWidths, APaperW: Integer;
AColm: TcxGridDBColumn;
AFtRpt: TfrxReport;
Begin
Inherited;
If FPrint Then Exit; //FPrint 公用变量,初始化为false AFtRpt := FtRpt1; //一个新TfrxReport控件,放了一个表头,变量'Var_Title'
//表尾'Var_Unit',放置了MasterData1和DetailData1
With AFtRpt Do
Begin
Variables['Var_Title'] := QuotedStr(cxTextEdit511.Text + ' '); //为表头赋值
Variables['Var_Unit'] := QuotedStr(cxTextEdit611.Text)//为表尾赋值
End; APaperW := 1050; //设置报表页面宽度(取得) For AInt := 0 To cxGrid2DBTableView1.VisibleColumnCount - 1 Do
Begin
//获得view控件总宽度
AWidths := AWidths + cxGrid2DBTableView1.VisibleColumns[AInt].Width;//
End; For AInt := 1 To cxGrid2DBTableView1.VisibleColumnCount Do
Begin
AInt1 := cxGrid2DBTableView1.VisibleColumnCount - AInt;
AColm := TcxGridDBColumn(cxGrid2DBTableView1.VisibleColumns[AInt1]); GreateFirMemo((AColm.Width * APaperW) Div AWidths, 16, 'FstMemo111' + inttostr(AInt),
AColm.Caption, 'MasterData1'); GreateFirMemo((AColm.Width * APaperW) Div AWidths, 18, 'FstMemo111' + inttostr(AInt),
'[FrxDtset_Print."' + AColm.DataBinding.FieldName + '"]', 'DetailData1'); End;
FPrint := True;
End;Function TCustomQueryForm.GreateFirMemo(Int_Width, Int_Heigth: Integer;
Str_Name, Str_Text, Str_Parent: String): Boolean;
Var
FastMemo: TfrxSysMemoView;
AFtRpt: TfrxReport;
Begin
Result := true;
AFtRpt := FtRpt1; Try
FastMemo := TfrxSysMemoView.Create(AFtRpt);
With FastMemo Do
Begin
Top := 0;
Height := Int_Heigth;
Width := Int_Width;
Align := baLeft;
Text := Str_Text; //设定连接数据库字段
Frame.Typ := [ftLeft, ftRight, ftTop, ftBottom]; //设定显示四个边
StretchMode := smMaxHeight; //设定自动伸缩
Visible := true;
Printable := true;
ParentFont := True; //
If Str_Parent = 'DetailData1' Then
Parent := TfrxDetailData(AFtRpt.FindObject(Str_Parent))
Else
Parent := TfrxMasterData(AFtRpt.FindObject(Str_Parent))
End;
Except
Result := false;
End;
//
End;
begin
for i := 0 to dbgInfo.SelectedRows.Count - 1 do
begin
GotoBook(pointer(dbgInfo.SelectedRows.Items[i]));
adrqPrint.Append;
adrqPrint.FieldByName('USERID').Value := dbgInfo.Fields[0].AsString;
adrqPrint.FieldByName('USERNAME').Value := dbgInfo.Fields[1].AsString;
adrqPrint.FieldByName('METERID').Value := dbgInfo.Fields[2].AsString;
adrqPrint.FieldByName('AREANAME').Value := dbgInfo.Fields[3].AsString;
adrqPrint.FieldByName('HOUSENAME').Value := dbgInfo.Fields[4].AsString;
adrqPrint.FieldByName('HOUSENO').Value := dbgInfo.Fields[5].AsString;
adrqPrint.FieldByName('TOTALHEAT').Value := dbgInfo.Fields[6].AsFloat;
adrqPrint.FieldByName('TOTALHEATMONEY').Value := dbgInfo.Fields[7].AsCurrency;
adrqPrint.FieldByName('LASTHEAT').Value := dbgInfo.Fields[8].AsFloat;
adrqPrint.FieldByName('CURRENTHEAT').Value := dbgInfo.Fields[9].AsFloat;
adrqPrint.FieldByName('TOTALCOLD').Value := dbgInfo.Fields[10].AsFloat;
adrqPrint.FieldByName('TOTALCOLDMONEY').Value := dbgInfo.Fields[11].AsCurrency;
adrqPrint.FieldByName('CURRENTCOLD').Value := dbgInfo.Fields[12].AsFloat;
adrqPrint.FieldByName('LASTCOLD').Value := dbgInfo.Fields[13].AsFloat;
adrqPrint.Post;
end; frxDBDataset1.DataSet := adrqPrint;
end;我现在用这种笨方法把选中的记录加到另外一个数据集,但是一编译就提示GotoBook未定义,请问这是怎么回事,我平时不用这个函数的