Signed-off-by: chy <chy@163.com>
This commit is contained in:
105
lidee-common/lidee-boot-export/pom.xml
Normal file
105
lidee-common/lidee-boot-export/pom.xml
Normal file
@@ -0,0 +1,105 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>top.lidee.taie</groupId>
|
||||
<artifactId>lidee-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<groupId>top.lidee.taie</groupId>
|
||||
<artifactId>lidee-boot-export</artifactId>
|
||||
|
||||
<name>lidee-boot-export</name>
|
||||
<description>lidee-boot-export</description>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>top.lidee.taie</groupId>
|
||||
<artifactId>lidee-boot-taie</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>3.2.1</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
<version>3.9.9</version>
|
||||
</dependency>
|
||||
<!--jasperreports-->
|
||||
<dependency>
|
||||
<groupId>net.sf.jasperreports</groupId>
|
||||
<artifactId>jasperreports</artifactId>
|
||||
<version>6.19.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.lowagie</groupId>
|
||||
<artifactId>itext</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itextpdf</artifactId>
|
||||
<version>5.5.13.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itext-asian</artifactId>
|
||||
<version>5.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.zxing</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.zxing</groupId>
|
||||
<artifactId>javase</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,80 @@
|
||||
package top.lidee.taie.export.config;
|
||||
|
||||
|
||||
import net.sf.jasperreports.engine.JasperCompileManager;
|
||||
import net.sf.jasperreports.engine.JasperReport;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ExportTemplateContext {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ExportTemplateContext.class);
|
||||
|
||||
private static Map<String, JasperReport> jasperReportCache = new HashMap<String, JasperReport>();
|
||||
|
||||
private static ExportTemplateContext instance;
|
||||
|
||||
private ExportTemplateContext() {
|
||||
}
|
||||
|
||||
public static ExportTemplateContext getInstance() {
|
||||
if (instance == null || jasperReportCache == null || jasperReportCache.isEmpty()) {
|
||||
synchronized (ExportTemplateContext.class) {
|
||||
if (instance == null) {
|
||||
instance = new ExportTemplateContext();
|
||||
logger.info("instance为空,重新初始化。");
|
||||
}
|
||||
if (jasperReportCache == null || jasperReportCache.isEmpty()) {
|
||||
logger.info("jasperReportCache为空,从文件中编译模板。");
|
||||
}
|
||||
instance.init();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
synchronized (jasperReportCache) {
|
||||
try {
|
||||
jasperReportCache.clear();
|
||||
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
Resource[] resources = resolver.getResources("classpath*:jaspers/*.jrxml");
|
||||
for (int i = resources.length - 1; i >= 0; i--) {
|
||||
Resource resource = resources[i];
|
||||
String jrxmlFileName = resource.getFilename();
|
||||
logger.info("模板文件:" + jrxmlFileName);
|
||||
String reportId = jrxmlFileName.substring(0, jrxmlFileName.indexOf("."));
|
||||
InputStream jrxmlTempate = resource.getInputStream();
|
||||
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlTempate);
|
||||
jasperReportCache.put(reportId, jasperReport);
|
||||
logger.info("编译模板成功" + reportId);
|
||||
|
||||
}
|
||||
logger.info("所有模板已加载完毕,共" + jasperReportCache.size() + "个");
|
||||
} catch (Exception e) {
|
||||
logger.error("编译模板失败!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JasperReport getJasperTemplate(String reportId) {
|
||||
if (jasperReportCache == null || jasperReportCache.isEmpty()) {
|
||||
logger.info("模板缓存为空,重新加载");
|
||||
getInstance();
|
||||
} else {
|
||||
logger.debug("模板缓存正常,从模板缓存中加载以提高速度");
|
||||
}
|
||||
return jasperReportCache.get(reportId);
|
||||
}
|
||||
|
||||
public Map<String, JasperReport> getJasperReportCache() {
|
||||
return jasperReportCache;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package top.lidee.taie.export.config;
|
||||
|
||||
import top.lidee.taie.constant.LideeConstant;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 导出中心属性
|
||||
* @author pyn
|
||||
* @since 2021-01-27
|
||||
*/
|
||||
@ConfigurationProperties(prefix = LideeConstant.COMPONENT_PREFIX + LideeExportProperties.COMPONENT_NAME)
|
||||
public class LideeExportProperties {
|
||||
|
||||
/**
|
||||
* 组件名称
|
||||
*/
|
||||
public final static String COMPONENT_NAME = "export";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package top.lidee.taie.export.constant;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ExpConstant {
|
||||
public static final String LIST_EXP = "${";
|
||||
public static final String GROUP_RIGHT = "groupRight(";
|
||||
public static final String GROUP = "group(";
|
||||
public static final String SUM = "=SUM(";
|
||||
public static final String[] FUNCTION = new String[]{"=SUM(", "=AVERAGE(", "=MAX(", "=MIN(", "=IF(", "=AND(", "=OR(", "=CONCAT("};
|
||||
public static final String[] FREEMARKER_FUNCTION = new String[]{"max(", "min(", "average(", "sum("};
|
||||
public static final String DYNAMIC = "dynamic(";
|
||||
public static final String MAX = "max(";
|
||||
public static final String EACH_TITLE = "eachTitle(";
|
||||
|
||||
public static List<Integer> getExpFunction(String e) {
|
||||
List<Integer> counts = new ArrayList<>();
|
||||
for (int i = 0; i < FUNCTION.length; i++) {
|
||||
if(e.contains(FUNCTION[i])){
|
||||
counts.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
return counts;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getExpFunction("da").toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package top.lidee.taie.export.enums;
|
||||
public enum DeleteFlagEnum {
|
||||
DELETED(1,"已删除"),
|
||||
UNDELETED(0,"未删除"),
|
||||
;
|
||||
|
||||
private int codeValue;
|
||||
private String codeDesc;
|
||||
|
||||
private DeleteFlagEnum(int codeValue, String codeDesc) {
|
||||
this.codeValue = codeValue;
|
||||
this.codeDesc = codeDesc;
|
||||
}
|
||||
|
||||
public int getCodeValue(){ return this.codeValue;}
|
||||
|
||||
public String getCodeDesc(){ return this.codeDesc;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package top.lidee.taie.export.enums;
|
||||
public enum EnableFlagEnum {
|
||||
DISABLE(0,"已禁用"),
|
||||
ENABLE(1,"已启用"),
|
||||
;
|
||||
|
||||
private int codeValue;
|
||||
private String codeDesc;
|
||||
|
||||
private EnableFlagEnum(int codeValue, String codeDesc) {
|
||||
this.codeValue = codeValue;
|
||||
this.codeDesc = codeDesc;
|
||||
}
|
||||
|
||||
public int getCodeValue(){ return this.codeValue;}
|
||||
|
||||
public String getCodeDesc(){ return this.codeDesc;}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package top.lidee.taie.export.enums;
|
||||
|
||||
public enum ExportTypeEnum {
|
||||
/**easy_excel*/
|
||||
SIMPLE_EXCEL("excel", "easy_excel"),
|
||||
/**pasper_excel*/
|
||||
JASPER_TEMPLATE_EXCEL("jasper_template_excel", "jasper_template_excel"),
|
||||
/**jasper_pdf*/
|
||||
JASPER_TEMPLATE_PDF("jasper_template_pdf", "jasper_template_pdf"),
|
||||
|
||||
/**lidee_excel
|
||||
* update by Devli,该类型已经搬到ld-report,暂时未开发好
|
||||
LIDEE_TEMPLATE_EXCEL("lidee_template_excel", "lidee_template_excel"),
|
||||
lidee_pdf
|
||||
LIDEE_TEMPLATE_PDF("lidee_template_pdf", "lidee_template_pdf"),
|
||||
*/
|
||||
;
|
||||
|
||||
private String codeValue;
|
||||
private String codeDesc;
|
||||
|
||||
private ExportTypeEnum(String codeValue, String codeDesc) {
|
||||
this.codeValue = codeValue;
|
||||
this.codeDesc = codeDesc;
|
||||
}
|
||||
|
||||
public String getCodeValue() {
|
||||
return this.codeValue;
|
||||
}
|
||||
|
||||
public String getCodeDesc() {
|
||||
return this.codeDesc;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package top.lidee.taie.export.enums;
|
||||
public enum FileStatusEnum {
|
||||
CREATING("creating","生成中"),
|
||||
SUCCESS("success","生成成功"),
|
||||
FAILED("failed","生成失败"),
|
||||
;
|
||||
|
||||
private String codeValue;
|
||||
private String codeDesc;
|
||||
|
||||
private FileStatusEnum(String codeValue, String codeDesc) {
|
||||
this.codeValue = codeValue;
|
||||
this.codeDesc = codeDesc;
|
||||
}
|
||||
|
||||
public String getCodeValue(){ return this.codeValue;}
|
||||
|
||||
public String getCodeDesc(){ return this.codeDesc;}
|
||||
|
||||
//根据codeValue获取枚举
|
||||
public static FileStatusEnum parseFromCodeValue(String codeValue){
|
||||
for (FileStatusEnum e : FileStatusEnum.values()){
|
||||
if(e.codeValue.equals(codeValue)){ return e;}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//根据codeValue获取描述
|
||||
public static String getCodeDescByCodeBalue(String codeValue){
|
||||
FileStatusEnum enumItem = parseFromCodeValue(codeValue);
|
||||
return enumItem == null ? "" : enumItem.getCodeDesc();
|
||||
}
|
||||
|
||||
//验证codeValue是否有效
|
||||
public static boolean validateCodeValue(String codeValue){ return parseFromCodeValue(codeValue)!=null;}
|
||||
|
||||
//列出所有值字符串
|
||||
public static String getString(){
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (FileStatusEnum e : FileStatusEnum.values()){
|
||||
buffer.append(e.codeValue).append("--").append(e.getCodeDesc()).append(", ");
|
||||
}
|
||||
buffer.deleteCharAt(buffer.lastIndexOf(","));
|
||||
return buffer.toString().trim();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package top.lidee.taie.export.event;
|
||||
|
||||
import top.lidee.taie.export.vo.ExportOperation;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* 功能描述:
|
||||
* 导出信息-事件发布
|
||||
* @Author: peiyanni
|
||||
* @Date: 2021/1/27 17:26
|
||||
*/
|
||||
public class LideeExportApplicationEvent extends ApplicationEvent {
|
||||
|
||||
private ExportOperation exportOperation;
|
||||
|
||||
public LideeExportApplicationEvent(Object source, ExportOperation exportOperation) {
|
||||
super(source);
|
||||
this.exportOperation=exportOperation;
|
||||
}
|
||||
|
||||
public ExportOperation getExportOperation() {
|
||||
return exportOperation;
|
||||
}
|
||||
|
||||
public void setExportOperation(ExportOperation exportOperation) {
|
||||
this.exportOperation = exportOperation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package top.lidee.taie.export.utils;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import top.lidee.taie.export.vo.ExportOperation;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 功能描述:
|
||||
* EasyExcel 相关操作工具类
|
||||
*
|
||||
* @Author: peiyanni
|
||||
* @Date: 2021/1/28 10:56
|
||||
*/
|
||||
public class EasyExcelUtil {
|
||||
|
||||
/**
|
||||
* 简单模式下
|
||||
* 根据filePath导出文件
|
||||
*
|
||||
* @param exportOperation
|
||||
*/
|
||||
public static void excelExportByFilePath(ExportOperation exportOperation, Class clazz) {
|
||||
File file = new File(exportOperation.getFilePath());
|
||||
EasyExcel.write(file, clazz).sheet(exportOperation.getFileTitle()).doWrite(exportOperation.getList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单模式下导出文件流格式
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static byte[] excelExportByte(ExportOperation exportOperation, Class clazz) throws Exception {
|
||||
ByteArrayOutputStream outputStream = null;
|
||||
try {
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
EasyExcel.write(outputStream, clazz).sheet(exportOperation.getFileTitle()).doWrite(exportOperation.getList());
|
||||
byte[] resultByte = outputStream.toByteArray();
|
||||
return resultByte;
|
||||
} catch (Exception e) {
|
||||
|
||||
} finally {
|
||||
if (null != outputStream) {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package top.lidee.taie.export.utils;
|
||||
|
||||
import top.lidee.taie.export.enums.ExportTypeEnum;
|
||||
import top.lidee.taie.export.enums.FileStatusEnum;
|
||||
import top.lidee.taie.export.event.LideeExportApplicationEvent;
|
||||
import top.lidee.taie.export.vo.ExportOperation;
|
||||
import top.lidee.taie.utils.ApplicationContextUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 导出工具类
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
public class ExportUtil {
|
||||
private static Logger logger = LoggerFactory.getLogger(ExportUtil.class);
|
||||
|
||||
private static ExportUtil exportUtil=null;
|
||||
|
||||
private ExportUtil(){
|
||||
}
|
||||
|
||||
public static synchronized ExportUtil getInstance(){
|
||||
if(null==exportUtil){
|
||||
synchronized (ExportUtil.class) {
|
||||
if (null == exportUtil) {
|
||||
exportUtil= new ExportUtil();
|
||||
}
|
||||
}
|
||||
}
|
||||
return exportUtil;
|
||||
}
|
||||
|
||||
public String getFileIdUUID() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* jasper方式 导出格式为文件
|
||||
*
|
||||
* @param exportOperation
|
||||
*/
|
||||
public void exportByFilePathJasper(ExportOperation exportOperation) {
|
||||
exportByFilePath(exportOperation, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* jasper方式 导出数据流
|
||||
*
|
||||
* @param exportOperation
|
||||
* @return
|
||||
*/
|
||||
public byte[] exportByteInfoJasper(ExportOperation exportOperation) {
|
||||
return exportByteInfo(exportOperation, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* easyExcel方式 导出格式为文件
|
||||
*
|
||||
* @param exportOperation 参数
|
||||
* @param clazz excel中list中的类型
|
||||
*/
|
||||
public void exportByFilePathSimple(ExportOperation exportOperation, Class clazz) {
|
||||
exportByFilePath(exportOperation, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* easyExcel方式 导出格式为文件流
|
||||
*
|
||||
* @param exportOperation 参数
|
||||
* @param clazz excel中list中的类型
|
||||
* @return
|
||||
*/
|
||||
public byte[] exportByteInfoSimple(ExportOperation exportOperation, Class clazz) {
|
||||
return exportByteInfo(exportOperation, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* easyExcel方式 导出格式为文件
|
||||
*
|
||||
* @param exportOperation 参数
|
||||
* @param clazz excel中list中的类型
|
||||
*/
|
||||
private void exportByFilePath(ExportOperation exportOperation, Class clazz) {
|
||||
String fileId = getFileIdUUID();
|
||||
String dictPath = exportOperation.getFilePath();
|
||||
exportOperation.setFileId(fileId);
|
||||
exportOperation.setFileCreateTime(LocalDateTime.now());
|
||||
exportOperation.setFileStatus(FileStatusEnum.CREATING.getCodeValue());
|
||||
logger.info("------>正在导出文件<-----:fileId:{}", exportOperation.getFileId());
|
||||
try {
|
||||
if (exportOperation.getExportType().equals(ExportTypeEnum.SIMPLE_EXCEL.getCodeValue())) {
|
||||
//easyexcel
|
||||
exportOperation.setFilePath(dictPath + fileId + ".xlsx");
|
||||
EasyExcelUtil.excelExportByFilePath(exportOperation, clazz);
|
||||
} else if (exportOperation.getExportType().equals(ExportTypeEnum.JASPER_TEMPLATE_EXCEL.getCodeValue())) {
|
||||
//jasper excel
|
||||
exportOperation.setFilePath(dictPath + fileId + ".xlsx");
|
||||
JasperUtil.jasperExcelByFilePath(exportOperation);
|
||||
} else if (exportOperation.getExportType().equals(ExportTypeEnum.JASPER_TEMPLATE_PDF.getCodeValue())) {
|
||||
//jasper pdf
|
||||
exportOperation.setFilePath(dictPath + fileId + ".pdf");
|
||||
JasperUtil.jasperPDFByFilePath(exportOperation);
|
||||
}
|
||||
|
||||
exportOperation.setFileStatus(FileStatusEnum.SUCCESS.getCodeValue());
|
||||
exportOperation.setFileFinishTime(LocalDateTime.now());
|
||||
logger.info("------>导出文件成功<-----:路径:{}", exportOperation.getFilePath());
|
||||
} catch (Exception e) {
|
||||
logger.error("----->导出文件失败<-----", e);
|
||||
exportOperation.setRemark(e.getMessage());
|
||||
exportOperation.setFileStatus(FileStatusEnum.FAILED.getCodeValue());
|
||||
exportOperation.setFileFinishTime(LocalDateTime.now());
|
||||
} finally {
|
||||
logger.info("------>导出文件发布事件<-----");
|
||||
exportOperation.setList(null);
|
||||
ApplicationContextUtils.publishEvent(new LideeExportApplicationEvent(ExportUtil.class, exportOperation));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* easyExcel方式 导出输出流
|
||||
*
|
||||
* @param exportOperation 参数
|
||||
* @param clazz excel中list中的类型
|
||||
* @return
|
||||
*/
|
||||
private byte[] exportByteInfo(ExportOperation exportOperation, Class clazz) {
|
||||
String fileId = getFileIdUUID();
|
||||
exportOperation.setFileId(fileId);
|
||||
exportOperation.setFileCreateTime(LocalDateTime.now());
|
||||
exportOperation.setFileStatus(FileStatusEnum.CREATING.getCodeValue());
|
||||
logger.info("------>正在导出byte<-----:fileId:{}", exportOperation.getFileId());
|
||||
byte[] resultByte = null;
|
||||
try {
|
||||
if (exportOperation.getExportType().equals(ExportTypeEnum.SIMPLE_EXCEL.getCodeValue())) {
|
||||
//easyexcel
|
||||
resultByte = EasyExcelUtil.excelExportByte(exportOperation, clazz);
|
||||
} else if (exportOperation.getExportType().equals(ExportTypeEnum.JASPER_TEMPLATE_EXCEL.getCodeValue())) {
|
||||
//jasper excel
|
||||
resultByte = JasperUtil.jasperExcelOutByte(exportOperation);
|
||||
} else if (exportOperation.getExportType().equals(ExportTypeEnum.JASPER_TEMPLATE_PDF.getCodeValue())) {
|
||||
//jasper pdf
|
||||
resultByte = JasperUtil.jasperPDFOutByte(exportOperation);
|
||||
}
|
||||
|
||||
exportOperation.setFileStatus(FileStatusEnum.SUCCESS.getCodeValue());
|
||||
exportOperation.setFileFinishTime(LocalDateTime.now());
|
||||
logger.info("------>导出byte成功<-----");
|
||||
} catch (Exception e) {
|
||||
logger.error("----->导出byte失败<-----", e);
|
||||
exportOperation.setRemark(e.getMessage());
|
||||
exportOperation.setFileStatus(FileStatusEnum.FAILED.getCodeValue());
|
||||
exportOperation.setFileFinishTime(LocalDateTime.now());
|
||||
}
|
||||
return resultByte;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package top.lidee.taie.export.utils;
|
||||
|
||||
import top.lidee.taie.export.config.ExportTemplateContext;
|
||||
import top.lidee.taie.export.vo.ExportOperation;
|
||||
import net.sf.jasperreports.engine.*;
|
||||
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
|
||||
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
|
||||
import net.sf.jasperreports.export.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
* Created by raodeming on 2020/11/26.
|
||||
*/
|
||||
public class JasperUtil {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(JasperUtil.class);
|
||||
|
||||
public static JasperPrint jasper(ExportOperation exportOperation) throws Exception {
|
||||
String reportId = exportOperation.getReportTemplateId();
|
||||
//获取报表模板
|
||||
logger.info("获取报表模板reportId=" + reportId);
|
||||
JasperReport jasperReport = ExportTemplateContext.getInstance().getJasperTemplate(reportId);
|
||||
if (jasperReport == null) {
|
||||
throw new RuntimeException("报表编号[" + reportId + "]对应的模板不存在");
|
||||
}
|
||||
JRDataSource dataSource = new JRBeanCollectionDataSource(exportOperation.getList(), false);
|
||||
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, exportOperation.getParameters(), dataSource);
|
||||
//纸张大小设置
|
||||
if (exportOperation.getPageWidth() > 0 && exportOperation.getPageHeight() > 0) {
|
||||
jasperPrint.setPageWidth(exportOperation.getPageWidth());
|
||||
jasperPrint.setPageHeight(exportOperation.getPageHeight());
|
||||
}
|
||||
if (exportOperation.getTopMargin() > 0) {
|
||||
jasperPrint.setTopMargin(exportOperation.getTopMargin());
|
||||
}
|
||||
if (exportOperation.getLeftMargin() > 0) {
|
||||
jasperPrint.setLeftMargin(exportOperation.getLeftMargin());
|
||||
}
|
||||
if (exportOperation.getBottomMargin() > 0) {
|
||||
jasperPrint.setBottomMargin(exportOperation.getBottomMargin());
|
||||
}
|
||||
if (exportOperation.getRightMargin() > 0) {
|
||||
jasperPrint.setRightMargin(exportOperation.getRightMargin());
|
||||
}
|
||||
return jasperPrint;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板模式,导出pdf文件
|
||||
* @param exportOperation
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void jasperPDFByFilePath(ExportOperation exportOperation) throws Exception {
|
||||
String filePath = exportOperation.getFilePath();
|
||||
//导出pdf
|
||||
JasperExportManager.exportReportToPdfFile(jasper(exportOperation), filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板模式,输出文件流
|
||||
* @param exportOperation
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] jasperPDFOutByte(ExportOperation exportOperation) throws Exception {
|
||||
//导出pdf文件流
|
||||
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
|
||||
JasperExportManager.exportReportToPdfStream(jasper(exportOperation),outputStream);
|
||||
byte[] resultByte=outputStream.toByteArray();
|
||||
outputStream.close();
|
||||
return resultByte;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板模式,导出excel文件
|
||||
* @param exportOperation
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void jasperExcelByFilePath(ExportOperation exportOperation) throws Exception {
|
||||
String filePath = exportOperation.getFilePath();
|
||||
// 创建Jasper Excel导出类
|
||||
JRXlsxExporter exporter = new JRXlsxExporter();
|
||||
// 设置导出配置项
|
||||
SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();
|
||||
conf.setWhitePageBackground(false);
|
||||
conf.setDetectCellType(true);
|
||||
exporter.setConfiguration(conf);
|
||||
// 设置输入项
|
||||
ExporterInput exporterInput = new SimpleExporterInput(jasper(exportOperation));
|
||||
exporter.setExporterInput(exporterInput);
|
||||
// 设置输出项
|
||||
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(filePath);
|
||||
exporter.setExporterOutput(exporterOutput);
|
||||
//导出报表
|
||||
exporter.exportReport();
|
||||
exporterOutput.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板模式,导出excel文件流
|
||||
* @param exportOperation
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] jasperExcelOutByte(ExportOperation exportOperation)throws Exception{
|
||||
// 创建Jasper Excel导出类
|
||||
JRXlsxExporter exporter = new JRXlsxExporter();
|
||||
// 设置导出配置项
|
||||
SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();
|
||||
conf.setWhitePageBackground(false);
|
||||
conf.setDetectCellType(true);
|
||||
exporter.setConfiguration(conf);
|
||||
// 设置输入项
|
||||
ExporterInput exporterInput = new SimpleExporterInput(jasper(exportOperation));
|
||||
exporter.setExporterInput(exporterInput);
|
||||
// 设置输出项
|
||||
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
|
||||
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
|
||||
exporter.setExporterOutput(exporterOutput);
|
||||
//导出报表
|
||||
exporter.exportReport();
|
||||
byte[] resultByte=outputStream.toByteArray();
|
||||
outputStream.close();
|
||||
exporterOutput.close();
|
||||
return resultByte;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package top.lidee.taie.export.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 功能描述:
|
||||
*
|
||||
* @Author: peiyanni
|
||||
* @Date: 2021/1/28 12:57
|
||||
*/
|
||||
public class ExportBaseInfo implements Serializable {
|
||||
private static final long serialVersionUID=1L;
|
||||
|
||||
private Long exportId;
|
||||
|
||||
/** 文件在t_file中的id,前端传它来读流接口显示,http://auth/file/download/fileId */
|
||||
private String fileId;
|
||||
|
||||
/** 文件标题,比如:对账单报表6月份报表 */
|
||||
private String fileTitle;
|
||||
|
||||
/** 导出前,查询的数据开始时间 */
|
||||
private LocalDateTime resultStartTime;
|
||||
|
||||
/** 导出前,查询的数据结束时间 */
|
||||
private LocalDateTime resultEndTime;
|
||||
|
||||
/** 导出查询结果,数据总条数 */
|
||||
private Long resultSize;
|
||||
|
||||
/** 文件导出触发时间 */
|
||||
private LocalDateTime fileCreateTime;
|
||||
|
||||
/** 文件生成完成时间 */
|
||||
private LocalDateTime fileFinishTime;
|
||||
|
||||
/** 文件状态,creating生成中,success生成成功,failed生成失败 */
|
||||
private String fileStatus;
|
||||
|
||||
/** 创建者userId */
|
||||
private Long createrUserid;
|
||||
|
||||
/** 创建者loginName */
|
||||
private String createrUsername;
|
||||
|
||||
/** 备注 */
|
||||
private String remark;
|
||||
|
||||
|
||||
public Long getExportId() {
|
||||
return exportId;
|
||||
}
|
||||
|
||||
public void setExportId(Long exportId) {
|
||||
this.exportId = exportId;
|
||||
}
|
||||
|
||||
public String getFileId() {
|
||||
return fileId;
|
||||
}
|
||||
|
||||
public void setFileId(String fileId) {
|
||||
this.fileId = fileId;
|
||||
}
|
||||
|
||||
public String getFileTitle() {
|
||||
return fileTitle;
|
||||
}
|
||||
|
||||
public void setFileTitle(String fileTitle) {
|
||||
this.fileTitle = fileTitle;
|
||||
}
|
||||
|
||||
public LocalDateTime getResultStartTime() {
|
||||
return resultStartTime;
|
||||
}
|
||||
|
||||
public void setResultStartTime(LocalDateTime resultStartTime) {
|
||||
this.resultStartTime = resultStartTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getResultEndTime() {
|
||||
return resultEndTime;
|
||||
}
|
||||
|
||||
public void setResultEndTime(LocalDateTime resultEndTime) {
|
||||
this.resultEndTime = resultEndTime;
|
||||
}
|
||||
|
||||
public Long getResultSize() {
|
||||
return resultSize;
|
||||
}
|
||||
|
||||
public void setResultSize(Long resultSize) {
|
||||
this.resultSize = resultSize;
|
||||
}
|
||||
|
||||
public LocalDateTime getFileCreateTime() {
|
||||
return fileCreateTime;
|
||||
}
|
||||
|
||||
public void setFileCreateTime(LocalDateTime fileCreateTime) {
|
||||
this.fileCreateTime = fileCreateTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getFileFinishTime() {
|
||||
return fileFinishTime;
|
||||
}
|
||||
|
||||
public void setFileFinishTime(LocalDateTime fileFinishTime) {
|
||||
this.fileFinishTime = fileFinishTime;
|
||||
}
|
||||
|
||||
public String getFileStatus() {
|
||||
return fileStatus;
|
||||
}
|
||||
|
||||
public void setFileStatus(String fileStatus) {
|
||||
this.fileStatus = fileStatus;
|
||||
}
|
||||
|
||||
public Long getCreaterUserid() {
|
||||
return createrUserid;
|
||||
}
|
||||
|
||||
public void setCreaterUserid(Long createrUserid) {
|
||||
this.createrUserid = createrUserid;
|
||||
}
|
||||
|
||||
public String getCreaterUsername() {
|
||||
return createrUsername;
|
||||
}
|
||||
|
||||
public void setCreaterUsername(String createrUsername) {
|
||||
this.createrUsername = createrUsername;
|
||||
}
|
||||
|
||||
public String getRemark() {
|
||||
return remark;
|
||||
}
|
||||
|
||||
public void setRemark(String remark) {
|
||||
this.remark = remark;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package top.lidee.taie.export.vo;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 导出中心
|
||||
* </p>
|
||||
* @since 2021-01-27
|
||||
*/
|
||||
|
||||
public class ExportOperation extends ExportBaseInfo {
|
||||
/************************** 以下为Export操作所需参数 ****************************/
|
||||
/** 自定义参数,导出模板渲染时,可以使用 */
|
||||
private Map<String, Object> parameters;
|
||||
/** 导出的list */
|
||||
private List list;
|
||||
/** 导出的JsonStr */
|
||||
private String jsonStr;
|
||||
/** 模板模式时,模板id */
|
||||
private String reportTemplateId;
|
||||
/** 文件格式,完整路径: /app/dist/export/excel/a3e847fhtu2341234asdfasd.xlsx*/
|
||||
private String filePath;
|
||||
/** 导出类型对应ExportTypeEnum枚举*/
|
||||
private String exportType;
|
||||
|
||||
/**纸张大小宽 JasperPrint.PROPERTY_PAGE_WIDTH*/
|
||||
private int pageWidth;
|
||||
/**纸张大小高 JasperPrint.PROPERTY_PAGE_HEIGHT*/
|
||||
private int pageHeight;
|
||||
private Integer topMargin = 0;
|
||||
private Integer leftMargin = 0;
|
||||
private Integer bottomMargin = 0;
|
||||
private Integer rightMargin = 0;
|
||||
|
||||
public int getPageWidth() {
|
||||
return pageWidth;
|
||||
}
|
||||
|
||||
public void setPageWidth(int pageWidth) {
|
||||
this.pageWidth = pageWidth;
|
||||
}
|
||||
|
||||
public int getPageHeight() {
|
||||
return pageHeight;
|
||||
}
|
||||
|
||||
public void setPageHeight(int pageHeight) {
|
||||
this.pageHeight = pageHeight;
|
||||
}
|
||||
|
||||
public Integer getTopMargin() {
|
||||
return topMargin;
|
||||
}
|
||||
|
||||
public void setTopMargin(Integer topMargin) {
|
||||
this.topMargin = topMargin;
|
||||
}
|
||||
|
||||
public Integer getLeftMargin() {
|
||||
return leftMargin;
|
||||
}
|
||||
|
||||
public void setLeftMargin(Integer leftMargin) {
|
||||
this.leftMargin = leftMargin;
|
||||
}
|
||||
|
||||
public Integer getBottomMargin() {
|
||||
return bottomMargin;
|
||||
}
|
||||
|
||||
public void setBottomMargin(Integer bottomMargin) {
|
||||
this.bottomMargin = bottomMargin;
|
||||
}
|
||||
|
||||
public Integer getRightMargin() {
|
||||
return rightMargin;
|
||||
}
|
||||
|
||||
public void setRightMargin(Integer rightMargin) {
|
||||
this.rightMargin = rightMargin;
|
||||
}
|
||||
|
||||
public Map<String, Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public void setParameters(Map<String, Object> parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public List getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public String getReportTemplateId() {
|
||||
return reportTemplateId;
|
||||
}
|
||||
|
||||
public void setReportTemplateId(String reportTemplateId) {
|
||||
this.reportTemplateId = reportTemplateId;
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public void setFilePath(String filePath) {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
public String getExportType() {
|
||||
return exportType;
|
||||
}
|
||||
|
||||
public void setExportType(String exportType) {
|
||||
this.exportType = exportType;
|
||||
}
|
||||
|
||||
public String getJsonStr() {
|
||||
return jsonStr;
|
||||
}
|
||||
|
||||
public void setJsonStr(String jsonStr) {
|
||||
this.jsonStr = jsonStr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#enabled.annotation.top.lidee.taie.EnabledLideeConfiguration=taop.lidee.taie.export.LideeExportAutoConfiguration
|
||||
Reference in New Issue
Block a user