您的位置 首页 java

Java使用IReport导出复杂报表

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.HttpServlet request ;
import javax.servlet.http.HttpServletResponse;
import java.io.IO Exception ;
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.set Header ("Content-disposition", "attachment; filename="
                + fileName);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile,paramMap, getConn());
        ServletOutputStream ouputStream = response.get Output Stream();
        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();
    }  

文章来源:智云一二三科技

文章标题:Java使用IReport导出复杂报表

文章地址:https://www.zhihuclub.com/189573.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

网站地图