How to bulk export pdf report

Statement of needs

After the report may show the current show reports through the Export button in the toolbar to export to pdf file, but in actual use will usually require a report is not required to show, by some operations directly export the report into a pdf file, and often require batch export to pdf file, by following a few examples introduce the next report does not show how volume generated excel file.

To achieve this need to use api general way, volume generated excel file, in accordance with the way up points can be divided into three categories:

A: single table to export a single multi-page pdf

Two: Export single multi-table multi-page pdf

Three: multi-table export multiple pdf file

Single multiple pages

This manner is generally a fixed report format, and then filtering the data according to a parameter, a plurality of parameters need to export data export pdf, and the data of each parameter into a different page in the same pdf, for example according to the present embodiment, area statistics order information, each region requires export export data to a pdf page, look below this practice:

Report design interfaces Needless to say, in accordance with the needs of the design on the line, as shown below:

Added a parameter report: area, means for receiving location parameters, and then concentrated in filtered data through the data line in this parameter.

Api jsp used in the following code:

<%@ page contentType="text/html;charset=UTF-8" %>

<%@ page import="com.raqsoft.report.model.*"%>

<%@ page import="com.raqsoft.report.usermodel.*"%>

<%@ page import="com.raqsoft.report.view.*"%>

<%@ page import="com.raqsoft.report.util.*"%>

<%@ page import="com.raqsoft.report.view.pdf.PdfReport"%>

<%@ page import="java.io.*" %>

<%

 String report = request.getParameter ( "report"); // get the report name

if (report == null) report = "orders .rpx"; // if the report on the url name is empty, then take the orders table

String fileName = report.substring (0, report.length () - 4); // read the file name, the name is used to set excel

