JasperReport2ExcelUtil

package com.xx.utils.jasperReport;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.export.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @description: JasperReport转为excel工具类
 */
public class JasperReport2ExcelUtil {
    /**
     * jasper生成excel文件过程
     * @param reportType
     * @param response
     * @param jasperReport jasperReport对象
     * @param params
     * @param list
     * @throws JRException
     * @throws UnsupportedEncodingException
     * @throws IOException
     */
    public static void jasperToExcel(String reportType, HttpServletResponse response, JasperReport jasperReport, Map<String, Object> params, List list) throws JRException, UnsupportedEncodingException, IOException {
        JRDataSource dataSource = new JRBeanCollectionDataSource(list, true);
        // print文件
        JasperPrint print = JasperFillManager.fillReport(jasperReport, params, dataSource);
        exportReportToExcel(reportType, response, print);
    }

    /**
     * jasper生成excel文件过程
     * @param reportType
     * @param response
     * @param jasperPath (主表的)jasper文件的路径
     * @param params
     * @param list
     * @throws JRException
     * @throws UnsupportedEncodingException
     * @throws IOException
     */
    public static void jasperToExcel(String reportType, HttpServletResponse response, String jasperPath, Map<String, Object> params, List list) throws JRException, UnsupportedEncodingException, IOException {
        JRDataSource dataSource = new JRBeanCollectionDataSource(list, true);
        // print文件
        JasperPrint print = JasperFillManager.fillReport(jasperPath, params, dataSource);
        exportReportToExcel(reportType, response, print);
    }

    /**
     * 导出报表为excel
     * @param reportType
     * @param response
     * @param print print对象
     * @throws IOException
     * @throws JRException
     */
    private static void exportReportToExcel(String reportType, HttpServletResponse response, JasperPrint print) throws IOException, JRException {
        // 设置导出时参数
        SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();
        conf.setWhitePageBackground(false);
        conf.setDetectCellType(true);

        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setConfiguration(conf);

        response.reset();

        response.setContentType("application/msexcel");

        response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(reportType, "UTF-8") + ".xls");
        // 设置输入项
        ExporterInput exporterInput = new SimpleExporterInput(print);
        exporter.setExporterInput(exporterInput);

        // 设置输出项
        OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(response.getOutputStream());
        exporter.setExporterOutput(exporterOutput);

        // 设置导出表的格式和配置
        SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
        configuration.setCollapseRowSpan(false);
        configuration.setAutoFitPageHeight(true);
        configuration.setWhitePageBackground(false);
        configuration.setRemoveEmptySpaceBetweenRows(true);
        configuration.setRemoveEmptySpaceBetweenColumns(true);
        exporter.setConfiguration(configuration);

        exporter.exportReport();

        response.setContentType("application/json;charset=UTF-8");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值