Fastreport Generating a Web report

Source: Internet
Author: User
Tags stringreplace

Developing Web applications often encounters report printing problems. Simple application can use the page printing function of IE, using HTML tag control format to realize. But complex business applications, reports are not only an important part of the application, but also often quite complex. Many applications now require the ability to provide custom reports-that is, customers can design and modify reports themselves.

In the C/s structure system, there are many mature solutions to the report problems. such as the Delphi development tool not only comes with the report control, but also utilizes third-party controls for fast and flexible report creation and printing, with the famous controls being the Fastreport of Fr-software & A.tzyganenko. Fastreport provides a complete control package that seamlessly integrates with Delphi from design to print, providing a friendly and flexible design interface that is a good solution for developing C/s applications that allow users to customize their reports.

In b/S structure application, Crystal report is a common and recommended solution for large-scale reporting system. But the Crystal report is now expensive and the system is quite large. It is not perfect for customization and precise control of print effects. Of course, on the market today, it is still a preferred reporting solution for Web applications.

If we can move the mature report solution of C/s application to B/s application, I believe it is very welcome for most of the developers. This article will describe a user-definable report solution in the Java environment that uses Fastreport to implement B/s applications. Because the author of the recent period is using Delphi, Java to do some projects, so the sample code to Delphi, Java writing.

The basic environment for this solution sample is: WINDOWS server+sql SERVER 2000+tomcat 4.0. Development tools: IntelliJ idea 3.0,delphi 5.0. The client is IE 5.0 browser.

The scheme requires Delphi to write two programs, one is to be included in the Web page and run in the browser ActiveX (. ocx), one is running on the server side of the report processing program, in the middle through the Java Program Connection-or any other web language can, such as ASP, PHP and so on. The schematic diagram of the scenario is as follows:

     report design process
    
     Report Printing process
    
    report SERVER: Can be made into a normal Windows program, can also be made into a COM program (Automation Object). During the report design process, the client (ACTIVEX) sends a report design request to the Web server that contains the name of the report to be designed, and when the request is received by the Web server, the report server is called to request the design of the reporting file; After the server receives the request, it loads the report's data environment and then compresses the report design file (. FRF) and the report's Data Environment file into a package file (. zip), returning the full pathname of the package file to the Web server invoker; web The server sends the package file back to the client (ACTIVEX), the client saves the received package file to the local hard disk, and unzip it, recovers the data environment from the Data Environment file, and opens the report file to the user by fastreport the corresponding control to provide a visual design. When the user designs the report in the ActiveX, although cannot connect with the database, but because the data environment already exists, therefore the user imitates the design report under the usual C/s application structure, can see the report data dictionary information normally. During report printing, the client (ACTIVEX) sends a report print/preview request to the Web server that contains the name of the report to print/preview, and when the request is received by the Web server, the report file is called to print or preview; After the server receives the request, it loads the report's Data environment, loads the report file (. FRF), runs the report in a no-interface state, and finally compresses the resulting prepared report file (. FRP) into a package file (. zip), returning the full pathname of the package file to the Web The server invokes the program; the WEB server sends the package file back to the client (ACTIVEX), the client saves the received package file to the local hard disk, and it is decompressed to open the report file (. FRP) through the appropriate control of Fastreport. Preview or print or reformat the user or output the file to another format. Users preview the report in ActiveX, like previewing the report under the usual C/s application structure.

Web SERVER: Provides the usual Web services functionality.

Activex:activex is a set of technologies that Microsoft has proposed to enable software parts to interact in a networked environment using COM (Component object model, part object models). It has nothing to do with the specific programming language. As a technology developed for Internet applications, ActiveX is widely used in various aspects of Web servers and clients. The ActiveX controls in this scenario mainly do two things, one is to use the Fastreport control for report processing, including report design (. frf files) and report printing (. frp files). One is to communicate with Web server, request and receive package files. The example ActiveX in this article was written in Delphi 5.0.

The following is an example of a specific implementation of each part (since only the implementation of the description scheme, so many code details are omission).

A. report SERVER

