Printing from java code
Initialize
JasperPrint jasperPrint = null;
JRXML file processing
JasperCompileManager.compileReportToFile(application.getRealPath("/reports/"+reportTemplate));
File reportFile = new File(application.getRealPath("/reports/"+reportRealPath));
if (!reportFile.exists())
throw new JRRuntimeException("File " + reportRealPath + " not found. The report design must be compiled first.");
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
Get all your Report's parameters
Map<String, Object> parameters = new HashMap<String, Object>();
jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,new JREmptyDataSource());
Assign output view
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jp);
printDocumentToScreen(response, jp, viewType, reportType2);
private void printDocumentToScreen(HttpServletResponse response, JasperPrint jp, String reportView, String reportType) throws IOException{
byte bytes[] = null;
if(reportView.equalsIgnoreCase(VIEW_OPTION_HTML))
bytes = GenerateHTML.generateHTML(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_PDF))
bytes = GeneratePDF.generatePDF(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_XLS))
bytes = GenerateXLS.generateXLS(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_OTD))
bytes = GenerateODT.generateODT(jp);
response.setContentType("application/xls/csv/html/pdf/txt/html");
response.setHeader("Content-Disposition", "attachment;filename="+reportType+"."+reportView);
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes,0,bytes.length);
response.getOutputStream().flush();
}
Write you new about JREmptyDataSource() in another post.
These are the basic how to pass values from java to iReport
Initialize
JasperPrint jasperPrint = null;
JRXML file processing
JasperCompileManager.compileReportToFile(application.getRealPath("/reports/"+reportTemplate));
File reportFile = new File(application.getRealPath("/reports/"+reportRealPath));
if (!reportFile.exists())
throw new JRRuntimeException("File " + reportRealPath + " not found. The report design must be compiled first.");
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
Get all your Report's parameters
Map<String, Object> parameters = new HashMap<String, Object>();
jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,new JREmptyDataSource());
Assign output view
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jp);
printDocumentToScreen(response, jp, viewType, reportType2);
private void printDocumentToScreen(HttpServletResponse response, JasperPrint jp, String reportView, String reportType) throws IOException{
byte bytes[] = null;
if(reportView.equalsIgnoreCase(VIEW_OPTION_HTML))
bytes = GenerateHTML.generateHTML(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_PDF))
bytes = GeneratePDF.generatePDF(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_XLS))
bytes = GenerateXLS.generateXLS(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_OTD))
bytes = GenerateODT.generateODT(jp);
response.setContentType("application/xls/csv/html/pdf/txt/html");
response.setHeader("Content-Disposition", "attachment;filename="+reportType+"."+reportView);
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes,0,bytes.length);
response.getOutputStream().flush();
}
Write you new about JREmptyDataSource() in another post.
These are the basic how to pass values from java to iReport
No comments:
Post a Comment