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");
}
}
JasperReport2ExcelUtil
最新推荐文章于 2024-06-13 15:23:35 发布