ExportServlet

Web.xml

<servlet>
        <servlet-name>ExportServlet</servlet-name>
        <servlet-class>
            com.XXXX.oss.jasperreports.utils.ExportServlet
        </servlet-class>

</servlet>

 <servlet-mapping>
       <servlet-name>ExportServlet</servlet-name>
       <url-pattern>/jasperreports/commons/export</url-pattern>
</servlet-mapping>

ExportServlet.java

package com.XXXX.oss.jasperreports.utils;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author  king
 * @date 20120201
 */


public class ExportServlet extends HttpServlet {

    private Log log = LogFactory.getLog(ExportServlet.class);

    private static final long serialVersionUID = 1626331706517382929L;

    @Override
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        JasperPrint jasperPrint = (JasperPrint) request.getSession()
                .getAttribute(
                        ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE);

        if (jasperPrint == null) {
            throw new ServletException("请先进行报表查询!");
        }

        response.setHeader("Content-Disposition", "attachment;filename=" + "report.xls");
        //response.setHeader("Content-Disposition", "filename=" + "report.xls");
        
        response.setCharacterEncoding("GBK");

        //response.setContentType("application/vnd.ms-excel");
        
        response.setContentType("application/x-msdownload");

        ServletOutputStream os = response.getOutputStream();

        JRXlsExporter xlsExporter = new JRXlsExporter();

        xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

        xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);

        xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

        xlsExporter.setParameter(
                JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE);

        try {
            xlsExporter.exportReport();

            os.flush();
            os.close();
        } catch (Exception e) {
            log.error("", e);
            throw new ServletException("导出数据出错:" + e.getMessage());
        }
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值