各位大佬,先有些问题想要请教,最近做的项目涉及到WEB打印问题,请教各位有没有什么好的建议或者是控件,我的开发环境是MyEclipse5.5+Tomacat5.5 基于MVC的框架结构,现在在WEB打印这里不知道该如何下手,有这方面的朋友们帮忙解决下呀,传统的打印我已经试过,无法控制页面的大小。主要因为有一个打印格式需要自定义,求教各位了。 谢谢!!!!
调试欢乐多
一般问题我可以帮你解决!
eg:
window.open("目录/test.jsp?id=1);
然后在test.jsp里面加入以下代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试</title> <!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<script lanuage="javascript">
<!--
function unLoadReportSession()
{
window.frm.action = "<%=request.getContextPath()%>/unloadSession.do";
window.frm.submit();
}
function loadReport()
{
var sWidth=window.screen.Width;
var sHeight=window.screen.Height-30;
window.resizeTo(sWidth,sHeight);
window.moveTo(0,0);
}
//-->
</script>
<body onunload="unLoadReportSession();" onload ="loadReport()" leftmargin="0" topmargin="0" rightmargin="0">
<form id ="frm" >
</form>
<%
int id;
id=Integer.parseInt(request.getParameter("id"));
String reportFile ="/report/iReport/test.jasper";
String params="dispatch=test&reportFile="+reportFile+"&id="+id;
%>
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH = "100%" HEIGHT = "100%" codebase="<%=request.getContextPath()%>/j2re.exe#Version=5,0,0,5"><NOEMBED></NOEMBED>
<APPLET CODE = "EmbeddedViewerApplet.class" NAME ="testApplet" CODEBASE ="<%=request.getContextPath()%>/applets" ARCHIVE = "jasperreports-applet.jar,jasperreports.jar" WIDTH = "100%" HEIGHT = "100%">
<PARAM NAME = CODE VALUE = "EmbeddedViewerApplet.class" >
<PARAM NAME = CODEBASE VALUE = "<%=request.getContextPath()%>/applets" >
<PARAM NAME = ARCHIVE VALUE = "jasperreports-applet.jar,jasperreports.jar" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.5">
<PARAM NAME="scriptable" VALUE="false">
<PARAM NAME = "REPORT_URL" VALUE ="<%=request.getContextPath()%>/testAction.do?<%=params%>">
</APPLET>
</OBJECT>
</body>
</html>
里面有几个东西你需要替换成你自己的,第一参数,第二你将要处理的ACTION,这里用的是testAction.do.其他主要就在下个action里面处理了.
testAction:
public ActionForward test(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map parameters = new HashMap();
int id;
String strSQL = "select * from test where id="+id;
parameters.put("strSQL",strSQL);
//下面的你不用动,只要上面你把自己的参数和sql放到map里面.
ServletContext context = this.getServlet().getServletContext();
String reportFilePath = request.getParameter("reportFile");
File reportFile = new File(context.getRealPath(reportFilePath));
JasperPrint jasperPrint = null;
Connection conn =dataSource.getConnection();
try
{
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath()); jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
conn
);
//将这个报表结果存入session中给后来的导出报表使用。
request.getSession().setAttribute("jasperReport",jasperPrint); }
catch (JRException e)
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Web Application Sample</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>"); out.println("<body bgcolor=\"white\">"); out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
out.println("<pre>"); e.printStackTrace(out); out.println("</pre>"); out.println("</body>");
out.println("</html>");
} finally {
try {
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if (jasperPrint != null)
{
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream = response.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close(); ouputStream.flush();
ouputStream.close();
}
else
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Web Application Sample</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>"); out.println("<body bgcolor=\"white\">"); out.println("<span class=\"bold\">Empty response.</span>"); out.println("</body>");
out.println("</html>");
}
return null;
}
其实这样后续还是报表设计了.代码部分就这些,建议先把ireport弄熟悉再做吧.也很简单的.