package org.vwind.user.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
import org.springframework.util.FileCopyUtils;
/**
* 报表基类
*/
public class ReportUtil {
//--------------------JasperPrint-----------------------------------------------------
public static JasperPrint getJasperFile(HttpServletRequest request, String fileName, Map parameters, JRDataSource dataSource) throws Exception{
String fileRealPath = request.getSession().getServletContext().getRealPath(fileName);
File reportFile = new File(fileRealPath);
if (!reportFile.exists()){
JasperCompileManager.compileReportToFile(fileRealPath);
reportFile = new File(fileRealPath);
}
String jrprint_url = JasperFillManager.fillReportToFile(fileRealPath, parameters, dataSource);
File sourceFile = new File(jrprint_url);
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
return jasperPrint;
}
//--------------------Print Button-----------------------------------------------------
public static PrintWriter getPrintWriter(HttpServletResponse response) throws Exception{
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>报表</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("<style type=\"text/css\">");
out.println("@media print {");
out.println(".noprint {display: none ;}}");
out.println("</style>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<div align='center' class='noprint'><input type='button' value='Print' onclick='javascript:window.print();'/></div>");
out.println("</body>");
out.println("</html>");
return out;
}
//-------------------- view by page-----------------------------------------------------
public static StringBuffer createExporterByPage(HttpServletRequest request, HttpServletResponse response, JasperPrint jasperPrint, int pageIndex) throws Exception{
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
StringBuffer sbuffer = new StringBuffer();
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
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.exportReport();// 导出
return sbuffer;
}
//-------------------- view by html-----------------------------------------------------
public static void createExporterByHtml(HttpServletRequest request, HttpServletResponse response, JasperPrint jasperPrint) throws Exception{
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
PrintWriter out = ReportUtil.getPrintWriter(response);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
exporter.exportReport();// 导出
}
//-------------------- 导出pdf -----------------------------------------------------
public static void exportPdfInExplore(HttpServletResponse res, String path, Map<String, Object> parameters, JRDataSource dataSource, OutputStream out) throws Exception {
String s1 = JasperFillManager.fillReportToFile(path+".jasper", parameters, dataSource);
String s2 = JasperExportManager.exportReportToPdfFile(path+".jrprint");
File sourceFile = new File(s1);
File destFile = new File(s2);
// 设置文件头
res.setHeader("Content-Disposition", "attachment;fileName=" + new String(destFile.getName().getBytes("gbk"), "iso-8859-1"));
// 从后台下载
FileCopyUtils.copy(new FileInputStream(new File(sourceFile.getParent(), destFile.getName())), out);
}
//-------------------- 导出world -----------------------------------------------------
public static void exportDocInExplore(HttpServletResponse res, String path, Map<String, Object> parameters, JRDataSource dataSource, OutputStream out) throws Exception {
// 数据填充
String jrprint_url = JasperFillManager.fillReportToFile(path + ".jasper", parameters, dataSource);
File sourceFile = new File(jrprint_url);
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".docx");
JRDocxExporter exporter = new JRDocxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
exporter.exportReport();// 导出到doc
// 设置文件头
res.setHeader("Content-Disposition", "attachment;fileName=" + new String(destFile.getName().getBytes("gbk"), "iso-8859-1"));
// 从后台下载
FileCopyUtils.copy(new FileInputStream(new File(sourceFile.getParent(), destFile.getName())), out);
}
//-------------------- 导出excel -----------------------------------------------------
public static void exportXlsInExplore(HttpServletResponse res, String path, Map<String, Object> parameters, JRDataSource dataSource, OutputStream out) throws Exception {
// 数据填充
String jrprint_url = JasperFillManager.fillReportToFile(path + ".jasper", parameters, dataSource);
File sourceFile = new File(jrprint_url);
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls");
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(false);
exporter.setConfiguration(configuration);
exporter.exportReport();
// 设置文件头
res.setHeader("Content-Disposition", "attachment;fileName=" + new String(destFile.getName().getBytes("gbk"), "iso-8859-1"));
// 从后台下载
FileCopyUtils.copy(new FileInputStream(new File(sourceFile.getParent(), destFile.getName(
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
SpringMVC+jspereport,实现报表分页查看、文件(pdf、excel、word)导出,以及html展示和调用浏览器打印功能实现打印,报文都是采用ireport5.6.0设计
资源推荐
资源详情
资源评论
收起资源包目录
SpringMVC+jspereport,实现报表分页查看、文件(pdf、excel、word)导出,以及html展示和调用浏览器打印功能实现打印 (115个子文件)
ReportUtil.class 8KB
UserController.class 7KB
GradeDataSource.class 6KB
CustomBean.class 4KB
User.class 2KB
MonthDataSource.class 2KB
MonthDataSource2.class 2KB
MonthMsg.class 1KB
UserService.class 1KB
IUserDao.class 381B
IUserService.class 312B
.classpath 4KB
org.eclipse.wst.common.component 581B
org.eclipse.wst.jsdt.ui.superType.container 49B
execute.gif 1KB
reload.GIF 1KB
first.GIF 925B
last.GIF 924B
next.GIF 913B
first_grey.GIF 912B
previous.GIF 910B
last_grey.GIF 907B
next_grey.GIF 897B
previous_grey.GIF 895B
source.gif 292B
groovy-all-2.4.3.jar 6.65MB
jasperreports-5.6.0.jar 4.74MB
poi-3.8.jar 1.74MB
jfreechart-1.0.19.jar 1.49MB
iText-2.1.7.jar 1.08MB
spring-context-3.1.1.RELEASE.jar 812KB
mysql-connector-java-5.1.10.jar 707KB
mybatis-3.1.0.jar 633KB
c3p0-0.9.1.2.jar 596KB
spring-beans-3.1.1.RELEASE.jar 576KB
spring-webmvc-3.1.1.RELEASE.jar 560KB
quartz-2.1.7.jar 551KB
commons-collections-3.1.jar 546KB
spring-web-3.1.1.RELEASE.jar 532KB
log4j-1.2.17.jar 478KB
spring-core-3.1.1.RELEASE.jar 439KB
jstl-1.2.jar 405KB
spring-jdbc-3.1.1.RELEASE.jar 395KB
spring-aop-3.1.1.RELEASE.jar 324KB
jcommon-1.0.23.jar 323KB
itextasian-1.5.2.jar 322KB
cglib-nodep-2.2.2.jar 319KB
spring-tx-3.1.1.RELEASE.jar 241KB
commons-beanutils-1.9.2.jar 228KB
commons-digester-2.1.jar 192KB
spring-expression-3.1.1.RELEASE.jar 172KB
oscache-2.4.1.jar 134KB
commons-el-1.0.jar 110KB
spring-context-support-3.1.1.RELEASE.jar 105KB
logkit-1.0.1.jar 70KB
avalon-framework-4.1.3.jar 62KB
spring-asm-3.1.1.RELEASE.jar 52KB
commons-logging-1.1.jar 52KB
mybatis-spring-1.2.0.jar 48KB
slf4j-api-1.7.5.jar 25KB
mybatis-oscache-1.0.1.jar 10KB
slf4j-log4j12-1.7.5.jar 9KB
aopalliance-1.0.jar 4KB
SecondReport.jasper 57KB
chart2.jasper 34KB
chart.jasper 33KB
DataSourceReport.jasper 31KB
list.jasper 24KB
liste.jasper 20KB
ReportUtil.java 8KB
GradeDataSource.java 7KB
UserController.java 6KB
CustomBean.java 3KB
MonthDataSource2.java 2KB
MonthDataSource.java 2KB
User.java 2KB
MonthMsg.java 866B
UserService.java 699B
IUserDao.java 234B
IUserService.java 186B
SecondReport.jrxml 22KB
chart2.jrxml 2KB
chart.jrxml 2KB
.jsdtscope 500B
list.jsp 3KB
userList.jsp 1KB
index.jsp 573B
MANIFEST.MF 36B
MANIFEST.MF 25B
MANIFEST.MF 25B
.mymetadata 288B
.myumldata 66B
org.eclipse.wst.jsdt.ui.superType.name 6B
jasperreports.png 1KB
com.genuitec.eclipse.j2eedt.core.prefs 1009B
com.genuitec.eclipse.migration.prefs 382B
org.eclipse.jdt.core.prefs 364B
org.eclipse.ltk.core.refactoring.prefs 106B
.project 2KB
jdbc.properties 178B
共 115 条
- 1
- 2
资源评论
- error-null2016-07-09太好了,谢谢分享
- ellenzhao1112016-08-30没有数据库
- ernyj2016-02-11需要积分太多,关键是下载的内容没什么价值。。
- 「已注销」2016-03-15能把数据库给个吗?
huadaobudele
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功