动态编译jrxml文件生成html报表收藏
2009-01-15 11:32
597 查看
生成html页面的代码如下:调试完成,欢迎探讨。
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.io.File" %>
<%@ page import="java.sql.*"%>
<jsp:useBean id="hashtable" class="java.util.Hashtable" scope="request"/>
<%
String rpt_id="",branch_id="",jasper_name="",user_id="",REPORT_STS_ID="",srpt_id="",item_id="";
if(hashtable!=null){
if(hashtable.get("rpt_id")!=null){
rpt_id=(String) hashtable.get("rpt_id");
System.out.println(rpt_id);
}
if(hashtable.get("branch_id")!=null){
branch_id=(String) hashtable.get("branch_id");
System.out.println(branch_id);
}
if(hashtable.get("user_id")!=null){
user_id=(String) hashtable.get("user_id");
}
if(hashtable.get("jasper_name")!=null){
jasper_name=(String)hashtable.get("jasper_name");
System.out.println(jasper_name);
}
else{
out.print("<script language='javascript'>alert('报表文件错误!');top.location='/Logout.jsp';</script>");
return;
}
}
%><html>
<script language="javascript" type="text/javascript">
function subreport(types)
{
if(types=="1"){
reportform.action="/servlets.jsperreport.PdfFile";
reportform.submit();
}
else{
reportform.action="/servlets.jsperreport.ExcelFile";
reportform.submit();}
}
</script>
<%
//获取页面控件的值
try{
%>
<link href="../css/css.css" rel="stylesheet" type="text/css">
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<script language="JavaScript" src="../js/function.js" type=""></script>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="3">
<form name="reportform" METHOD="POST" action="">
<tr>
<td align="center"><strong><span class="AlertColor">导出报表>></span></strong></td>
<td align="center"> <input type="image" src="../images/MoniIconPDF.gif" name="but1" onClick="subreport('1');return false;" onMouseOver="showLib(1,'导出为PDF',68)" onMouseOut="hideLib()"></td>
<td height="52" align="center" valign="bottom">
<input type="image" src="../images/MoniIconExcel.gif" name="but2" onClick="subreport('2');return false;" onMouseOver="showLib(1,'导出为Excel',68)" onMouseOut="hideLib()">
<%
Iterator i=hashtable.keySet().iterator();
while (i.hasNext())
{
String key=(String)i.next();
String value=(String) hashtable.get(key);
%>
<input type="hidden" name="<%=key%>" value="<%=value%>">
<%
}%>
<%if("C0000001".equals(request.getSession().getAttribute("oc_branch_id"))){%>
<input type="hidden" name="flag" value="1">
<%}%>
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
<%
Connection conn = com.sysnet.dbutils.PubDataPoolBean.getInstance().
getConn();
Map parameters = new HashMap(hashtable);
parameters.put("REPORT_DIR",this.getServletConfig().getServletContext().getRealPath("/jasper/"));
if("C0000001".equals(request.getSession().getAttribute("oc_branch_id"))){
parameters.put("flag","1");
}
String path = this.getServletConfig().getServletContext().getRealPath("/jasper/"+jasper_name+".jrxml");
String newPath = path.replaceAll("jrxml", "jasper");
File f1=new File(path);
if (!f1.exists())
throw new JRRuntimeException("File "+jasper_name+".jrxml not found. The report design must be compiled first.");
File f2=new File(newPath);
if(!f2.exists()){
JasperCompileManager.compileReportToFile(path, newPath);
}
else{
Long lngJrxmlLastModified = new Long(f1.lastModified());
Long lngJasperLastModified = new Long(f2.lastModified());
if ( (lngJasperLastModified != null) &&
(lngJasperLastModified.longValue() <= lngJrxmlLastModified.longValue())) {
JasperCompileManager.compileReportToFile(path, newPath);
}
}
File reportFile = new File(newPath);
//InputStream reportFile=this.getServletConfig().getServletContext().getResourceAsStream("/jasper/"+jasper_name+".jasper");
if (reportFile==null)
throw new JRRuntimeException("File "+jasper_name+".jasper not found. The report design must be compiled first.");
//JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
JasperPrint jasperPrint =JasperFillManager.fillReport(jasperReport,parameters,conn);
JRHtmlExporter exporter = new JRHtmlExporter();
StringBuffer sbuffer = new StringBuffer();
// Map imagesMap = new HashMap();
//imagesMap.put();
String pathImage = request.getRealPath("/images");
Map imagesMap = new HashMap();
imagesMap.put("pathImage", pathImage);
session.setAttribute("IMAGES_MAP", imagesMap);
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP,imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.TRUE);
// exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER,"");
// exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER,"");
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/net.sf.jasperreports.j2ee.servlets.ImageServlet?image=");
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.exportReport();
conn.close();
} catch (JRException jre) {
System.out.println("JRException:" + jre.getMessage());
}catch(Exception ex){
System.out.println("Exception:" + ex.getMessage());
}
%>
</body></html>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.io.File" %>
<%@ page import="java.sql.*"%>
<jsp:useBean id="hashtable" class="java.util.Hashtable" scope="request"/>
<%
String rpt_id="",branch_id="",jasper_name="",user_id="",REPORT_STS_ID="",srpt_id="",item_id="";
if(hashtable!=null){
if(hashtable.get("rpt_id")!=null){
rpt_id=(String) hashtable.get("rpt_id");
System.out.println(rpt_id);
}
if(hashtable.get("branch_id")!=null){
branch_id=(String) hashtable.get("branch_id");
System.out.println(branch_id);
}
if(hashtable.get("user_id")!=null){
user_id=(String) hashtable.get("user_id");
}
if(hashtable.get("jasper_name")!=null){
jasper_name=(String)hashtable.get("jasper_name");
System.out.println(jasper_name);
}
else{
out.print("<script language='javascript'>alert('报表文件错误!');top.location='/Logout.jsp';</script>");
return;
}
}
%><html>
<script language="javascript" type="text/javascript">
function subreport(types)
{
if(types=="1"){
reportform.action="/servlets.jsperreport.PdfFile";
reportform.submit();
}
else{
reportform.action="/servlets.jsperreport.ExcelFile";
reportform.submit();}
}
</script>
<%
//获取页面控件的值
try{
%>
<link href="../css/css.css" rel="stylesheet" type="text/css">
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<script language="JavaScript" src="../js/function.js" type=""></script>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="3">
<form name="reportform" METHOD="POST" action="">
<tr>
<td align="center"><strong><span class="AlertColor">导出报表>></span></strong></td>
<td align="center"> <input type="image" src="../images/MoniIconPDF.gif" name="but1" onClick="subreport('1');return false;" onMouseOver="showLib(1,'导出为PDF',68)" onMouseOut="hideLib()"></td>
<td height="52" align="center" valign="bottom">
<input type="image" src="../images/MoniIconExcel.gif" name="but2" onClick="subreport('2');return false;" onMouseOver="showLib(1,'导出为Excel',68)" onMouseOut="hideLib()">
<%
Iterator i=hashtable.keySet().iterator();
while (i.hasNext())
{
String key=(String)i.next();
String value=(String) hashtable.get(key);
%>
<input type="hidden" name="<%=key%>" value="<%=value%>">
<%
}%>
<%if("C0000001".equals(request.getSession().getAttribute("oc_branch_id"))){%>
<input type="hidden" name="flag" value="1">
<%}%>
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
<%
Connection conn = com.sysnet.dbutils.PubDataPoolBean.getInstance().
getConn();
Map parameters = new HashMap(hashtable);
parameters.put("REPORT_DIR",this.getServletConfig().getServletContext().getRealPath("/jasper/"));
if("C0000001".equals(request.getSession().getAttribute("oc_branch_id"))){
parameters.put("flag","1");
}
String path = this.getServletConfig().getServletContext().getRealPath("/jasper/"+jasper_name+".jrxml");
String newPath = path.replaceAll("jrxml", "jasper");
File f1=new File(path);
if (!f1.exists())
throw new JRRuntimeException("File "+jasper_name+".jrxml not found. The report design must be compiled first.");
File f2=new File(newPath);
if(!f2.exists()){
JasperCompileManager.compileReportToFile(path, newPath);
}
else{
Long lngJrxmlLastModified = new Long(f1.lastModified());
Long lngJasperLastModified = new Long(f2.lastModified());
if ( (lngJasperLastModified != null) &&
(lngJasperLastModified.longValue() <= lngJrxmlLastModified.longValue())) {
JasperCompileManager.compileReportToFile(path, newPath);
}
}
File reportFile = new File(newPath);
//InputStream reportFile=this.getServletConfig().getServletContext().getResourceAsStream("/jasper/"+jasper_name+".jasper");
if (reportFile==null)
throw new JRRuntimeException("File "+jasper_name+".jasper not found. The report design must be compiled first.");
//JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
JasperPrint jasperPrint =JasperFillManager.fillReport(jasperReport,parameters,conn);
JRHtmlExporter exporter = new JRHtmlExporter();
StringBuffer sbuffer = new StringBuffer();
// Map imagesMap = new HashMap();
//imagesMap.put();
String pathImage = request.getRealPath("/images");
Map imagesMap = new HashMap();
imagesMap.put("pathImage", pathImage);
session.setAttribute("IMAGES_MAP", imagesMap);
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP,imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.TRUE);
// exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER,"");
// exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER,"");
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/net.sf.jasperreports.j2ee.servlets.ImageServlet?image=");
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.exportReport();
conn.close();
} catch (JRException jre) {
System.out.println("JRException:" + jre.getMessage());
}catch(Exception ex){
System.out.println("Exception:" + ex.getMessage());
}
%>
</body></html>
相关文章推荐
- 动态编译jrxml文件生成html报表
- 使用自动生成java文件和自动编译的动态代理模式
- .NET 中的动态编译(生成exe文件)
- 让一个动态链接库文件debug和release编译的时候生成不同的文件名的方法
- 遍历指定文件夹下所有的xml文件并动态生成HTML页面!
- 使用自动生成java文件和自动编译的动态代理模式-马士兵设计模式教程
- [收藏]利用ASP.NET技术动态生成HTML页面
- 调用codeSmith API,l动态编译cst文件,生成exe文件可以在目前pc免安装codeSmith
- 读取jrxml文件生成JasperReport 子报表, javabean作为数据源
- 【转】C#生成dll文件:如何用C#编译生成dll文件即动态链接库文件供其它程序调用
- 动态代理---动态生成java文件并编译成class文件
- 动态创建服务器控件,获取HTML输出(用于生成静态文件,简化页面,简化编程等)
- 动态创建服务器控件,获取HTML输出(用于生成静态文件,简化页面,简化编程等)
- 通过生成PSR文件,实现动态报表格式
- 动态代理---动态生成java文件并编译成class文件
- 在Window下编译生成ffmpeg的动态链接库.dll文件
- makefile学习经验(三)----编译生成动态库文件(方式一)
- 遍历指定文件夹下所有的xml文件并动态生成HTML页面!
- .NET : 动态生成工作流定义文件并且进行编译
- Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件