The report server can be made into a common Windows program, or it can be made into a COM program (Automation Object). In this example, for simplicity, a common Windows program implementation is used.

     a new application in Delphi. Add controls such as Tfrreport, Tfrdbdataset, ADOConnection, Tadoquery, and so on in the form-in order to use the Fastreport control, you need to install the control package from the site http:// www.fast-report.com Download, many software sites in the country provide the download of the control package. Where the Tfrdbdataset, Tadoquery control depends on the application need to join more than one, in order to compress the file, but also to add a compression control, this example uses Vclzip. Add three functions to Form1: Predesignreport (rpfilename:string), Preprintreport (rpfilename:string), Zipreportfiles (rpFileName: String), respectively, to prepare the report design file, prepare the report print file, and compress the report file  . The Form1.create method is:

procedure tform1.formcreate (Sender: tobject);
var
    rpfilename,mode:string;
Begin
  if paramcount>1 then
    begin
         MODE:=PARAMSTR (1);
      rpfilename:=paramstr (2);
      if mode= ' d '  then   //design report
       if predesignreport (rpfilename)  then
           zipreportfiles (Rpfilename);
      if mode= ' R '  then   //Print report
         if preprintreport (rpfilename)  then
             zipreportfiles (Rpfilename);
   end;
   Application.Terminate;
End;


     The program determines whether to prepare the report design file or prepare the report print file according to the invocation parameters, and then calls the appropriate procedure to implement it. The final application.terminate  is to let the program execute the function and then exit-because this is a service-side program, it is not able to interact with the user.
  predesignreport (rpfilename:string) method   

function tform1.predesignreport (rpfilename:string): boolean;
var

...    //other variables
        dtffilename: String;
Begin
    
    dtffilename:=stringreplace (Rpfilename, extractfileext (rpFileName), '. DTF ',  [rfreplaceall, rfignorecase]);
  try
        rpadoquery.sql.add (' ... ');
        rpadoquery.open;//open a report's Data Environment
         rpadoquery.fieldlist.savetofile (Dtffilename);
        result:=true;
   except
        on exception do
            result:=false;
   end;
End;


The function of Predesignreport is to prepare the report design file. You can refer to multiple datasets in a report, this example assumes that the report references only a dataset named Rpadoquery. Rpfilename is the report file name (. FRF), Dtffilename is the file name (. DTF) that holds the data environment. Because the client cannot connect to the database, the fileds in the dataset is saved to a file by RpAdoquery.FieldList.SaveToFile (Dtffilename), and the report file is routed to the client's ActiveX, ActiveX uses. dtf files to reproduce the data environment of the report.
Preprintreport (rpfilename:string) method:

function Tform1.preprintreport (rpfilename:string): boolean;
Var
......
repfilename:string;
Begin
......
Repfilename:=stringreplace (Rpfilename, Extractfileext (rpfilename), '. FRP ', [Rfreplaceall, Rfignorecase]);
Try
RpAdoquery.SQL.Add (' ... ');
rpadoquery.open;//open a report's Data environment
Frreport1.showprogress:=false;
Frreport1.clear;
Frreport1.loadfromfile (Rpfilename);
Frdbdataset1.dataset: =rpadoquery;
Frreport1.dataset: =frdbdataset1;
Frreport1.preparereport;
Frreport1.savepreparedreport (Repfilename);
Result:=true;
Except
On Exception do
Result:=false;
End
End


function Preprintreport is the purpose of preparing a printed report file, which is to load the report and run it on the server side, save the running report as a file, and use it to send to the client for previewing or printing. Repfilename is the prepared report file name (. FRP). Also assume that the report refers only to a dataset named Rpadoquery. Frreport1.showprogress:=false The Progress window is not displayed while the report is running (the server side cannot display the interface with which the user interacts); next frreport1.clear; Load report files and set related data properties; Frreport1.preparereport is to run the report without displaying the preview window; Frreport1.savepreparedreport (repfilename) saves the running report to a file , the file is routed to the client's Activex,activex to preview or display the report directly.
Zipreportfiles (rpfilename:string) method:

