转载自:http://t.zoukankan.com/hnxxcxg-p-2940801.html
以下代碼在D7+ACCESS+FastReport 3.15版中測試通過.
1.將FastReport存入數據庫中:
在窗體的"Insert"按鈕的OnClick事件中插入下面代碼:
(Insert_rep: TADOQuery.設置SQL語句:select * from reportdata.
注:ReportData為放置數據庫的表,其中有字段ID(標志),REPORT(報表內容))
procedure TForm1.Button4Click(Sender: TObject);
var
template : TStream;
begin
template := TMemoryStream.Create;
template.Position := 0;
frxReport1.SaveToStream(template);
Insert_rep.Insert;
try
Insert_rep.Active := true;
Insert_rep.Edit;
Insert_rep.DisableControls;
Insert_rep.FieldByName('rptname').AsString := ExtractFileName(OpenDialog1.FileName);//報表名稱,作為報表數據的標志
(Insert_rep.FieldByName('rptdata') as TBlobField).LoadFromStream(template);
Insert_rep.FieldByName('uptime').AsDateTime := now;
Insert_rep.Append;
ShowMessage('Insert OK!');
finally
Insert_rep.EnableControls;
template.Free;
end;
end;
2.將報表從數據庫中讀取出來:
在Button的OnClick事件中添加下面代碼:
(qr_report: ADOQuery)
procedure TForm1.Button4Click(Sender: TObject);
var
template : TStream;
begin
try
g_filename := ...;//報表名稱
qr_report.Close;
qr_report.SQL.Clear;
qr_report.SQL.Add('select * from reportdata where id =:id');
qr_report.Parameters.ParamByName('id').Value := g_filename;
qr_report.Open;
try
template := qr_report.CreateBlobStream(qr_report.FieldByName('report'), bmRead);
template.Position := 0;
frxReport1.LoadFromStream(template);
finally
template.Free;
end;
end;
end;
3修改報表,并寫入數據庫.
在frxDesigner1控件的OnSaveReport事件中,加入下面代碼:
(設計報表后,單擊報表中的 "save" 圖標,觸發該事件)
function TForm1.frxDesigner1SaveReport(Report: TfrxReport;
SaveAs: Boolean): Boolean;
var template : TStream;
begin
template := TMemoryStream.Create;
template.Position := 0;
frxReport1.SaveToStream(template);
qr_report.Edit;
try
qr_report.DisableControls;
(qr_report.FieldByName('Report') as TBlobField).LoadFromStream(template);
qr_report.Post;
finally
qr_report.EnableControls;
template.Free;
end;
end