/** * @Description: 将模板导出为Excel * @author: * @date: 2018年3月22日 下午8:57:24 * @param pagename 模板文件名称 * @param params 导出参数 * @param javaBean 报表数据源实体类 * @param request 请求对象 * @param response 响应对象 * @throws JRException Jasper异常 * @throws IOException IO异常 */ @Override public void exportTemplateToExcel(String pagename, Map<String, Object> params, Object javaBean, HttpServletRequest request, HttpServletResponse response) throws JRException, IOException { List<Object> list = new ArrayList<Object>(); list.add(javaBean); // 获取模板 JasperReport jasperReport = getJasperReport(pagename); // 创建数据源 JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list); // 设置jasper文件夹的路径 params = setJasperPath(params); // 填充模版 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource); // 生成文件名 String fileName = UUID.randomUUID().toString().toUpperCase() + ".xlsx"; // 资源文件完整路径 String completePath = getPdfUrl(request, fileName); // 服务器文件临时路径 String templatePath = getTempFoleder() + "/" + fileName; // 设置response参数,用于打开下载页面 response.reset(); response.setContentType("application/x-msdownload;"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1")); // 创建Jasper Excel导出类 JRXlsxExporter exporter = new JRXlsxExporter(); // 设置导出配置项 SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration(); conf.setWhitePageBackground(false); conf.setDetectCellType(true); exporter.setConfiguration(conf); // 设置输入项 ExporterInput exporterInput = new SimpleExporterInput(jasperPrint); exporter.setExporterInput(exporterInput); // 设置输出项 OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(templatePath); exporter.setExporterOutput(exporterOutput); //导出报表 exporter.exportReport(); // 重定向到资源文件url response.sendRedirect(completePath); exporterOutput.close(); } /** * 导出模板为PDF文件 * @param pagename * @param params * @param javaBean * @param request * @param response * @throws JRException * @throws IOException */ @Override public void exportTemplateToPdf(String pagename, Map<String, Object> params, Object javaBean, HttpServletRequest request, HttpServletResponse response) throws JRException, IOException { List<Object> list = new ArrayList<Object>(); list.add(javaBean); // 获取模板 JasperReport jasperReport = getJasperReport(pagename); // 创建数据源 JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list); // 设置jasper文件夹的路径 params = setJasperPath(params); // 填充模版 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource); // 生成文件名 String fileName = UUID.randomUUID().toString().toUpperCase() + ".pdf"; // 资源文件完整路径 String completePath = getPdfUrl(request, fileName); // 服务器文件临时路径 String templatePath = getTempFoleder() + "/" + fileName; // 设置response参数,用于打开下载页面 response.reset(); response.setContentType("application/pdf;"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1")); //定义报表输出源 JRPdfExporter exporter = new JRPdfExporter(); // 设置输入项 ExporterInput exporterInput = new SimpleExporterInput(jasperPrint); exporter.setExporterInput(exporterInput); // 设置输出项 OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(templatePath); exporter.setExporterOutput(exporterOutput); //导出报表 exporter.exportReport(); // 重定向到资源文件url response.sendRedirect(completePath); exporterOutput.close(); }
原文地址:https://www.cnblogs.com/qiushuiblog/p/10821145.html
时间: 2024-10-07 18:08:51