function Tform1.zipreportfiles (rpfilename:string): boolean;
Var
......
zipfilename,filename:string;
Zipcount:integer;
Begin
......
Zipfilename:=stringreplace (Rpfilename, Extractfileext (rpfilename), '. zip ', [Rfreplaceall, Rfignorecase]);
filename:= Extractfilename (rpfilename);
filename:= Changefileext (FileName, '. * ');
Try
Vclzip1.zipname:= Zipfilename;
Vclzip1.rootdir:= '. ';
VCLZIP1.FILESLIST.ADD (FileName);
Zipcount:= Vclzip1.zip;
If Zipcount = 0 Then
Result:=false
Else
Result:=true;
Except
On Exception do
Result:=false;
End
End


The function of Zipreportfiles is to compress the report file to be transmitted to the client into a. zip file, simplifying the file transfer process, and compressing the amount of data. When ActiveX receives a. zip file, it extracts the files from the package before processing.
Second, WEB SERVER
The role of Web server in the scenario is primarily to invoke the report server based on an ActiveX request and send the. zip file generated by the report server to ActiveX. The sample is handled by a report.jsp file: ActiveX sends the. zip file to ActiveX after the report.jsp file is called by the GET request, and the report.jsp file calls the report server processing.
report.jsp file:

<%@ page import= "..."%>
<% @page contenttype= "Application/octet-stream"%>
<%
try{
String reqfilename = Request.getparameter ("Rpfilename");
String Reqmode = Request.getparameter ("mode");//d for Design report, R for Print report
String rpfilename = Xxxx.getrpfilename (reqfilename); The actual report file name is obtained based on the requested report name, such as the request Order report, and the actual report document corresponding to the order report is ORDER.FRF.
String l_cmd= "Reportserver.exe" +reqmode+ "+ reqfilename;
Process l_ps=java.lang.runtime.getruntime (). exec (L_cmd,null);
Byte[] L_b=new byte[100];
while (L_ps.getinputstream (). Read (l_b,0,100)!=-1) {
..;
}

Send file
String zipfilename = Xxxx.getzipfilename (reqfilename); Get the compressed file name
Response.setheader ("Content-disposition", "attachment; Filename=" "" + Zipfilename + "" "");
Java.io.FileInputStream FileInputStream = new Java.io.FileInputStream (zipfilename);

int i;
while ((I=fileinputstream.read ())! =-1) {
Out.write (i);
}
Fileinputstream.close ();
Out.close ();
}catch (Exception e) {
......
}
%>

String l_cmd= "Reportserver.exe" +reqmode+ "+ reqfilename; The command string that makes up the call to the report server. while (L_ps.getinputstream (). Read (l_b,0,100)!=-1) {;} waits for the report server to finish executing, otherwise the program executes the next line of statements after starting the report server. There are many ways to send files, such as by using ActiveX via FTP.

Third, ACTIVEX

The ActiveX controls in the scenario mainly do two things: report processing using the Fastreport control, including report design (. frf file) and report printing (. frp file). One is to communicate with Web server, request and receive package files.

     in the Delphi new activeform  application, named Reportaform. Add Combox, button, edit, label and other controls to user interaction in the form; To process the report, add multiple Frspeedbutton of Fastreport to handle report events such as design, preview, print, page flip, save, etc. Add Frreport, Frdbdataset, Frdesigner and so on to design the report at run time, and if you want to use graphics, checkboxes, and so on when designing the report, add the appropriate controls; Join Frpreview, Frtextexport, Frrtfexport and other controls enable you to preview the report and output the report as text, RTF, and other format files; Join Adoquery (can add multiple according to actual need) to provide data environment for report design, adoquery not open, not connected with database ; Join Nmhttp to contact the Web server. Add four functions: Designreport (rpfilename:string), PrintReport (rpfilename:string), Unzipreportfiles (rpfilename:string), Getreportfile (rpfilename,mode:string) is used to design reports, print reports, decompress reports, and send requests to Web server to obtain report file  .
    getreportfile (rpfilename,mode:string) method:

