生成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>