当前位置: 首页 > 工具软件 > Jasper > 使用案例 >

JasperUtil

郗浩
2023-12-01
package com.ynashk.shg.util;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.*;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

public class JasperUtil {
    private final static String jplocal = "/jasper/";


    public static void exportPdf(
            String reportName, Map<String, Object> parameters,
            HttpServletResponse response, Connection connection) throws SQLException, JRException, IOException, ParseException {




        //获取文件流
        ClassPathResource resource = new ClassPathResource( jplocal+reportName + ".jasper");
        InputStream jasperStream = resource.getInputStream();


        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,connection);

        JRPdfExporter exporter = new JRPdfExporter ();
        //设置输入项
        ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
        exporter.setExporterInput(exporterInput);

        //设置输出项
        OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(response.getOutputStream());
        exporter.setExporterOutput(exporterOutput);

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(reportName + ".pdf","utf8"));
        exporter.exportReport();
        connection.close();
    }

    public static void exportExcel(
            String reportName, String fileName, Map<String, Object> parameters,
            HttpServletResponse response, Connection connection) throws SQLException, JRException, IOException, ParseException {
        parameters = parameters == null ? new HashMap<>() : parameters;



        //获取文件流
        ClassPathResource resource = new ClassPathResource( jplocal+reportName + ".jasper");
        InputStream jasperStream = resource.getInputStream();


        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,connection);
        //获取文件流
        //File exe_rpt = new File("C:\\jasper\\"+reportName+".jasper");

        JRXlsxExporter exporter = new JRXlsxExporter();

        //设置输入项
        ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
        exporter.setExporterInput(exporterInput);

        //设置输出项
        OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(response.getOutputStream());
        exporter.setExporterOutput(exporterOutput);

        String newFileName =  fileName+".xlsx";
        //设置响应方式
        newFileName= URLEncoder.encode(newFileName,"UTF-8");
        newFileName=newFileName.replaceAll("\\+","%20");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename*=UTF-8''" + newFileName);

        exporter.exportReport();
        connection.close();

    }

    public static void exportExcel2(
            String reportName, String fileName, Map<String, Object> parameters,
            HttpServletResponse response, Connection connection) throws SQLException, JRException, IOException, ParseException {
        parameters = parameters == null ? new HashMap<>() : parameters;



        //获取文件流

        File exe_rpt = new File("f:\\jasper\\"+reportName+".jasper");

        JasperPrint jasperPrint = JasperFillManager.fillReport(exe_rpt.getPath(), parameters,connection);
        //获取文件流
        //File exe_rpt = new File("C:\\jasper\\"+reportName+".jasper");

        JRXlsxExporter exporter = new JRXlsxExporter();

        //设置输入项
        ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
        exporter.setExporterInput(exporterInput);

        //设置输出项
        OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(response.getOutputStream());
        exporter.setExporterOutput(exporterOutput);

        String newFileName =  fileName+".xlsx";
        //设置响应方式
        newFileName= URLEncoder.encode(newFileName,"UTF-8");
        newFileName=newFileName.replaceAll("\\+","%20");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename*=UTF-8''" + newFileName);

        exporter.exportReport();
        connection.close();

    }

    public static void exportWord(
            String reportName, Map<String, Object> parameters,
            HttpServletResponse response, Connection connection) throws SQLException, JRException, IOException {
        parameters = parameters == null ? new HashMap<>() : parameters;
        //获取文件流
        ClassPathResource resource = new ClassPathResource( jplocal+reportName + ".jasper");
        InputStream jasperStream = resource.getInputStream();
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);

        JRDocxExporter exporter = new JRDocxExporter ();

        //设置输入项
        ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
        exporter.setExporterInput(exporterInput);

        //设置输出项
        OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(response.getOutputStream());
        exporter.setExporterOutput(exporterOutput);

        response.setContentType("application/msword");
        response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode(reportName + ".doc","utf8"));

        exporter.exportReport();
        connection.close();

    }

    public static void exportHtml(
            String reportName, Map<String, Object> parameters,
            HttpServletResponse response, Connection connection) throws SQLException, JRException, IOException {
        InputStream jasperStream = null;
        try{
           parameters = parameters == null ? new HashMap<>() : parameters;
           //获取文件流
           ClassPathResource resource = new ClassPathResource(jplocal+ reportName + ".jasper");
            jasperStream = resource.getInputStream();
           JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
           JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
           //设置输入项
           HtmlExporter exporter = new HtmlExporter();

           SimpleHtmlExporterConfiguration config=new SimpleHtmlExporterConfiguration();
           String header= "<html>\n"+
                   "<head>\n"+
                   "  <title></title>\n"+
                   "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n"+
                   "  <link rel=\"stylesheet\" type=\"text/css\" href=\"css/jasper.css\" />\n"+
                   "  <style type=\"text/css\">\n"+
                   "    a {text-decoration: none}\n"+
                   "  </style>\n"+
                   "</head>\n";

           config.setHtmlHeader(header);
           exporter.setConfiguration(config);

           ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
           exporter.setExporterInput(exporterInput);

           //设置输出项
           SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(response.getOutputStream());
           exporter.setExporterOutput(exporterOutput);

           response.setContentType("text/html");
           response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode(reportName + ".html","utf8"));

           exporter.exportReport();

       }catch (Exception e){
           e.printStackTrace();
       }finally {
            jasperStream.close();
            connection.close();
       }

    }

    public static String exportExcelToFile(
            String reportName, String excelName, Map<String, Object> parameters,
            HttpServletResponse response, Connection connection) throws SQLException, JRException, IOException, ParseException {
        parameters = parameters == null ? new HashMap<>() : parameters;
        //FileOutputStream outputStream=null;
        ByteArrayOutputStream outPut=new ByteArrayOutputStream();
        try{
            String Path = Constants.getApplicationValue("applicationContext","save.Report.path");
            File file = new File(Path);
            if(!file.exists()){
                file.mkdirs();
            }

            //获取文件流
            ClassPathResource resource = new ClassPathResource( jplocal+reportName + ".jasper");
            InputStream jasperStream = resource.getInputStream();
            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
            // File exe_rpt = new File("C:\\jasper\\"+reportName+".jasper");
            File excelFile = new File(Path,  excelName+ ".xlsx");//要导出的Excel文件

            for (int i = 2; excelFile.exists() && i < Integer.MAX_VALUE; i++){
                excelFile = new File(Path,excelName + "-"+i+".xlsx");
            }
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,connection);



            //导出Excel
            JRXlsxExporter exporter = new JRXlsxExporter();

            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(excelFile));


            exporter.exportReport();
            return excelFile.getName();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
             outPut.close();
            connection.close();
        }
        return null;

    }

    public static String exportExcelToFile2(
            String reportName, String excelName, Map<String, Object> parameters,
            HttpServletResponse response, Connection connection) throws SQLException, JRException, IOException, ParseException {
        parameters = parameters == null ? new HashMap<>() : parameters;
        FileOutputStream outputStream=null;
        ByteArrayOutputStream outPut=new ByteArrayOutputStream();
        try{
            String Path = Constants.getApplicationValue("applicationContext","save.Report.path");
            File file = new File(Path);
            if(!file.exists()){
                file.mkdirs();
            }

            //获取文件流
            /*ClassPathResource resource = new ClassPathResource( jplocal+reportName + ".jasper");
            InputStream jasperStream = resource.getInputStream();
            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);*/
            File exe_rpt = new File("f:\\jasper\\"+reportName+".jasper");
            File excelFile = new File(Path,  excelName+ ".xlsx");//要导出的Excel文件

            for (int i = 2; excelFile.exists() && i < Integer.MAX_VALUE; i++){
                excelFile = new File(Path,excelName + "-"+i+".xlsx");
            }
            JasperPrint jasperPrint = JasperFillManager.fillReport(exe_rpt.getPath(), parameters,connection);



            //导出Excel
            JRXlsxExporter exporter = new JRXlsxExporter();

            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(excelFile));


            exporter.exportReport();
            return excelFile.getName();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            outPut.close();
            connection.close();
        }
        return null;

    }
}

 类似资料:

相关阅读

相关文章

相关问答