[csharp] view plain copy
注意点:扩展方法必须是静态方法,所在的类必须是静态类,所在的命名空间改成System.Web.MVC则能省略页面中必须添加命名空间的约束。
//主要就是输出分页的超级链接的标签
//自定义分页Helper扩展
public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
{
var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath
pageSize = pageSize == 0 ? 3 : pageSize
var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1)//总页数
var output = new StringBuilder()
if (totalPages >1)
{
用Jatools打印插件,目前好像都是收费的版本,但我在网上找到个免费的版本就拿来用了,做的练习。可实现分页,打印。
<script type="text/javascript">
function JatoolsPrinterViewPDFReport(page){
var frm = document.getElementById("toolsForm")
var url = "<%=webapp %>/B1F216Action.do?method=JatoolsPrinterViewPDFReport"
url+="&page="+page
frm.action=url
frm.target='_self'
frm.submit()
}
function doPrint(how)
{
//打印文档对象
var myDoc = {
documents:document,// 打印页面(div)们在本文档中
copyrights:'杰创软件拥有版权 www.jatools.com'
}
// 调用打印方法
if(how == '打印预览...')
jatoolsPrinter.printPreview(myDoc,true ) // 打印预览
else if(how == '打印...')
jatoolsPrinter.print(myDoc ,true) // 打印前弹出打印设置对话框
else
jatoolsPrinter.print(myDoc ,false) // 不弹出对话框打印
}
<body style="margin:10px">
<html:form styleId="toolsForm" method="post"
action="/B1F216Action.do?method=JatoolsPrinterViewPDFReport"
enctype="multipart/form-data">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="50%"></td>
<td align="left">
<hr size="1" color="#000000">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="10%" align="left"><img src="<%=webapp
%>/jasReports/images/reload.GIF" border="0"
onclick="JatoolsPrinterViewPDFReport(0)"></td>
<td> </td>
<%
if (pageIndex >0)
{
%>
<td>
<img src="<%=webapp %>/jasReports/images/first.GIF" border="0" onclick="JatoolsPrinterViewPDFReport(0)">
<img src="<%=webapp
%>/jasReports/images/previous.GIF" border="0"
onclick="JatoolsPrinterViewPDFReport(<%=pageIndex - 1%>)">
</td>
<%
}
else
{
%>
<td>
<img src="<%=webapp %>/jasReports/images/first_grey.GIF" border="0">
<img src="<%=webapp %>/jasReports/images/previous_grey.GIF" border="0">
</td>
<%
}
if (pageIndex <lastPageIndex)
{
%>
<td>
<img src="<%=webapp %>/jasReports/images/next.GIF"
border="0" onclick="JatoolsPrinterViewPDFReport(<%=pageIndex +
1%>)">
<img src="<%=webapp %>/jasReports/images/last.GIF"
border="0"
onclick="JatoolsPrinterViewPDFReport(<%=lastPageIndex%>)">
</td>
<%
}
else
{
%>
<td>
<img src="<%=webapp %>/jasReports/images/next_grey.GIF" border="0">
<img src="<%=webapp %>/jasReports/images/last_grey.GIF" border="0">
</td>
<%
}
%>
<td width="50%" align="center">
<input type="button" value="打印预览..." onclick="doPrint(this.value)">
<input type="button" value="打印..." onclick="doPrint(this.value)">
<input type="button" value="直接打印" onclick="doPrint(this.value)">
</td>
</tr>
</table>
<hr size="1" color="#000000">
</td>
<td width="50%"></td>
</tr>
<tr>
<td width="50%"></td>
<td align="center">
<%=sbuffer%>
</td>
<td width="50%"></td>
</tr>
</table>
</html:form>
</body>
后台代码
// 1.html显示
JRHtmlExporter exporter = new JRHtmlExporter()
//设置导出参数
request.getSession().setAttribute( BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint)
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint)
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out)
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE)//页面显示图片为X,可设置false
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK")
int pageIndex = 0
int lastPageIndex = 0
if (jasperPrint.getPages() != null)
{
lastPageIndex = jasperPrint.getPages().size() - 1
}
String pageStr = request.getParameter("page")
try
{
pageIndex = Integer.parseInt(pageStr)
}
catch(Exception e)
{
}
if (pageIndex <0)
{
pageIndex = 0
}
if (pageIndex >lastPageIndex)
{
pageIndex = lastPageIndex
}
StringBuffer sbuffer = new StringBuffer()
exporter.setParameter(JRExporterParameter.PAGE_INDEX, Integer.valueOf(pageIndex))
exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "")
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "")
exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "")
exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer)
request.setAttribute("pageIndex", pageIndex)
request.setAttribute("lastPageIndex", lastPageIndex)
request.setAttribute("sbuffer", sbuffer)
//此处就是html导出方式,打印部分在页面通过插件实现
exporter.exportReport()