In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Report printing is a problem often encountered in the process of report use and development. this paper summarizes some typical application cases about the development and printing function of Web report, taking the most widely used FineReport as an example.
Case 1: java directly calls report printing
When the java background defines the function of timing printing, and at the same time needs to directly call the printing of the report, because the printer model and paper are different, it is necessary to specify the printer, so as to print successfully and set the return value. So how can this process be achieved?
Realization process
1. Define the report running environment
/ / define the report running environment before executing the report String envPath = "D:\\ FineReport\\ develop\\ code\\ build\\ package\\ WebReport\\ WEB-INF"; FRContext.setCurrentEnv (new LocalEnv (envPath))
2. Define the execution template workbook
TemplateWorkBook workbook = TemplateWorkBookIO.readTemplateWorkBook (FRContext.getCurrentEnv (), "GettingStarted.cpt")
3. Get the report parameters and set the value
/ / Parameter value Parameter [] parameters = workbook.getParameters (); HashMap paraMap = new HashMap (); paraMap.put (parameters [0] .getName (), "North China")
4. Call the report printing method and judge in java.
Print through the formula PrintUtils.printWorkBook (cptPath) and the print window does not pop up at this time.
To print the options dialog box, use PrintUtils.printWorkBook (cptPath, true), where the parameter true displays the print options dialog box, and the code is as follows:
Call the report printing method boolean a = PrintUtils.printWorkBook ("GettingStarted.cpt", paraMap, true) in java; if (a = = false) {System.out.println ("failed! return" + a);} else {System.out.println ("success! return" + a);}
The return value of printWorkBook () is Boolean, and the success of printing can be judged by the return values of true (print success) and false (print failure).
5. Complete code
The process shown above can be edited and printed with the complete code, as follows:
Package com.fr.io; import java.io.File; import java.util.HashMap; import com.fr.base.FRContext; import com.fr.base.Parameter; import com.fr.dav.LocalEnv;import com.fr.main.TemplateWorkBook; import com.fr.print.PrintUtils Public class JavaPrint {public static void main (String [] args) {/ / define the report runtime environment before executing the report String envPath = "D:\\ FineReport\\ develop\\ code\\ build\\ package\\ WebReport\\ WEB-INF"; FRContext.setCurrentEnv (new LocalEnv (envPath)) Try {TemplateWorkBook workbook = TemplateWorkBookIO.readTemplateWorkBook (FRContext.getCurrentEnv (), "GettingStarted.cpt"); / / Parameter value Parameter [] parameters = workbook.getParameters (); HashMap paraMap = new HashMap (); paraMap.put (parameters [0] .getName (), "North China") / / call the report printing method boolean a = PrintUtils.printWorkBook ("GettingStarted.cpt", paraMap, true) in java; if (a = = false) {System.out.println ("failed! Return "+ a);} else {System.out.println (" successful! Return "+ a);}} catch (Exception e) {e.printStackTrace ();}
Edit the program, pop up the printer settings window, select the printer to print, and return to the structure in the background: successful! Return to true.
Case 2: the print template is different from the preview template
Problem feedback
In the process of printing, some users may encounter the following situation: the user system uses iframe to embed the report, and the client browser sees report style 1, not the effect you see when printing, but style 2. For example, the user system shows that the iframe size of the report is fixed and cannot fully display the A4 paper size template, so it is paged when the report is displayed. If you print this display structure directly to A4 paper, only the fixed bar data is displayed on each page, and the bottom is all blank, which is not only beautiful but also a waste of paper.
When you click to print, call the print method of FR through js, and print using the print template, as follows:
Function doFRPrint () {/ / trigger var reportURL= "/ WebReport/ReportServer?reportlet=report_print.cpt" when you click to print; / / print the template path, which is different from the effect viewed by BS FR.doURLFlashPrint (reportURL); / / call the FR printing method} print
Case 3: store the current page data into the database
Template multiple pages, such as some ledger templates, print part of the page each time, and want to trigger an event after each print. The ledger corresponding to the location record page has been printed in the database. Each page has the number of this ledger in a fixed position, and there is a record field in which there is a record whether a record has been printed in the database.
Take the WebReport project that has deployed FineReport to the tomcat server as an example, the detailed process is as follows:
1. Add a post-print event to the template
Open the designer, open its tomcat report group directory, click report > report WEB Properties > pagination Preview Settings in the menu, in the right column, such as click add Flash Post-print event.
In its JavaScript, enter the js code in order to call a jsp written under its WedReport project to perform the storage operation.
2. Get the value of the cell on the page
If the number ID is in cell J3, that is, the third row and the tenth column, the js method is as follows:
Var a = $("# r eq 2-0", "div.reportPane"). Children (). Eq (9) [0] [xss_clean]
3. Transfer the value to the jsp page through Ajax
The complete code is as follows:
Var a = $("# r eq 2-0", "div.reportPane"). Children (). Eq (9) [0] [xss_clean]
$.ajax ({
Url: 'http://localhost:8080/WebReport/print1.jsp?ID='+a
})
4. Define jsp to get the number and modify the database table
To define the page print1.jsp, first get the value of the numbered ID, and then connect to the database to modify the toprint field in the table through the update statement, as follows:
Package com.fr.io;import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import com.fr.base.FRContext; import com.fr.base.Env; public class SaveReportToDatabase {public static void main (String [] args) {SaveReport () } private static void SaveReport () {try {/ / Connect to the database String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@192.168.100.169:1521:orcl10g"; String user = "temp"; String pass = "temp123"; Class.forName (driver) Connection conn = DriverManager.getConnection (url, user, pass); PreparedStatement presmt = conn .prepareStatement ("insert into report values (,)"); / / read into the template file that needs to be saved into the library Env oldEnv = FRContext.getCurrentEnv (); String envPath = oldEnv.getPath () File cptfile = new File (envPath + "\\ reportlets\\ gettingstarted.cpt"); int lens = (int) cptfile.length (); InputStream ins = new FileInputStream (cptfile); / / Save the template to presmt.setString (1, "gettingstarted.cpt") / / the first field stores the template relative path presmt.setBinaryStream (2, ins, lens); / / the second field stores the binary stream of the template file presmt.execute (); conn.commit (); presmt.close (); conn.close () } catch (Exception e) {e.printStackTrace ();}
Case 4: submit the printed information to the library
When printing, sometimes want to know clearly which reports are printed at what time, and record it in a record table in the database, in order to easily view the relevant information. Generally speaking, clicking on print will trigger the print event directly, that is, clicking on print is the status of performing printing. For the whole project, when previewing the report in the browser, click FLASH to print and record the print information into the database table as an example.
1. Create a new information table
Create a new record information table, such as: create a new table in the access database and name it Table 1. Enter two fields, date and tableName, whose types are date and text, respectively.
2. Add post-print events
Click Server > Server configuration, select the paging preview settings option, and enter the paging preview settings window. In the right column, click add FLASH Post-print event, as shown in the following figure:
Choose to submit to the library and select its information record table, and click on the Smart add field, enter the formula: = today () to get the current date in date as above, and enter the formula: = reportName in tablename to get the currently printed report.
Case 5: dynamic printing
When viewing the report, we will sometimes find one or two important or error messages, which need to be printed out and backed up, then we need to dynamically achieve the printing effect, check the information to be printed, and print it out.
1. Parent template
Data preparation
Create a new template and a new data query: sql is a SELECT * FROM employee.
Report subject design
Use the check box control in the A3 cell to check the information to be printed, as shown in the following figure:
Get the value of the check box
Add an initialization event to the check box and save the information about the check box in the array, as follows:
If (! FR.checkBoxes) {FR.checkBoxes=new Array ();} var len=FR.checkBoxes.length;FR.checkBoxes [len] = this
Toolbar add Custom print Button
Click template > template web Properties > fill in Page Settings, select separate settings for the template, add a custom print button to the toolbar, and clear the custom print button.
Customize the JavaScript with the following code:
Var joinData=function () {var datas= []; for (var iTuno [I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.