首页 > 其他 > 详细

IREPORT报表实现

时间:2014-11-20 02:00:56      阅读:614      评论:0      收藏:0      [点我收藏+]

IREPORT报表实现

一、在JSP中通过传参数实现源代码:(在jasper模板中定义好查询条件及参数)

bubuko.com,布布扣
bubuko.com,布布扣
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
bubuko.com,布布扣
<%@ page import="java.io.*,java.sql.*,net.sf.jasperreports.engine.*"%>
bubuko.com,布布扣
<%@ page import="com.szywit.dbapi.dbconn.CPool,net.sf.jasperreports.engine.data.JRBeanCollectionDataSource,net.sf.jasperreports.engine.util.JRLoader"%>
bubuko.com,布布扣
<jsp:directive.include file="/public_jdbc.jspf" />
bubuko.com,布布扣
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
bubuko.com,布布扣
<html>
bubuko.com,布布扣
<head>
bubuko.com,布布扣
<title>报表打印</title>
bubuko.com,布布扣
</head>
bubuko.com,布布扣
bubuko.com,布布扣
<body id="Body">
bubuko.com,布布扣    
<%
bubuko.com,布布扣        String parentid 
= "",sect_name="",actcode="",run_types="";
bubuko.com,布布扣        CommonModel CM;
bubuko.com,布布扣        String reportName 
= request.getParameter("reportName");
bubuko.com,布布扣        
if (request.getParameter("parentid"!= null)
bubuko.com,布布扣            parentid 
= request.getParameter("parentid");
bubuko.com,布布扣        
if (request.getParameter("pect_name"!= null)
bubuko.com,布布扣            sect_name 
= request.getParameter("pect_name");
bubuko.com,布布扣        
if (request.getParameter("actcode"!= null)
bubuko.com,布布扣            actcode 
= request.getParameter("actcode");
bubuko.com,布布扣        
if (request.getParameter("run_types"!= null)
bubuko.com,布布扣            run_types 
= request.getParameter("run_types");
bubuko.com,布布扣
bubuko.com,布布扣        
//报表编译之后的.jasper文件的存放位置
bubuko.com,布布扣
        File reportFile = new File(
bubuko.com,布布扣                application.getRealPath(
"/ems/report/eventLog.jasper"));
bubuko.com,布布扣        
//传递报表中用到的参数值,这里是空值
bubuko.com,布布扣
        System.out.println("param:=" + parentid);
bubuko.com,布布扣        Map parameters 
= new HashMap();
bubuko.com,布布扣        System.out.print(sect_name);
bubuko.com,布布扣                   parameters.put(
"event_code""%"+event_code+"%");
bubuko.com,布布扣           parameters.put(
"event_name",  "%"+event_name+"%");
bubuko.com,布布扣           parameters.put(
"type_code2",  "%"+parentid+"%");
bubuko.com,布布扣        
bubuko.com,布布扣        
bubuko.com,布布扣        
bubuko.com,布布扣           parameters.put(
"type_code""%"+parentid+"%");
bubuko.com,布布扣           parameters.put(
"type_code1",  "%"+parentid+"%");
bubuko.com,布布扣           parameters.put(
"type_code2",  "%"+parentid+"%");
bubuko.com,布布扣           
bubuko.com,布布扣           parameters.put(
"sect_name",  "%"+sect_name+"%");
bubuko.com,布布扣           parameters.put(
"actcode",  "%"+actcode+"%");
bubuko.com,布布扣           parameters.put(
"run_types",  "%"+run_types+"%");
bubuko.com,布布扣
bubuko.com,布布扣        
//连接到数据库
bubuko.com,布布扣
        Connection conn = null;
bubuko.com,布布扣        
try {
bubuko.com,布布扣            conn 
= ConnectionManager.getConnection("EMSDATACON");
bubuko.com,布布扣        }
 catch (Exception e) {
bubuko.com,布布扣            e.printStackTrace();
bubuko.com,布布扣            
throw new Exception("创建默认数据库连接不成功,请检查dbconn.properties是否正确配置");
bubuko.com,布布扣        }

bubuko.com,布布扣        System.out.println(
"----------jasper begin-----------");
bubuko.com,布布扣        
//在控制台打印报表文件的物理路径 
bubuko.com,布布扣
        System.out.println("****物理路径 ***" + reportFile.getPath());
bubuko.com,布布扣        
byte[] bytes = JasperRunManager.runReportToPdf(
bubuko.com,布布扣                reportFile.getPath(), parameters, conn);
bubuko.com,布布扣            
bubuko.com,布布扣                
bubuko.com,布布扣        System.out.println(
"bytes=" + parameters);
bubuko.com,布布扣        System.out.println(
"---------jasper end-------");
bubuko.com,布布扣        response.setContentType(
"application/pdf");
bubuko.com,布布扣        response.setContentLength(bytes.length);
bubuko.com,布布扣
bubuko.com,布布扣        
//清除输出对象的冲突(因jsp本身有自己的out输出对象,如果去除下面的语句,则response.getOutputStream()得到的输出对象与原来存在的out对象冲突,会报异常)
bubuko.com,布布扣

bubuko.com,布布扣        out.clear();
bubuko.com,布布扣        out 
= pageContext.pushBody();
bubuko.com,布布扣
bubuko.com,布布扣        ServletOutputStream output 
= response.getOutputStream();
bubuko.com,布布扣        output.write(bytes, 
0, bytes.length);
bubuko.com,布布扣        output.flush();
bubuko.com,布布扣        output.close();
bubuko.com,布布扣
bubuko.com,布布扣        
try {
bubuko.com,布布扣            
if (conn != null{
bubuko.com,布布扣                conn.close();
bubuko.com,布布扣            }

bubuko.com,布布扣        }
 catch (Exception e) {
bubuko.com,布布扣            e.printStackTrace();
bubuko.com,布布扣        }

bubuko.com,布布扣    
%>
bubuko.com,布布扣
</body>
bubuko.com,布布扣
</html>
bubuko.com,布布扣
bubuko.com,布布扣

二、通过传递结果集,JAVA实现

bubuko.com,布布扣<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
bubuko.com,布布扣
<%@ page language="java" import="com.szywit.file.ReportExport"%>
bubuko.com,布布扣
<%@ page
bubuko.com,布布扣    
import="java.io.*,net.sf.jasperreports.engine.*,
bubuko.com,布布扣
net.sf.jasperreports.engine.util.JRLoader,net.sf.jasperreports.engine.data.JRBeanCollectionDataSource,net.sf.jasperreports.engine.export.JRRtfExporter"%>
bubuko.com,布布扣
<jsp:directive.include file="/public_jdbc.jspf" />
bubuko.com,布布扣
<%
bubuko.com,布布扣    String parentid 
= "", sect_name = "", actcode = "", run_types = "", types = "", typedot = "";
bubuko.com,布布扣    List list 
= new ArrayList();
bubuko.com,布布扣    File reportFile 
= new File(
bubuko.com,布布扣            application.getRealPath(
"/ems/report/preschDetail.jasper"));
bubuko.com,布布扣
bubuko.com,布布扣    Vector rsVector;
bubuko.com,布布扣    CommonModel CM;
bubuko.com,布布扣
bubuko.com,布布扣    
//传参
bubuko.com,布布扣
    if (request.getParameter("types"!= null)
bubuko.com,布布扣        types 
= request.getParameter("types");
bubuko.com,布布扣    
if (request.getParameter("parentid"!= null)
bubuko.com,布布扣        parentid 
= request.getParameter("parentid");
bubuko.com,布布扣    
if (request.getParameter("pect_name"!= null)
bubuko.com,布布扣        sect_name 
= request.getParameter("pect_name");
bubuko.com,布布扣    
if (request.getParameter("actcode"!= null)
bubuko.com,布布扣        actcode 
= request.getParameter("actcode");
bubuko.com,布布扣    
if (request.getParameter("run_types"!= null)
bubuko.com,布布扣        run_types 
= request.getParameter("run_types");
bubuko.com,布布扣
bubuko.com,布布扣    String sql 
= "select  * from vw_preschdetail  where ( type_code like ‘%"
bubuko.com,布布扣            
+ parentid
bubuko.com,布布扣            
+ "%‘"
bubuko.com,布布扣            
+ "or type_code2 like   ‘%"
bubuko.com,布布扣            
+ parentid
bubuko.com,布布扣            
+ "%‘  or type_code1 like   ‘%"
bubuko.com,布布扣            
+ parentid
bubuko.com,布布扣            
+ "%‘)"
bubuko.com,布布扣            
+ "and (sect_name like  ‘%"
bubuko.com,布布扣            
+ sect_name
bubuko.com,布布扣            
+ "%‘)  and (run_types like   ‘%"
bubuko.com,布布扣            
+ run_types
bubuko.com,布布扣            
+ "%‘)"
bubuko.com,布布扣            
+ " and (actcode like   ‘%" + actcode + "%‘)";
bubuko.com,布布扣
bubuko.com,布布扣    
try {
bubuko.com,布布扣        rsVector 
= CCommonDAO.select(sql, "EMSDATACON");
bubuko.com,布布扣        
if (rsVector != null && !rsVector.isEmpty()) {
bubuko.com,布布扣            
for (int j = 0; j < rsVector.size(); j++{
bubuko.com,布布扣                CM 
= (CommonModel) rsVector.get(j);
bubuko.com,布布扣                Map map 
= new HashMap();
bubuko.com,布布扣                map.put(
"sect_names", CM.getAttrValue("sect_names")
bubuko.com,布布扣                        .trim());
bubuko.com,布布扣                map.put(
"type_name", CM.getAttrValue("type_name")
bubuko.com,布布扣                        .trim());
bubuko.com,布布扣                map.put(
"actcode", CM.getAttrValue("actcode").trim());
bubuko.com,布布扣                map.put(
"actconts", CM.getAttrValue("actconts").trim());
bubuko.com,布布扣                map.put(
"run_types", CM.getAttrValue("run_types")
bubuko.com,布布扣                        .trim());
bubuko.com,布布扣                map.put(
"car_lens", CM.getAttrValue("car_lens").trim());
bubuko.com,布布扣
bubuko.com,布布扣                map.put(
"dev_info", CM.getAttrValue("dev_info").trim());
bubuko.com,布布扣                map.put(
"vehi_info", CM.getAttrValue("vehi_info")
bubuko.com,布布扣                        .trim());
bubuko.com,布布扣                map.put(
"vehisplit_info",
bubuko.com,布布扣                        CM.getAttrValue(
"vehisplit_info").trim());
bubuko.com,布布扣                map.put(
"car_lens", CM.getAttrValue("car_lens").trim());
bubuko.com,布布扣                map.put(
"pub_info", CM.getAttrValue("pub_info").trim());
bubuko.com,布布扣                list.add(map);
bubuko.com,布布扣            }

bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        String filenames 
= "预案台帐"
bubuko.com,布布扣                
+ CAutoSerialNumber.getServerDateTime();
bubuko.com,布布扣        
//String filenames = "预案台帐." + typedot;
bubuko.com,布布扣
        ReportExport.export(list, filenames, types, reportFile,
bubuko.com,布布扣                request, response);
bubuko.com,布布扣
/*         out.clear();
bubuko.com,布布扣        out = pageContext.pushBody(); 
*/

bubuko.com,布布扣        
/*         JRDataSource ds = new JRBeanCollectionDataSource(list, false);
bubuko.com,布布扣         JasperReport jasperReport = (JasperReport) JRLoader
bubuko.com,布布扣         .loadObject(reportFile);
bubuko.com,布布扣         Map parameters = new HashMap();
bubuko.com,布布扣         JasperPrint jasperPrint = JasperFillManager.fillReport(
bubuko.com,布布扣         jasperReport, parameters, ds);
bubuko.com,布布扣
bubuko.com,布布扣         response.setContentType("application/msword;charset=utf-8");
bubuko.com,布布扣         String files = "预案台帐" + CAutoSerialNumber.getServerDateTime() + ".doc";
bubuko.com,布布扣         String fileName = new String(files.getBytes("GBK"), "ISO8859_1");
bubuko.com,布布扣         response.setHeader("Content-disposition",
bubuko.com,布布扣         "attachment; filename=" + fileName);
bubuko.com,布布扣        
bubuko.com,布布扣         OutputStream os=response.getOutputStream();   
bubuko.com,布布扣
bubuko.com,布布扣         JRExporter exporter = new JRRtfExporter();
bubuko.com,布布扣         exporter.setParameter(JRExporterParameter.JASPER_PRINT,
bubuko.com,布布扣         jasperPrint);
bubuko.com,布布扣         exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
bubuko.com,布布扣         os);
bubuko.com,布布扣
bubuko.com,布布扣         exporter.exportReport();
bubuko.com,布布扣        
bubuko.com,布布扣        
bubuko.com,布布扣
bubuko.com,布布扣         out.clear();   
bubuko.com,布布扣         out = pageContext.pushBody();  
*/

bubuko.com,布布扣
bubuko.com,布布扣    }
 catch (Exception e) {
bubuko.com,布布扣        e.printStackTrace();
bubuko.com,布布扣
bubuko.com,布布扣    }

bubuko.com,布布扣
%>

JAVA代码

bubuko.com,布布扣package com.szywit.file;
bubuko.com,布布扣
bubuko.com,布布扣
import java.io.File;
bubuko.com,布布扣
import java.io.IOException;
bubuko.com,布布扣
import java.io.InputStream;
bubuko.com,布布扣
import java.io.ObjectOutputStream;
bubuko.com,布布扣
import java.io.UnsupportedEncodingException;
bubuko.com,布布扣
import java.lang.reflect.Field;
bubuko.com,布布扣
import java.util.Collection;
bubuko.com,布布扣
import java.util.HashMap;
bubuko.com,布布扣
import java.util.Map;
bubuko.com,布布扣
bubuko.com,布布扣
import javax.servlet.ServletOutputStream;
bubuko.com,布布扣
import javax.servlet.http.HttpServletRequest;
bubuko.com,布布扣
import javax.servlet.http.HttpServletResponse;
bubuko.com,布布扣
bubuko.com,布布扣
import net.sf.jasperreports.engine.JRDataSource;
bubuko.com,布布扣
import net.sf.jasperreports.engine.JRException;
bubuko.com,布布扣
import net.sf.jasperreports.engine.JRExporter;
bubuko.com,布布扣
import net.sf.jasperreports.engine.JRExporterParameter;
bubuko.com,布布扣
import net.sf.jasperreports.engine.JasperExportManager;
bubuko.com,布布扣
import net.sf.jasperreports.engine.JasperFillManager;
bubuko.com,布布扣
import net.sf.jasperreports.engine.JasperPrint;
bubuko.com,布布扣
import net.sf.jasperreports.engine.JasperReport;
bubuko.com,布布扣
import net.sf.jasperreports.engine.base.JRBaseReport;
bubuko.com,布布扣
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
bubuko.com,布布扣
import net.sf.jasperreports.engine.export.JRHtmlExporter;
bubuko.com,布布扣
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
bubuko.com,布布扣
import net.sf.jasperreports.engine.export.JRRtfExporter;
bubuko.com,布布扣
import net.sf.jasperreports.engine.export.JRXlsExporter;
bubuko.com,布布扣
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
bubuko.com,布布扣
import net.sf.jasperreports.engine.util.JRLoader;
bubuko.com,布布扣
bubuko.com,布布扣
/**
bubuko.com,布布扣 * 按照类型导出不同格式文件
bubuko.com,布布扣 * 
bubuko.com,布布扣 * 
@param datas
bubuko.com,布布扣 *            数据
bubuko.com,布布扣 * 
@param type
bubuko.com,布布扣 *            文件类型
bubuko.com,布布扣 * 
@param is
bubuko.com,布布扣 *            jasper文件的来源
bubuko.com,布布扣 * 
@param request
bubuko.com,布布扣 * 
@param response
bubuko.com,布布扣 
*/

bubuko.com,布布扣
bubuko.com,布布扣
public class ReportExport {
bubuko.com,布布扣    
public static void prepareReport(JasperReport jasperReport, String type) {
bubuko.com,布布扣        
/*
bubuko.com,布布扣         * 如果导出的是excel,则需要去掉周围的margin
bubuko.com,布布扣         
*/

bubuko.com,布布扣        
if ("excel".equals(type))
bubuko.com,布布扣            
try {
bubuko.com,布布扣                Field margin 
= JRBaseReport.class
bubuko.com,布布扣                        .getDeclaredField(
"leftMargin");
bubuko.com,布布扣                margin.setAccessible(
true);
bubuko.com,布布扣                margin.setInt(jasperReport, 
0);
bubuko.com,布布扣                margin 
= JRBaseReport.class.getDeclaredField("topMargin");
bubuko.com,布布扣                margin.setAccessible(
true);
bubuko.com,布布扣                margin.setInt(jasperReport, 
0);
bubuko.com,布布扣                margin 
= JRBaseReport.class.getDeclaredField("bottomMargin");
bubuko.com,布布扣                margin.setAccessible(
true);
bubuko.com,布布扣                margin.setInt(jasperReport, 
0);
bubuko.com,布布扣                Field pageHeight 
= JRBaseReport.class
bubuko.com,布布扣                        .getDeclaredField(
"pageHeight");
bubuko.com,布布扣                pageHeight.setAccessible(
true);
bubuko.com,布布扣                pageHeight.setInt(jasperReport, 
2147483647);
bubuko.com,布布扣            }
 catch (Exception exception) {
bubuko.com,布布扣            }

bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 导出excel
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void exportExcel(JasperPrint jasperPrint,String filename,
bubuko.com,布布扣            HttpServletRequest request, HttpServletResponse response)
bubuko.com,布布扣            
throws IOException, JRException {
bubuko.com,布布扣        
/*
bubuko.com,布布扣         * 设置头信息
bubuko.com,布布扣         
*/

bubuko.com,布布扣        
if (filename.trim() != null && filename != null{
bubuko.com,布布扣            filename 
= filename + ".xls";
bubuko.com,布布扣        }
 else {
bubuko.com,布布扣            filename 
= "export.xls";
bubuko.com,布布扣        }

bubuko.com,布布扣            
bubuko.com,布布扣        response.setContentType(
"application/vnd.ms-excel");
bubuko.com,布布扣        String fileName 
= new String(filename.getBytes("GBK"), "ISO8859_1");
bubuko.com,布布扣        response.setHeader(
"Content-disposition""attachment; filename="
bubuko.com,布布扣                
+ fileName);
bubuko.com,布布扣
bubuko.com,布布扣        ServletOutputStream ouputStream 
= response.getOutputStream();
bubuko.com,布布扣        JRXlsExporter exporter 
= new JRXlsExporter();
bubuko.com,布布扣        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
bubuko.com,布布扣        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
bubuko.com,布布扣        exporter.setParameter(
bubuko.com,布布扣                JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
bubuko.com,布布扣                Boolean.TRUE);
bubuko.com,布布扣        exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
bubuko.com,布布扣                Boolean.FALSE);
bubuko.com,布布扣        exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
bubuko.com,布布扣                Boolean.FALSE);
bubuko.com,布布扣        exporter.exportReport();
bubuko.com,布布扣        ouputStream.flush();
bubuko.com,布布扣        ouputStream.close();
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 导出pdf,注意此处中文问题, 1)在ireport的classpath中加入iTextAsian.jar
bubuko.com,布布扣     * 2)在ireport画jrxml时,pdf font name :STSong-Light ,pdf encoding :
bubuko.com,布布扣     * UniGB-UCS2-H
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void exportPdf(JasperPrint jasperPrint,
bubuko.com,布布扣            HttpServletRequest request, HttpServletResponse response)
bubuko.com,布布扣            
throws IOException, JRException {
bubuko.com,布布扣        response.setContentType(
"application/pdf");
bubuko.com,布布扣        String fileName 
= new String("未命名.pdf".getBytes("GBK"), "ISO8859_1");
bubuko.com,布布扣        response.setHeader(
"Content-disposition""attachment; filename="
bubuko.com,布布扣                
+ fileName);
bubuko.com,布布扣        ServletOutputStream ouputStream 
= response.getOutputStream();
bubuko.com,布布扣        JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream);
bubuko.com,布布扣        ouputStream.flush();
bubuko.com,布布扣        ouputStream.close();
bubuko.com,布布扣    }

bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 在线显示pdf,
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void exportLinePdf(JasperPrint jasperPrint,String defaultFilename,
bubuko.com,布布扣            HttpServletRequest request, HttpServletResponse response)
bubuko.com,布布扣            
throws IOException, JRException {
bubuko.com,布布扣    
bubuko.com,布布扣        String defaultname 
= null;
bubuko.com,布布扣        
try {
bubuko.com,布布扣            response.setContentType(
"application/pdf");
bubuko.com,布布扣            response.setCharacterEncoding(
"UTF-8");
bubuko.com,布布扣            
if (defaultFilename.trim() != null && defaultFilename != null{
bubuko.com,布布扣                defaultname 
= defaultFilename + ".pdf";
bubuko.com,布布扣            }
 else {
bubuko.com,布布扣                defaultname 
= "export.pdf";
bubuko.com,布布扣            }

bubuko.com,布布扣            response.setHeader(
"Content-disposition""inline; filename="+defaultname);
bubuko.com,布布扣            ServletOutputStream ouputStream 
= response.getOutputStream();
bubuko.com,布布扣            JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream);
bubuko.com,布布扣            ouputStream.flush();
bubuko.com,布布扣            ouputStream.close();
bubuko.com,布布扣        }
 catch (Exception e) {
bubuko.com,布布扣            System.out.println(
"Jasper Output Error:" + e.getMessage());
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 导出html
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void exportHtml(JasperPrint jasperPrint,
bubuko.com,布布扣            HttpServletRequest request, HttpServletResponse response)
bubuko.com,布布扣            
throws IOException, JRException {
bubuko.com,布布扣        response.setContentType(
"text/html");
bubuko.com,布布扣        ServletOutputStream ouputStream 
= response.getOutputStream();
bubuko.com,布布扣        JRHtmlExporter exporter 
= new JRHtmlExporter();
bubuko.com,布布扣        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
bubuko.com,布布扣                Boolean.FALSE);
bubuko.com,布布扣        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
bubuko.com,布布扣        exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, 
"UTF-8");
bubuko.com,布布扣        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
bubuko.com,布布扣
bubuko.com,布布扣        exporter.exportReport();
bubuko.com,布布扣
bubuko.com,布布扣        ouputStream.flush();
bubuko.com,布布扣        ouputStream.close();
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 导出word
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void exportWord(JasperPrint jasperPrint,String filename,
bubuko.com,布布扣            HttpServletRequest request, HttpServletResponse response)
bubuko.com,布布扣            
throws JRException, IOException {
bubuko.com,布布扣        
if (filename.trim() != null && filename != null{
bubuko.com,布布扣            filename 
= filename + ".xls";
bubuko.com,布布扣        }
 else {
bubuko.com,布布扣            filename 
= "export.xls";
bubuko.com,布布扣        }

bubuko.com,布布扣        response.setContentType(
"application/msword;charset=utf-8");
bubuko.com,布布扣        String fileName 
= new String(filename.getBytes("GBK"), "ISO8859_1");
bubuko.com,布布扣        response.setHeader(
"Content-disposition""attachment; filename="
bubuko.com,布布扣                
+ fileName);
bubuko.com,布布扣        ServletOutputStream ouputStream 
= response.getOutputStream();
bubuko.com,布布扣        JRExporter exporter 
= new JRRtfExporter();
bubuko.com,布布扣        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
bubuko.com,布布扣        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
bubuko.com,布布扣                ouputStream);
bubuko.com,布布扣
bubuko.com,布布扣        exporter.exportReport();
bubuko.com,布布扣        ouputStream.flush();
bubuko.com,布布扣        ouputStream.close();
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 打印
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void exportPrint(JasperPrint jasperPrint,
bubuko.com,布布扣            HttpServletResponse response, HttpServletRequest request)
bubuko.com,布布扣            
throws IOException {
bubuko.com,布布扣        response.setContentType(
"application/octet-stream");
bubuko.com,布布扣        ServletOutputStream ouputStream 
= response.getOutputStream();
bubuko.com,布布扣        ObjectOutputStream oos 
= new ObjectOutputStream(ouputStream);
bubuko.com,布布扣        oos.writeObject(jasperPrint);
bubuko.com,布布扣        oos.flush();
bubuko.com,布布扣        oos.close();
bubuko.com,布布扣        ouputStream.flush();
bubuko.com,布布扣        ouputStream.close();
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
public static void export(Collection datas,String filename, String type, File is,
bubuko.com,布布扣            HttpServletRequest request, HttpServletResponse response) 
{
bubuko.com,布布扣        
try {
bubuko.com,布布扣            JasperReport jasperReport 
= (JasperReport) JRLoader.loadObject(is);
bubuko.com,布布扣            prepareReport(jasperReport, type);
bubuko.com,布布扣            JRDataSource ds 
= new JRBeanCollectionDataSource(datas, false);
bubuko.com,布布扣            Map parameters 
= new HashMap();
bubuko.com,布布扣            JasperPrint jasperPrint 
= JasperFillManager.fillReport(
bubuko.com,布布扣                    jasperReport, parameters, ds);
bubuko.com,布布扣
bubuko.com,布布扣            
if (EXCEL_TYPE.equals(type)) {
bubuko.com,布布扣                exportExcel(jasperPrint,filename,request, response);
bubuko.com,布布扣            }
 else if (PDF_TYPE.equals(type)) {
bubuko.com,布布扣                exportPdf(jasperPrint, request, response);
bubuko.com,布布扣            }
 else if (HTML_TYPE.equals(type)) {
bubuko.com,布布扣                exportHtml(jasperPrint, request, response);
bubuko.com,布布扣            }
 else if (WORD_TYPE.equals(type)) {
bubuko.com,布布扣                exportWord(jasperPrint, filename,request, response);
bubuko.com,布布扣            }
else if (LINE_TYPE.equals(type)) {
bubuko.com,布布扣                exportLinePdf(jasperPrint,filename,request, response);
bubuko.com,布布扣            }

bubuko.com,布布扣        }
 catch (Exception e) {
bubuko.com,布布扣            e.printStackTrace();
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
public static final String PRINT_TYPE = "print";
bubuko.com,布布扣    
public static final String PDF_TYPE = "pdf";
bubuko.com,布布扣    
public static final String EXCEL_TYPE = "excel";
bubuko.com,布布扣    
public static final String HTML_TYPE = "html";
bubuko.com,布布扣    
public static final String WORD_TYPE = "word";
bubuko.com,布布扣    
public static final String LINE_TYPE = "linePdf";
bubuko.com,布布扣
bubuko.com,布布扣}

bubuko.com,布布扣




 

IREPORT报表实现

原文:http://www.blogjava.net/fiele/archive/2014/11/19/420284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!