Hello. Has a task to save prepared reports and open them later in fastReportViewer, Spend 4 days to write this
.fr3 is a model – it doesn’t contains data
.fp3 is a data, prepared report
So, how to save prepared report and open it is later? Below an example
saving and sending on server…
1 2 3 4 5 6 7 |
with Report do begin PrepareReport(); PreviewPages.SaveToFile(Ffilepath); // .fp3 Self.zipAndSend(); // not Report method ShowPreparedReport(); end; |
open after downloading
1 2 3 4 |
Report.Clear(); Report.PreviewPages.Initialize(); Report.PreviewPages.LoadFromFile(filename); // .fp3 Report.ShowPreparedReport; |
full code example from project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
procedure TEventReportDM.GenerateReport; begin frxResources.LoadFromFile('Russian.frc'); Report.PrepareReport(); Assert(FReportFormat <> ''); if FReportFormat = 'pdf' then begin Ffilepath := clientTempDir + '\' + TPath.GetRandomFileName + '.pdf'; zipAndSend(); frxPDFExport.ShowDialog := false; frxPDFExport.FileName := Ffilepath; Report.export(frxPDFExport); frxPDFExport.ShowDialog := true; // setting back //show report in default program ShellExecute(Application.Handle, nil, PChar(Ffilepath), nil, nil, SW_SHOWNORMAL); end else if FReportFormat = 'fp3' then begin Ffilepath := clientTempDir + '\' + TPath.GetRandomFileName + '.fp3'; with Report do begin PrepareReport(); PreviewPages.SaveToFile(Ffilepath); Self.zipAndSend(); // not Report method ShowPreparedReport(); end; end; end; |
download and open report
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
procedure TEventReportDM.DownloadAndShowReport(AReportID: integer); var q: TFDQuery; fs: TFileStream; relWebPath: string; filenameZIP, filename: string; server: string; z: TZipFile; i: integer; ext: string; begin frxResources.LoadFromFile('Russian.frc'); init(); q := TFDQuery.Create(Self); try with q do begin Connection := MainModule.FDConnection; SQL.Text := 'SELECT * FROM coffeetest_db.generatedreports where id=:id'; params.ParamValues['id'] := AReportID; Disconnect(); Open(); relWebPath := FieldByName('relWebPath').AsString; //load from server filenameZIP := clientTempDir + '\' + TPath.GetRandomFileName() + '.zip'; fs := TFileStream.Create(filenameZIP, fmCreate); try fs.Position := 0; idhttp.Get(MainModule.HTTPServerToRequest + '/' + relWebPath, fs); fs.Position := 0; finally fs.Free; if TFile.Exists(filenameZIP) then begin MainModule.TrashFiles.Add(ExtractFilePath(Application.ExeName) + filenameZIP); // unzip z := TZipFile.Create; try z.Open(filenameZIP, zmRead); if not TDirectory.Exists(clientTempDir) then TDirectory.CreateDirectory(clientTempDir); for i := 0 to z.FileCount - 1 do begin filename := z.FileName[i]; // having only one file in archive z.Extract(filename, clientTempDir, true); end; finally z.Free; end; filename := ExtractFilePath(Application.ExeName) + clientTempDir + '\' + filename; MainModule.TrashFiles.Add(filename); // check extension ext := ExtractFileExt(filename); if ext = '.fp3' then begin Report.Clear(); Report.PreviewPages.Initialize(); Report.PreviewPages.LoadFromFile(filename); Report.ShowPreparedReport; end else if ext = '.pdf' then // open in standart begin ShellExecute(Application.Handle, nil, PChar(filename), nil, nil, SW_SHOWNORMAL); end; end; end; end; finally q.Free(); end; end; |