博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iReport报表生成html,pdf,xls,word工具类
阅读量:6262 次
发布时间:2019-06-22

本文共 7584 字,大约阅读时间需要 25 分钟。

package com.report;

import java.io.ByteArrayOutputStream;

import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRAbstractExporter;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
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.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

/* import java.util;

<%@page import="java.io.File"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="com.report.ReportUtil"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.zhiwei.core.util.AppUtil"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="net.sf.jasperreports.j2ee.servlets.*"%>
<%@page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@page import="net.sf.jasperreports.engine.JasperRunManager"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.JRAbstractExporter"%>
<%@page import="net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.export.JRXlsExporter"%>
<%@page import="org.apache.lucene.store.jdbc.support.JdbcTemplate"%>
<%@page import="net.sf.jasperreports.engine.export.JRHtmlExporter"%>
<%@page import="net.sf.jasperreports.engine.export.ooxml.JRDocxExporter"%>
<%@page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.export.JRHtmlExporterParameter"%>*/

import com.thirdPayInteface.SinaPay.SinaPayIntefaceUtil;

import com.zhiwei.core.util.AppUtil;

/**

* 初始化加载report_config.properties
* 该文件配置的是系统中所有的报表信息key=报表名称
* @author zhangcb
*
*/
@SuppressWarnings("unchecked")
public class ReportUtil {
private static Log logger=LogFactory.getLog(SinaPayIntefaceUtil.class);

public static Map<String,String> getReportPro(){

Map<String,String> reportConfigMap=new HashMap<String,String>();
try{
Properties props = new Properties();
InputStream in = ReportUtil.class.getResourceAsStream("/com/report/report_config.properties");
props.load(in);
Iterator it= props.keySet().iterator();
while(it.hasNext()){
String key=(String)it.next();
reportConfigMap.put(key, props.getProperty(key));
}
return reportConfigMap;
}catch(Exception e){
e.printStackTrace();
logger.error(e.getMessage());
return null;
}
}
public static void createReportHtml(Map<String,String> map,String reportKey,String reportType,HttpServletRequest request){
HttpServletResponse response = ServletActionContext.getResponse();
//报表类型
Map<String,String> configMap = ReportUtil.getReportPro();
String rootPath = ReportUtil.class.getResource("").getPath().replaceAll("%20"," ");
String reportPath=configMap.get(reportKey);
String[] str=reportPath.split("/");
String reportName=str[str.length-1];
try {
DataSource dataSource = (DataSource) AppUtil.getBean("dataSource");
Connection conn = dataSource.getConnection();
File fullPath = new File(rootPath+reportPath+".jasper");
ServletOutputStream ouputStream = response.getOutputStream();
//将解析完的参数传入报表模板中并生成报表
//如果接收到的参数为pdf类型的话,则生成pdf的报表
if ("pdf".equals(reportType)) {
byte[] bytes = JasperRunManager.runReportToPdf(fullPath.getPath(), map, conn);
//设置报表生成类型为PDF
response.setContentType("application/pdf;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setContentLength(bytes.length);
reportName = reportName+".pdf";
response.setHeader( "Content-Disposition", "attachment;filename=" + new String( reportName.getBytes("gb2312"), "ISO8859-1" ) );
ouputStream.write(bytes, 0, bytes.length);
} else if ("xls".equals(reportType)) {
//如果接受到的参数为xls(excel类型)的话,则生成xls类型的报表
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
JasperPrint jasperPrint = JasperFillManager.fillReport(fullPath.getPath(), map, conn);
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,oStream);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
//设置报表生成类型为excel
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
response.setContentLength(bytes.length);
reportName = reportName+".xls";
response.setHeader( "Content-Disposition", "attachment;filename=" + new String( reportName.getBytes("gb2312"), "ISO8859-1" ) );
ouputStream.write(bytes, 0, bytes.length);
} else if("word".equals(reportType)){
reportName = reportName+".docx";
JRAbstractExporter exporter = new JRDocxExporter();
List<?> jasperPrintList = (List<?>)request.getSession().getAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE);
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
response.setContentType("application/msword;charset=utf-8");
response.setHeader( "Content-Disposition", "attachment;filename=" + new String( reportName.getBytes("utf-8"), "ISO8859-1" ) );
request.setCharacterEncoding("utf-8");
exporter.exportReport();
}else{
//否则生成html类型的报表
JRHtmlExporter exporter = new JRHtmlExporter();
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
JasperPrint jasperPrint = JasperFillManager.fillReport(fullPath.getPath(), map, conn);
//这里也是解决HTML报表图片不显示的
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);
//下面这一句是解决HTML报表图片不显示的问题,注意URI得写对,这里URI是指jasper文件所在的目录
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"jasper?"+"time="+(new Date()).toString()+"&image=");
exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING,"utf-8");
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM, oStream);
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
//设置报表生成类型的html
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentLength(bytes.length);
response.setStatus(200);
ouputStream.write(bytes, 0, bytes.length);
}
ouputStream.flush();
ouputStream.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

转载于:https://www.cnblogs.com/chinaifae/p/10188982.html

你可能感兴趣的文章
grep
查看>>
归档备份被删,GoldenGate无法抽取数据
查看>>
Could not create the view: An unexpected exception was thrown.
查看>>
codeforces 445A
查看>>
基础语法
查看>>
多线程和CPU的关系
查看>>
005 -- DuLinkList_add nodes, delete node, Caser print..
查看>>
HashMap、TreeMap、LinkedHashMap、hashtable的区别
查看>>
ajax之百度 应用实例
查看>>
单行文本溢出、多文本溢出
查看>>
yarn的学习-2-从 npm 迁移到 yarn-包管理工具
查看>>
vagrant特性——基于docker开发环境(docker和vagrant的结合)-1-基本使用
查看>>
SQL 学习——简序以及学习路线
查看>>
Quoit Design
查看>>
iOS---后台运行机制详解
查看>>
python-装饰器的最终形态和固定格式 语法糖
查看>>
iphone配置实用工具iPhone Configuration Utility
查看>>
Centos搭建开发环境,PHP7+ Nginx1.12+ Mysql5.7
查看>>
RSA的密钥把JAVA格式转换成C#的格式
查看>>
转载 HTTPS 之fiddler抓包、jmeter请求
查看>>