JasperReport导出工具类

8 篇文章 0 订阅
6 篇文章 0 订阅
package com.example.wordexport.utils;

import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.export.HtmlExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;
import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.Map;

@Slf4j
public class JasperReportUtil {
    final static String jasperDir = "report";

 
    public static String getJasperFileDir(String fileName) {
        return jasperDir + File.separator + fileName + ".jasper";
    }
 
    private static String getContentType(ReportType type) {
        String contentType;
        switch (type) {
            case HTML:
                contentType = "text/html;charset=utf-8";
                break;
            case PDF:
                contentType = "application/pdf";
                break;
            case IMAGE:
                contentType = "image/jpeg";
                break;
            case XLS:
                contentType = "application/vnd.ms-excel";
                break;
            case XLSX:
                contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
            case XML:
                contentType = "text/xml";
                break;
            case RTF:
                contentType = "application/rtf";
                break;
            case CSV:
                contentType = "text/plain";
                break;
            case DOC:
                contentType = "application/msword";
                break;
            default:
                contentType = "text/html;charset=utf-8";
        }
        return contentType;
    }
 
    static JasperPrint getJasperPrint(InputStream jasperStream, Map parameters, List<?> list) throws JRException {
        JRDataSource dataSource = null;
        if (null == list || list.size() == 0) {
            dataSource = new JREmptyDataSource();
        } else {
            dataSource = new JRBeanCollectionDataSource(list);
        }
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperStream, parameters, dataSource);
        return jasperPrint;
    }
 
    public static void exportToPdf(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {
        OutputStream outputStream = response.getOutputStream();
        try {
            ClassPathResource resource = new ClassPathResource(jasperPath);
            response.setContentType(getContentType(ReportType.PDF));
            InputStream jasperStream = resource.getInputStream();
            JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
 
            JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
 
        } catch (Exception e) {
            log.error("读取报表异常", e);
            System.out.println("读取报表异常"+ e.toString());
            outputStream.write("读取报表异常".getBytes());
        } finally {
            outputStream.flush();
            outputStream.close();
        }
    }
 
    public static void exportToPdfFile(String jasperPath, Map parameters, List<?> list,String desFilePath) throws Exception {
        try {
            ClassPathResource resource = new ClassPathResource(jasperPath);
            InputStream jasperStream = resource.getInputStream();
            JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
 
            System.out.println("写入文件路径:"+desFilePath);
            // 输出文档
            JasperExportManager.exportReportToPdfFile(jasperPrint, desFilePath);
 
        } catch (Exception e) {
            log.error("读取报表异常", e);
            System.out.println("读取报表异常"+ e.toString());
        } finally {
 
        }
    }
 
    public static byte[] exportToPdfBytes(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {
        try {
            ClassPathResource resource = new ClassPathResource(jasperPath);
            //response.setContentType(getContentType(ReportType.PDF));
            InputStream jasperStream = resource.getInputStream();
            JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
 
            byte[] body = JasperExportManager.exportReportToPdf(jasperPrint);
 
            return body;
 
        } catch (Exception e) {
            log.error("读取报表异常", e);
            System.out.println("读取报表异常"+ e.toString());
        } finally {
        }
 
        return null;
    }
 
 
    public static void exportToXml(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {
        OutputStream outputStream = response.getOutputStream();
        try {
            ClassPathResource resource = new ClassPathResource(jasperPath);
            response.setContentType(getContentType(ReportType.XML));
            InputStream jasperStream = resource.getInputStream();
            JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
            JasperExportManager.exportReportToXmlStream(jasperPrint, outputStream);
        } catch (Exception e) {
            log.error("读取报表异常", e);
            outputStream.write("读取报表异常".getBytes());
        } finally {
            outputStream.flush();
            outputStream.close();
        }
    }
 
    public static void exportToHtml(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {

        response.setHeader("Content-type", "text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType(getContentType(ReportType.HTML));
        PrintWriter out = response.getWriter();
        HtmlExporter exporter = new HtmlExporter();
        try {
            ClassPathResource resource = new ClassPathResource(jasperPath);
            InputStream jasperStream = resource.getInputStream();
            JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            SimpleHtmlReportConfiguration configuration = new SimpleHtmlReportConfiguration();
            exporter.setConfiguration(configuration);
            HtmlExporterOutput outPut = new SimpleHtmlExporterOutput(out);
            exporter.setExporterOutput(outPut);
            exporter.exportReport();
        } catch (Exception e) {
            log.error("读取报表异常", e);
            out.write("读取报表异常");
        } finally {
            out.flush();
            out.close();
        }
    }


    public static void exportToImage(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {
        //生成图片报表
        OutputStream outputStream = response.getOutputStream();
        response.setContentType(getContentType(ReportType.IMAGE));
 
        ClassPathResource resource = new ClassPathResource(jasperPath);
        InputStream jasperStream = resource.getInputStream();
        JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
        JRGraphics2DExporter exporter = new JRGraphics2DExporter();//创建graphics输出器
        //创建一个影像对象
        BufferedImage bufferedImage = new BufferedImage(jasperPrint.getPageWidth() * 4, jasperPrint.getPageHeight() * 4, BufferedImage.TYPE_INT_RGB);
        //取graphics
        Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
        //设置相应参数信息
        exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, g);
        exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, Float.valueOf(4));
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.exportReport();
        g.dispose();//释放资源信息
        //这里的bufferedImage就是最终的影像图像信息,可以通过这个对象导入到cm中了.
        //ImageIO.write(bufferedImage, "JPEG", new File("d:/aa.jpg"));
        ImageIO.write(bufferedImage,"JPEG", outputStream);
    }
 
 
 
    public static void exportToImageFile(String jasperPath, Map parameters, List<?> list,String desFilePath) throws Exception {
        //生成图片报表
        byte[] data = null;
        ClassPathResource resource = new ClassPathResource(jasperPath);
        InputStream jasperStream = resource.getInputStream();
        JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
        JRGraphics2DExporter exporter = new JRGraphics2DExporter();//创建graphics输出器
        //创建一个影像对象
        BufferedImage bufferedImage = new BufferedImage(jasperPrint.getPageWidth() * 4, jasperPrint.getPageHeight() * 4, BufferedImage.TYPE_INT_RGB);
        //取graphics
        Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
        //设置相应参数信息
        exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, g);
        exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, Float.valueOf(4));
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.exportReport();
        g.dispose();//释放资源信息
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //这里的bufferedImage就是最终的影像图像信息,可以通过这个对象导入到cm中了.
        System.out.println("写入文件路径:"+desFilePath);
        ImageIO.write(bufferedImage, "JPEG", new File(desFilePath));
//        ImageIO.write(bufferedImage,"JPEG", baos);
//        baos.flush();
//        data = baos.toByteArray();
//        baos.close();
//        return data;
    }
 
 
    public enum ReportType {
        HTML,
        PDF,
        XLS,
        XLSX,
        XML,
        RTF,
        CSV,
        DOC,
        IMAGE
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值