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));
|
}
|
|
}
|