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();
}

}
}

iReport报表生成html,pdf,xls,word工具类的更多相关文章

  1. pdf转word工具

    pdf转word工具及安装:http://blog.sina.com.cn/s/blog_6172011c0102vxir.html pdf去加密:http://www.downxia.com/dow ...

  2. 真正免费,不限页数的PDF转Word工具

    真正免费,不限页数的PDF转Word工具 我们知道PDF转Word工具非常多,但大部分都有各种限制,限大小,限页数,加水印等等. 这其中绝大部分其实并不能做到格式完全一样,遇到图片更是直接傻了. 我们 ...

  3. WPS PDF转Word工具

    WPS PDF转Word工具链接:https://pan.baidu.com/s/1Ijh5MSBWZtsXsm05_6yYvw 提取码:gufy  下载运行后会解压到"D:\Program ...

  4. PDF转WORD工具 Solid Converter PDF v9.1.6744

    Solid Converter PDF中文破解版(pdf转换成word转换器)是一款功能强大的PDF格式转换软件.Solid Converter PDF允许用户将PDF转换为Word(PDF to W ...

  5. WordUtil java导出word工具类

    import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedWriter ...

  6. PDF转WORD工具推荐:迅捷PDF转换器6.8版

    迅捷PDF转换器是一款功能强大的pdf转换成word转换器软件.它不仅支持pdf转换成word,word转pdf,还可以实现excel,ppt,jpg与pdf文件的一键式转换.有了它,你可以很轻松转换 ...

  7. Java借助itext pdf生成固定格式pdf的模板工具类

    这里是标题区域 这里是副标题1: 副标题的内容 这里是副标题2: 这里是副标题2的内容 这里是副标题3: 这里是副标题3的内容 序号 表头1 复合表头 表头2 子表头1 子表头2 子表头3 1 居左内 ...

  8. pdf 转word 工具

    在线转换,每天有次数限制,但是很强大: https://smallpdf.com/cn 可以使用python 写代码来转换文档 参考下面博客连接 https://blog.csdn.net/Dontl ...

  9. Jacob工具类使用文件互转服务 word转html html转excel word转pdf excel转pdf ppt转pdf

    前提条件  必须安装MS office 1.jdk使用jdk1.8 2.jacob.dll放在..\jdk1.8\jre\bin目录下 3.eclipse的jre版本要和jdk一致,window-&g ...

随机推荐

  1. singleton单例模式小结

    1.饿汉模式 public class SingletonEntity2 { // 在类加载的时候创建对象:饿汉模式 public static SingletonEntity2 obj = new ...

  2. 后端token认证模板

    1.创建一个视图 from rest_framework import exceptions from app01 import models from rest_framework.authenti ...

  3. P5541 [USACO19FEB]Sleepy Cow Herding

    ri,被黄题虐. 思路:贪心?? 提交:2次 错因:没有特判 题解: 先排序. 最小代价:固定区间长度为\(n\),我们扫一遍数组看区间最多包含几个数,设为 \(mx\) ,答案就是\(n-mx+1\ ...

  4. 007_Python3 字符串

    字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. 例如: var1 = 'Hello World!' ...

  5. vue+上传文件夹

    在做项目开发的时候,上传东西无论文件也好,还是文件夹都需要用到 <input type="file" id="filepicker" name=" ...

  6. 洛谷P3119草鉴定

    题目 草鉴定,tarjan可以用来缩点,优化spfa的时间, 缩点之后就是一个\(DAG\)了,因此完全可以用来跑spfa上的最长路,然后枚举每条边,查看是否这条边的两个节点分别可以到达起点所在的强连 ...

  7. windows下安装redis、celery,并启动测试

    Windows 环境下基于 Redis 的 Celery 任务调度模块的实现 搭建环境: Windows-x64 10 Celery 3.1.23 Celery-with-redis 3.0 Redi ...

  8. IDEA中获取资源路径问题

    更正 以src开始,就能用相对路径了... shift+ctrl+alt+s 调出项目结构, 在Modules里,就是设置 Sources Resources Test的界面, 右面的路径就是相对路径 ...

  9. WPF中打开网页的两种方法

    1.浏览器打开 Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "http://www. ...

  10. JAVA基础知识|继承的几个问题

    1.子类从父类继承了什么? 子类拥有父类非private的属性,方法. 2.子类可以操作父类的非private属性吗? 子类不能继承父类的私有属性,但是如果父类中的非private方法影响到了私有属性 ...