李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.chinaztt.mes.report.service.impl;
 
import cn.hutool.core.io.resource.ResourceUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.report.entity.Template;
import com.chinaztt.mes.report.mapper.JasperTemplateMapper;
import com.chinaztt.mes.report.service.JasperReportService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.springframework.stereotype.Service;
 
import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.util.Map;
 
/**
 * @Author: zhangxy
 * @Date: 2021-06-03 11:09
 */
@Service
@AllArgsConstructor
public class JasperReportServiceImpl extends ServiceImpl<JasperTemplateMapper, Template> implements JasperReportService {
 
    private static final String JASPER_XML_EXT = ".jrxml";
 
    private DataSource localDataSource;
 
    @SneakyThrows
    @Override
    public byte[] gen(String templatePath, ReportType type, Map<String, Object> params) {
        JasperReport template = getJasperReport(templatePath);
        if (template == null) {
            throw new RuntimeException(templatePath + "模板不存在");
        }
 
        JasperPrint jasperPrint = JasperFillManager.fillReport(template, params, localDataSource.getConnection());
        switch (type) {
            case PDF:
                return JasperExportManager.exportReportToPdf(jasperPrint);
            case XLSX:
                JRXlsxExporter exporter = new JRXlsxExporter();
                exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(stream));
                SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
                configuration.setDetectCellType(true);
                configuration.setCollapseRowSpan(false);
                exporter.setConfiguration(configuration);
                exporter.exportReport();
                return stream.toByteArray();
            default:
                throw new RuntimeException("没有找到对应的导出类型");
 
        }
 
    }
 
 
    private static JasperReport getJasperReport(String templatePath) {
        if (StringUtils.isNotBlank(templatePath)) {
            if (templatePath.endsWith(JASPER_XML_EXT)) {
                return getJasperReportFromXml(templatePath);
            } else {
                return getJasperReportFromJasper(templatePath);
            }
        }
        return null;
    }
 
    /**
     * 获取导出对象,从 xml 中
     *
     * @param templatePath 模板路径(base classpath)
     * @return
     * @throws FileNotFoundException
     * @throws JRException
     */
    @SneakyThrows
    private static JasperReport getJasperReportFromXml(String templatePath) {
        return JasperCompileManager.compileReport(ResourceUtil.getStream(templatePath));
    }
 
 
    /**
     * 获取导出对象,从 jasper 中
     * (jasper 为 jrxml 编译后生成的文件)
     *
     * @param templatePath 模板路径(base classpath)
     * @return
     * @throws FileNotFoundException
     * @throws JRException
     */
    @SneakyThrows
    private static JasperReport getJasperReportFromJasper(String templatePath) {
        return (JasperReport) JRLoader.loadObject(ResourceUtil.getStream(templatePath));
    }
 
}