function Treportaform.getreportfile (rpfilename,mode:string): boolean;
Var
......
zipfilename:string;
Begin
......
Zipfilename:=stringreplace (Rpfilename, Extractfileext (rpfilename), '. zip ', [Rfreplaceall, Rfignorecase]);
Try
Nmhttp1.inputfilemode: = TRUE;
Nmhttp1.body:= '. " ' + zipfilename;
NMHTTP1. Get (' http://www..../. /report.jsp?rpfilename= ' +rpfilename+ ' &mode= ' +mode);
Result:=true;
Except
On Exception do
Result:=false;
End
End

The function of Getreportfile is to send a report request (via the Nmhttp get method) to the Web server and save the returned compressed package file to the local hard disk (Zipfilename).
Unzipreportfiles (rpfilename:string) method:

function Treportaform.unzipreportfiles (rpfilename:string): boolean;
Var
......
zipfilename,filename:string;
Zipcount:integer;
Begin
......
Zipfilename:=stringreplace (Rpfilename, Extractfileext (rpfilename), '. zip ', [Rfreplaceall, Rfignorecase]);
filename:= Extractfilename (rpfilename);
filename:= Changefileext (FileName, '. * ');
Try
Vclunzip1.zipname:= '. ' + Zipfilename;
Vclunzip1.destdir:= '. ';
Vclunzip1.overwritemode:= always;
Vclunzip1.readzip;
VCLUNZIP1.FILESLIST.ADD (FileName);
Zipcount:= Vclunzip1.unzip;
If Zipcount = 0 Then
Result:=false
Else
Result:=true;
Except
On Exception do
Result:=false;
End
End


The function of Unzipreportfiles is to extract the files from the compressed package for use by ActiveX. It is exactly the opposite of the Zipreportfiles in the report server program.
Designreport (rpfilename:string) method:

function Treportaform. Designreport (rpfilename:string): boolean;
Var
dtffilename,rpfilename:string;
Fldlist:tstringlist;
T:tstringfield;
I:integer;
Begin
......
Dtffilename:=stringreplace (Rpfilename, Extractfileext (rpfilename), '. DTF ', [Rfreplaceall, Rfignorecase]);// Get the Data Environment file name
Fldlist:=tstringlist.create;
Fldlist. LoadFromFile (Dtffilename);
RpAdoquery.Fields.Clear;
For I: = 0 to Fldlist. Count-1 do
Begin
T: = Tstringfield.create (nil);
T.fieldname: = Fldlist[i];
T.name: = Rpadoquery.name + t.fieldname;
RpAdoquery.Fields.add (T);
End
Frreport1.loadfromfile (Rpfilename);
Frreport1.designreport;
End

The function Designreport recovers the report's data Environment from the. DTF (generated by the reports server) file, and then uses the Fastreport Frreport control to design the report. In Fastreport, the field in the dataset only cares about the name (all through variant types), not the data type, so when you restore the data Environment for the report, all the fields are joined as String types. The sample assumes that the report has only one dataset named Rpadoquery. The report design run-time window runs in the ActiveX process space.

After the client has designed the report and saved it, it needs to send the saved report file (. FRF) back to the server store. File uploads for most developers should be familiar and simple, this part of the program is omitted.
PrintReport (rpfilename:string) method:

function Treportaform. PrintReport (rpfilename:string): boolean;
Var
repfilename:string;
Begin
......
Repfilename:=stringreplace (Rpfilename, Extractfileext (rpfilename), '. FRP ', [Rfreplaceall, Rfignorecase]);// Get the prepared report file name
Try
Frpreview1.clear;
Frreport1.preview:=nil;
Frreport1.clear;
Frreport1.loadpreparedreport (Repfilename);
Frreport1.preview: =frpreview1;
Frreport1.showpreparedreport;
Result:=true;
Except
On Exception do
Result:=false;
End
End

The function PrintReport loads a report that is run by the reports server. FRP files, previewed and printed on the ActiveX side by calling the Frreport Showpreparedreport method.

The introduction of the scenario implementation method ends. The advantages of this scheme are: to keep the structure of the application unchanged (b/s), the C/S application structure has been very mature report scheme transplant, so that in the Web application can also achieve any complex report design and printing, as well as accurate control of the printing effect.

Http://www.cnblogs.com/sonicit/archive/2008/04/18/1160379.html

Fastreport Generating a Web report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.