Java使用IReport可以导出pdf, doc ,excel ,html格式的复杂报表。针对doc格式的文件,使用JRRtfExporter实现类进行导出,如此方能保留完整样式。使用JRDocxExporter实现类导出报表会出现文字线条丢失问题。
RTF格式又叫富文本格式或者多文本格式,RTF是Rich Text Format的缩写,意即丰富的文本格式。在rtf文档中可以嵌入图像等文件,RTF是word为了与其他字处理软件兼容而能够保存的文档格式,类似 DOC格式(Word文档)的文件,有很好的兼容性。
下面是报表导出工具类,需要导出以上格式的报表只需要调用本工具类中的方法即可。
package com.ntocc.util.ireport;import com.ntocc.config.RDSConnection;import net.sf.jasperreports.engine.*;import net.sf.jasperreports.engine.export.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.util.Map;/** * @author: 詹Sir * @date: 2022/08/31 * @Desc: :描述该类的主要功能 */@Componentpublic class IReportUtils { /** * 导出pdf * @param jasperFile * @param paramMap * @param defaultFileName * @param request * @param response * @throws IOException * @throws JRException */ public static void exportToPdf(String jasperFile, Map paramMap, String defaultFileName, HttpServletRequest request, HttpServletResponse response) throws IOException, JRException { if (StringUtils.isEmpty(defaultFileName)){ defaultFileName = "export.pdf"; }else { defaultFileName = defaultFileName + ".pdf"; } String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn()); ServletOutputStream ouputStream = response.getOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream); ouputStream.flush(); ouputStream.close(); } /** * 导出doc文件 * @param jasperFile * @param paramMap * @param defaultFileName * @param request * @param response * @throws IOException * @throws JRException */ public static void exportToDoc(String jasperFile, Map paramMap, String defaultFileName, HttpServletRequest request, HttpServletResponse response) throws IOException, JRException { if (StringUtils.isEmpty(defaultFileName)){ defaultFileName = "export.doc"; }else { defaultFileName = defaultFileName + ".doc"; } String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn()); //设置导出输出流 JRExporter exporter = new JRRtfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); exporter.exportReport(); } /** * 导出html文件 * @param jasperFile * @param paramMap * @param request * @param response * @throws IOException * @throws JRException */ public static void exportToHtml(String jasperFile, Map paramMap, HttpServletRequest request, HttpServletResponse response) throws IOException, JRException { response.setContentType("text/html"); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn()); ServletOutputStream ouputStream = response.getOutputStream(); JRHtmlExporter exporter = new JRHtmlExporter(); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); //设置图片文件存放路径,此路径为服务器上的绝对路径 String imageDIR =request.getSession().getServletContext().getRealPath("/"); exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, imageDIR); //设置图片请求URI String imageURI = request.getContextPath() + "/"; exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageURI); //设置导出图片到图片存放路径 exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE); exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE); exporter.exportReport(); ouputStream.flush(); ouputStream.close(); } /** * 导出xls文件 * @param jasperFile * @param paramMap * @param defaultFileName * @param request * @param response * @throws IOException * @throws JRException */ public static void exportToExcel(String jasperFile, Map paramMap, String defaultFileName, HttpServletRequest request, HttpServletResponse response) throws IOException, JRException { if (StringUtils.isEmpty(defaultFileName)){ defaultFileName = "export.xls"; }else { defaultFileName = defaultFileName + ".xls"; } String fileName = new String(defaultFileName.getBytes("GBK"), "ISO8859_1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn()); //设置导出输出流 ServletOutputStream ouputStream = response.getOutputStream(); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); // 删除记录最下面的空行 exporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除多余的ColumnHeader exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); // 显示边框 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporter.exportReport(); ouputStream.flush(); ouputStream.close(); }
留言与评论(共有 0 条评论) “” |