String reportName = request.getRealPath("WEB-INF\\\reportFiles\\\"+report);//

String exportPath = request.getRealPath ( "/ export"); // build export directory under the application root directory, place the export file

ReportDefine rd = (ReportDefine)ReportUtils.read(reportName);//读取报表

String areas = "North, Northeast, Northwest, South, Southwest"; // this case in accordance with the regional circulation, practice may receive other parameters, you can also get data from the database

String\[\] area=areas.split(",");

OutputStream os = null;

os = new FileOutputStream(exportPath+"/"+fileName+".pdf");

PdfReport is = new PdfReport (us);

for (int i = 0; i <area.length; i ++) {// do loop by region

Context cxt = new Context();

cxt.setParamValue ( "area", area \ [i \]); // area is defined in the report parameters, parameter values ​​set here

  Engine engine = new Engine (rd, cxt); // constructor reporting engine

  IReport iReport = engine.calc (); // report operation

 er.export (iReport); // set the report results to the pdf

}

er.save();

 os.flush();

 os.close();

%>

 

Thus, it will generate a corresponding pdf file in the root directory of the application export

Multi-table multi-page

Pdf is applied to this case derived from a plurality of statements supported, then report to export each pdf excel in a different page, and may be different for each report parameter (if the same parameter, then a sample and the like, but according to reports name circulating on the line), the following look at specific implementation process:

Due to the different reporting parameters may be different, so each analytical report and the corresponding parameter will be relatively large in the degree of difficulty in api, this embodiment requires access reports through url, access a particular format, such as:

? http: // localhost: 6868 / demo / reportJsp / exportPdf1.jsp report = { 1} report names no parameters no parameters {2} {report name Report 1 (parameter 1 = value1; parameter 2 = value2; ...)} { report 2 (parameter 1 = value1; value2 parameter = 2; ...)} , such as: ? HTTP: // localhost: 6868 / Demo / reportJsp / exportPdf1.jsp report test1.rpx} = {{test2.rpx (arg1 =. 11 ; arg2 = 22; arg3 33) } = in this manner, the high version now in tomcat defined there will be some special symbols, {} may be converted to the corresponding use urlencode manner, of {% 7B,} is % 7B, if there is another value, the corresponding transition on the line to do, after the setup is complete url, then look on how to parse the url, and batch production pdf, code is as follows:

<%@ page contentType="text/html;charset=UTF-8" %>

<%@ page import="com.raqsoft.report.model.*"%>

<%@ page import="com.raqsoft.report.usermodel.*"%>

<%@ page import="com.raqsoft.report.view.*"%>

<%@ page import="com.raqsoft.report.util.*"%>

<%@ page import="com.raqsoft.report.view.pdf.PdfReport"%>

<%@ page import="java.io.*" %>

<%

 // This JSP parameter format: report = {no report parameter name 1} {2} were no report parameters {Statement 1 (Parameter 1 = VALUE1; Parameter 2 = value2; ...)} {2 reports (Parameter 1 = value1; parameter 2 = value2; ...)}

request.setCharacterEncoding( "UTF-8" );

 String report = request.getParameter( "report" );

 if (report == null || report.trim (). length () == 0) throw new Exception ( "Please enter the file name and the report parameter string parameter report = {no} {Report 1 Report name (parameter 1 = value1 ; parameter 2 = value2; ...)} {2 reports (parameter 1 = value1; parameter 2 = value2; ...)} ... ");

String exportPath = request.getRealPath ( "/ export"); // build export directory under the application root directory, place the export file

String report1 = report.replace ( "}", ""); //} string removed

String report2 = report1.substring (1, report1.length ()); // remove the leftmost string of {

String \ [\] a = report2.split ( "\\\ {"); // this time with a report among the plurality of strings {separated, so here the array generated in accordance with the split symbol

OutputStream os = null;

os = new FileOutputStream(exportPath+"/test.pdf");

PdfReport is = new PdfReport (us);

for (int i = 0; i <a.length; i ++) {// cycle by the array, i.e. by report cycle

The sub strings if (a \ [i \]. LastIndexOf ( "(") <= 0) // determines whether segmentation contains (such as comprises, representing the parameter is not included, there is no parameter

{

String reportPath = request.getRealPath ( "WEB-INF \\\ reportFiles \\\" + a \ [i \]); // Get Report path

 String sheetName=a\[i\].substring(0,a\[i\].length()-4);//获取sheet名称

 ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);//读取报表

 Context cxt = new Context();

 Engine engine = new Engine (rd, cxt); // constructor reporting engine

IReport iReport = engine.calc (); // calculate Report

er.export (sheetName, iReport); // report results into sheet

}

else{

 . System.out.println ( "report has parameters, report named =" + a \ [i \] split ( "\\\ (") \ [0 \]); // if there are parameters, press (Character split, on the left side for the report name

 String reportPath = request.getRealPath("WEB-INF\\\reportFiles\\\"+a\[i\].split("\\\(")\[0\]);

 String sheetName=a\[i\].split("\\\(")\[0\].substring(0,a\[i\].split("\\\(")\[0\].length()-4);

 ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);

 Context cxt = new Context();

 . String \ [\] cs = a \ [i \] split ( "\\\ (") \ [1 \] replace ( ")", ".") Split. ( ";"); // right is a parameter string, and removes the parameter strings), the plurality of parameters; separated, so here follow; Split

 for (int j = 0; j <cs.length; j ++) {// cycle Parametric

 cxt.setParamValue (. cs \ [j \] split ( "=") \ [0 \], cs \ [j \] split ( "=") \ [1 \].); // set the parameters

 }

 Engine engine = new Engine (rd, cxt); // constructor reporting engine

IReport iReport = engine.calc();

er.export (iReport);

}

}

er.save();

 os.flush();

 os.close();

%>

 

Multi-table multi-pdf

Example Two similar and this way, requires that each export the report to a different pdf file pdf Export in actual use, but may be related to subsequent download problem, so in this way is usually to create a temporary directory, then multiple pdf into a temporary directory, you can download package. Specific code as follows:

<%@ page contentType="text/html;charset=UTF-8" %>

<%@ page import="com.raqsoft.report.model.*"%>

<%@ page import="com.raqsoft.report.usermodel.*"%>

<%@ page import="com.raqsoft.report.view.*"%>

<%@ page import="com.raqsoft.report.util.*"%>

<%@ page import="com.raqsoft.report.view.excel.ExcelReport"%>

<%

 // This JSP parameter format: report = {no report parameter name 1} {2} were no report parameters {Statement 1 (Parameter 1 = VALUE1; Parameter 2 = value2; ...)} {2 reports (Parameter 1 = value1; parameter 2 = value2; ...)}

request.setCharacterEncoding( "UTF-8" );

 String report = request.getParameter( "report" );

 if (report == null || report.trim (). length () == 0) throw new Exception ( "Please enter the file name and the report parameter string parameter report = {no} {Report 1 Report name (parameter 1 = value1 ; parameter 2 = value2; ...)} {2 reports (parameter 1 = value1; parameter 2 = value2; ...)} ... ");

String exportPath = request.getRealPath ( "/ export"); // build export directory under the application root directory, place the export file

String fileName=Double.toString(Math.random()*100000000).toString().substring(0,6);

String pdfPath=exportPath+"\\\"+fileName;

java.io.File file = new java.io.File (pdfPath);

 if(!file.exists()) {

 file.mkdirs();

 } else {

 }

String report1 = report.replace ( "}", ""); //} string removed

String report2 = report1.substring (1, report1.length ()); // remove the leftmost string of {

String \ [\] a = report2.split ( "\\\ {"); // this time with a report among the plurality of strings {separated, so here the array generated in accordance with the split symbol

ExcelReport er=new ExcelReport();

for (int i = 0; i <a.length; i ++) {// cycle by the array, i.e. by report cycle

The sub strings if (a \ [i \]. LastIndexOf ( "(") <= 0) // determines whether segmentation contains (such as comprises, representing the parameter is not included, there is no parameter

{

String reportPath = request.getRealPath ( "WEB-INF \\\ reportFiles \\\" + a \ [i \]); // Get Report path

 String sheetName = a \ [i \] substring (0, a \ [i \] length () - 4.);. // Get the name of the report

 ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);//读取报表

 Context cxt = new Context();

 Engine engine = new Engine (rd, cxt); // constructor reporting engine

IReport iReport = engine.calc (); // calculate Report

ReportUtils.exportToPDF(pdfPath+"/"+sheetName+".pdf",iReport);

}

else{

 . System.out.println ( "report has parameters, report named =" + a \ [i \] split ( "\\\ (") \ [0 \]); // if there are parameters, press (Character split, on the left side for the report name

 String reportPath = request.getRealPath("WEB-INF\\\reportFiles\\\"+a\[i\].split("\\\(")\[0\]);

 String sheetName=a\[i\].split("\\\(")\[0\].substring(0,a\[i\].split("\\\(")\[0\].length()-4);

 ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);

 Context cxt = new Context();

 . String \ [\] cs = a \ [i \] split ( "\\\ (") \ [1 \] replace ( ")", ".") Split. ( ";"); // right is a parameter string, and removes the parameter strings), the plurality of parameters; separated, so here follow; Split

 for (int j = 0; j <cs.length; j ++) {// cycle Parametric

 cxt.setParamValue (. cs \ [j \] split ( "=") \ [0 \], cs \ [j \] split ( "=") \ [1 \].); // set the parameters

 }

 Engine engine = new Engine (rd, cxt); // constructor reporting engine

IReport iReport = engine.calc();

ReportUtils.exportToPDF(pdfPath+"/"+sheetName+".pdf",iReport);

}

}

%>

 

Download package

After the file directory corresponding to the next generation, can be its own separate links to the pdf file download may be generated directly download the pdf file directly, add the following code in the corresponding file jsp:

response.setContentType("application/msword");

 response.setHeader("Content-disposition","attachment; filename="+java.net.URLEncoder.encode(fileName+".pdf", "UTF-8"));

 BufferedInputStream bis = null;

 BufferedOutputStream bos = null;

 try {

 bis = new BufferedInputStream(new FileInputStream( exportPath+"/"+fileName+".pdf"));

 bos = new BufferedOutputStream(response.getOutputStream());

 byte\[\] buff = new byte\[2048\];

 int bytes read;

 while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

 bos.write(buff,0,bytesRead);

 }

 } catch(final IOException e) {

 System.out.println ( "出现IOException." + e );

 } finally {

 if (bis != null)

 bis.close();

 if (bos != null)

 bos.close();

 }

 System.out.println ( "the download is complete ----------------");

File file = new File(exportPath+"/"+fileName+".pdf");

if (file.exists ()) file.delete (); // delete files

 

Practical applications may be required to file the culmination way zip package, such as Example Three, java program to fight the zip package under the direct Baidu, and then download the zip line.

to sum up

This article describes how a report by api bulk export to pdf way, in practice it is also possible to generate excel or txt, etc., to the idea is the same, the time to change to a different line of api.

Published 48 original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/zozoxxma/article/details/103760972