Java 使用 IReport 导出复杂报表
作者:源字节1号
- 2022 年 8 月 04 日
本文字数:3516 字
阅读完需:约 12 分钟
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: :描述该类的主要功能
*/
@Component
public class IReportUtils {
/**
* 导出pdf
* @param jasperFile
* @param paramMap
* @param defaultFileName
* @param request
* @param response
* @throws IOException
* @throws JRException
*/
public static void exportToPdf(String jasperFile, Map<String,Object> 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<String,Object> 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<String,Object> 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<String,Object> 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();
}
复制代码
如若转载,请注明出处:开源字节 https://sourcebyte.cn/article/211.html
划线
评论
复制
发布于: 刚刚阅读数: 3
源字节1号
关注
一个着迷于技术又喜欢不断折腾的技术活跃者 2022.03.09 加入
一个着迷于技术又喜欢不断折腾的技术活跃者。喜欢并热爱编程,执着于努力之后所带来的美好生活!
评论