Excel 导入导出

v1.4.1
Parker 4 years ago
parent 6432e6e31b
commit ed0b6111b2

@ -20,6 +20,13 @@
<version>${version}</version>
</dependency>
<!-- 引入Excel插件 -->
<dependency>
<groupId>org.opsliframework.boot</groupId>
<artifactId>opsli-plugins-excel</artifactId>
<version>${plugins.version}</version>
</dependency>
<!-- ———————————————————— 集成SwaggerApi - 开始 ———————————————————— -->
<!-- Swagger API文档 -->
<dependency>

@ -1,11 +1,16 @@
package org.opsli.api.base.warpper;
import com.baomidou.mybatisplus.annotation.*;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.Version;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.opsli.plugins.excel.annotation.CellStyleFormat;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
@ -29,6 +34,11 @@ import java.util.Date;
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ContentRowHeight(16)
@HeadRowHeight(21)
@HeadFontStyle(fontName = "Arial",color = 9,fontHeightInPoints = 10)
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 23)
@ColumnWidth(22)
public abstract class ApiWrapper implements Serializable {
private static final long serialVersionUID = 1L;
@ -36,30 +46,42 @@ public abstract class ApiWrapper implements Serializable {
/** ID */
@TableId
@ApiModelProperty(value = "ID")
@ExcelProperty(value = "ID", order = 1000)
@CellStyleFormat
private String id;
/** 创建人 */
@ApiModelProperty(value = "创建人")
@ExcelProperty(value = "创建人", order = 1001)
@CellStyleFormat
private String createBy;
/** 创建时间 */
@ApiModelProperty(value = "创建时间")
@ExcelProperty(value = "创建时间", order = 1002)
@CellStyleFormat
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/** 更新人 */
@ApiModelProperty(value = "修改人")
@ExcelProperty(value = "修改人", order = 1003)
@CellStyleFormat
private String updateBy;
/** 更新时间 */
@ApiModelProperty(value = "修改时间")
@ExcelProperty(value = "修改时间", order = 1004)
@CellStyleFormat
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/** 乐观锁 版本 */
@ApiModelProperty(value = "版本")
@ExcelProperty(value = "版本", order = 1005)
@CellStyleFormat
@Version
private Integer version;

@ -5,8 +5,10 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.test.TestModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@ -121,4 +123,26 @@ public interface TestApi {
HttpServletRequest request
);
/**
* Excel
* @return
*/
@GetMapping("/exportExcel")
ResultVo<?> exportExcel(HttpServletRequest request, HttpServletResponse response);
/**
* Excel
* @return
*/
@GetMapping("/exportImport")
ResultVo<?> excelImport(MultipartHttpServletRequest request);
/**
* Excel
* @return
*/
@GetMapping("/exportImport/template")
ResultVo<?> importTemplate(HttpServletResponse response);
}

@ -1,9 +1,13 @@
package org.opsli.api.wrapper.test;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.plugins.excel.annotation.CellStyleFormat;
/**
* @BelongsProject: opsli-boot
@ -17,9 +21,13 @@ import org.opsli.api.base.warpper.ApiWrapper;
public class TestModel extends ApiWrapper {
@ApiModelProperty(value = "名称")
@ExcelProperty(value = "名称", order = 1)
@CellStyleFormat
private String name;
@ApiModelProperty(value = "备注")
@ExcelProperty(value = "备注", order = 2)
@CellStyleFormat
private String remark;
}

@ -47,6 +47,8 @@
<version>${plugins.version}</version>
</dependency>
</dependencies>
</project>

@ -1,9 +1,17 @@
package org.opsli.core.base.concroller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.common.annotation.EnableHotData;
import org.opsli.common.exception.ServiceException;
@ -12,13 +20,21 @@ import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.entity.BaseEntity;
import org.opsli.core.base.service.interfaces.CrudServiceInterface;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.excel.ExcelUtil;
import org.opsli.plugins.excel.exception.ExcelPluginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.List;
/**
* @BelongsProject: opsli-boot
@ -92,6 +108,81 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
return model;
}
/**
* Excel
* @param request
* @return
*/
protected ResultVo<?> excelImport(MultipartHttpServletRequest request){
// 计时器
TimeInterval timer = DateUtil.timer();
Iterator<String> itr = request.getFileNames();
String uploadedFile = itr.next();
List<MultipartFile> files = request.getFiles(uploadedFile);
if (CollectionUtils.isEmpty(files)) {
// 请选择文件
return ResultVo.error(CoreMsg.EXCEL_FILE_NULL.getCode(),
CoreMsg.EXCEL_FILE_NULL.getMessage());
}
try {
List<E> modelList = ExcelUtil.readExcel(files.get(0), modelClazz);
boolean ret = IService.insertBatch(modelList);
if(!ret){
throw new ExcelPluginException(CoreMsg.EXCEL_IMPORT_NO);
}
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_IMPORT_SUCCESS.getMessage(), modelList.size(), timerCount);
// 导入成功
return ResultVo.success(msg);
} catch (ExcelPluginException e) {
// 花费毫秒数
long timerCount = timer.interval();
log.error(e.getMessage(),e);
// 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_IMPORT_ERROR.getMessage(), e.getMessage(), timerCount);
// 导入失败
return ResultVo.error(CoreMsg.EXCEL_IMPORT_ERROR.getCode(), msg);
}
}
/**
*
* @param fileName
* @param response
*/
protected ResultVo<?> importTemplate(String fileName, HttpServletResponse response){
return this.excelExport(fileName+" - 模版 ",null, response);
}
/**
*
* @param fileName
* @param queryWrapper
* @param response
*/
protected ResultVo<?> excelExport(String fileName, QueryWrapper<T> queryWrapper, HttpServletResponse response){
try {
List<E> modelList = Lists.newArrayList();
if(queryWrapper != null){
List<T> entityList = IService.findList(queryWrapper);
// 转化类型
modelList = WrapperUtil.transformInstance(entityList, modelClazz);
}
// 导出Excel
ExcelUtil.writeExcel(response, modelList ,fileName,"sheet", modelClazz ,ExcelTypeEnum.XLSX);
// 导出成功
return ResultVo.success(CoreMsg.EXCEL_EXPORT_SUCCESS.getMessage());
} catch (ExcelPluginException e) {
log.error(e.getMessage(),e);
// 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_EXPORT_ERROR.getMessage(), e.getMessage());
// 导出失败
return ResultVo.error(CoreMsg.EXCEL_EXPORT_ERROR.getCode(),msg);
}
}
// =================================================
@PostConstruct

@ -87,6 +87,13 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
return null;
}
@Override
public boolean insertBatch(List<E> models) {
if(models == null || models.size() == 0) return false;
List<T> entitys = transformMs2Ts(models);
return super.saveBatch(entitys);
}
@Override
public E update(E model) {
if(model == null) return null;

@ -54,6 +54,20 @@ public interface CrudServiceInterface<E,T> extends BaseServiceInterface<T> {
*/
E insert(E model);
/**
* ( )
*
*
*
*
*
*
* @param models model
* @return E
*/
boolean insertBatch(List<E> models);
/**
*
*

@ -23,9 +23,14 @@ public enum CoreMsg implements BaseMsg {
REDIS_EXCEPTION_PUSH_SUB(10200,"Redis 订阅通道失败!"),
/**
* MySQL
* Excel
*/
EXCEL_EXPORT_SUCCESS(10300,"Excel 导出成功!"),
EXCEL_EXPORT_ERROR(10301,"Excel 导出失败!{}"),
EXCEL_IMPORT_SUCCESS(10302,"EXCEL 导入成功!数据行数:{} 耗时(秒){}"),
EXCEL_IMPORT_ERROR(10303,"Excel导入失败{} 耗时(秒){}"),
EXCEL_IMPORT_NO(10304,""),
EXCEL_FILE_NULL(10305,"请选择文件")
;

@ -64,6 +64,17 @@ public class PageQueryBuilder<E extends ApiWrapper,T extends BaseEntity>{
this.entityClazz = entityClazz;
}
/**
*
* @param entityClazz Entity clazz
* @param parameterMap request
*/
public PageQueryBuilder(Class<T> entityClazz, Map<String, String[]> parameterMap){
this.parameterMap = parameterMap;
this.entityClazz = entityClazz;
}
/**
* builderPage
* @return
@ -75,6 +86,14 @@ public class PageQueryBuilder<E extends ApiWrapper,T extends BaseEntity>{
return page;
}
/**
*
* @return
*/
public QueryWrapper<T> builderQueryWrapper(){
return this.createQueryWrapper();
}
/**
*
* @return

@ -0,0 +1,66 @@
package org.opsli.modulars.test.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import lombok.Data;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.opsli.plugins.excel.annotation.CellStyleFormat;
import java.io.Serializable;
/**
* Created Date by 2020/5/9 0009.
*
* Excel
* @author Parker
*/
@Data
@ContentRowHeight(16)
@HeadRowHeight(21)
@HeadFontStyle(fontName = "Arial",color = 9,fontHeightInPoints = 10)
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 23)
@ColumnWidth(22)
public class Test implements Serializable {
private static final long serialVersionUID = 1L;
/** 名称 */
@ExcelProperty(value = "名称",index = 0)
@CellStyleFormat
private String name;
/** 编号 */
@ExcelProperty(value = "编号",index = 1)
@CellStyleFormat
private String code;
/** 年龄 */
@ExcelProperty(value = "年龄",index = 2)
@CellStyleFormat
private Integer age;
/** 性别 */
@ExcelProperty(value = "性别",index = 3)
@CellStyleFormat
private Integer sex;
/** 金额 */
// @ExcelProperty(value = "金额",index = 4)
// @CellStyleFormat
private Double amt;
/**
*
*/
public Test() {
}
public Test(String name, String code, Integer age, Integer sex, Double amt) {
this.name = name;
this.code = code;
this.age = age;
this.sex = sex;
this.amt = amt;
}
}

@ -21,7 +21,15 @@ public class TestEntity extends BaseEntity {
/** 备注 */
private String remark;
// ========================================
/** 多租户字段 */
private String tenantId;
/** 逻辑删除字段 */
//@TableLogic
//private Integer deleted;
}

@ -3,10 +3,11 @@ package org.opsli.modulars.test.web;
import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.test.TestModel;
import org.opsli.api.web.test.TestApi;
import org.opsli.api.wrapper.test.TestModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.concroller.BaseRestController;
@ -23,8 +24,10 @@ import org.opsli.plugins.redis.RedisPlugin;
import org.opsli.plugins.redis.lock.RedisLock;
import org.opsli.plugins.redis.pushsub.entity.BaseSubMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@ -38,6 +41,7 @@ import java.util.concurrent.TimeUnit;
* @CreateTime: 2020-09-13 17:40
* @Description:
*/
@Slf4j
@ApiRestController("/test")
public class TestRestRestController extends BaseRestController<TestModel, TestEntity, ITestService>
implements TestApi {
@ -259,4 +263,25 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
return ResultVo.success(page.getBootstrapData());
}
@ApiOperation(value = "导出Excel", notes = "导出Excel")
@Override
public ResultVo<?> exportExcel(HttpServletRequest request, HttpServletResponse response) {
PageQueryBuilder<TestModel,TestEntity> pageQueryBuilder = new PageQueryBuilder<>(
TestEntity.class, request.getParameterMap()
);
return super.excelExport("测试", pageQueryBuilder.builderQueryWrapper(), response);
}
@ApiOperation(value = "导入Excel", notes = "导入Excel")
@Override
public ResultVo<?> excelImport(MultipartHttpServletRequest request) {
return super.excelImport(request);
}
@ApiOperation(value = "导出Excel模版", notes = "导出Excel模版")
@Override
public ResultVo<?> importTemplate(HttpServletResponse response) {
return super.importTemplate("测试", response);
}
}

@ -1,7 +1,20 @@
**建议在 当前 plugins 父类pom中定义子maven所引用的版本号**
## opsli 插件类
1. mail 邮件插件
## opsli 插件b包
0. ehcache 本地缓存插件
MsgCode
90000 - 90099
1. excel Excel插件
MsgCode
90100 - 90199
2. mail 邮件插件
MsgCode
90200 - 90299
3. redis 远程缓存插件
MsgCode
90300 - 90399

@ -0,0 +1,28 @@
<?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">
<parent>
<artifactId>opsli-plugins</artifactId>
<groupId>org.opsliframework.boot</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>opsli-plugins-excel</artifactId>
<version>${project.parent.version}</version>
<dependencies>
<!-- EasyExcel -->
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,193 @@
package org.opsli.plugins.excel;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.plugins.excel.exception.ExcelPluginException;
import org.opsli.plugins.excel.listener.ExcelListener;
import org.opsli.plugins.excel.msg.ExcelMsg;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
/**
* ExcelUtil
* easyExcelpoi3.17
* BeanCopy ExcelException
* java
* @author parker
*/
@Slf4j
public final class ExcelUtil {
/**
*
*/
private ExcelUtil(){}
/**
* Excel( sheet)
* sheetlistExcelReaderAnalysisEventListener
* invoke doAfterAllAnalysed
* getExtendsBeanList Bean ExcelReader
* @param excel
* @param rowModel BaseRowModel
* @return Excel list
*/
public static <T> List<T> readExcel(MultipartFile excel,Class<T> rowModel)
throws ExcelPluginException {
return readExcel(excel, rowModel, null, 1);
}
/**
* sheet Excel
* @param excel
* @param rowModel BaseRowModel
* @param sheetName sheet 1
* @return Excel list
*/
public static <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName)
throws ExcelPluginException{
return readExcel(excel, rowModel, sheetName, 1);
}
/**
* sheet Excel
* @param excel
* @param rowModel BaseRowModel
* @param sheetName sheet 1
* @param headLineNum 1
* @return Excel list
*/
public static <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName,
int headLineNum) throws ExcelPluginException {
ExcelListener excelListener = new ExcelListener();
InputStream inputStream = null;
try{
if(null != excel){
inputStream = excel.getInputStream();
}
}catch (IOException e){
log.error(e.getMessage(),e);
}
if(null == inputStream){
return Lists.newArrayList();
}
ExcelReader excelReader = EasyExcel.read(inputStream, rowModel, excelListener).build();
if (excelReader == null) {
return Lists.newArrayList();
}
ReadSheet readSheet;
if(StringUtils.isEmpty(sheetName)){
readSheet = EasyExcel.readSheet().build();
}else{
readSheet = EasyExcel.readSheet(sheetName).build();
}
readSheet.setHeadRowNumber(headLineNum);
excelReader.read(readSheet);
// 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
excelReader.finish();
return getExtendsBeanList(excelListener.getDataList(),rowModel);
}
/**
* Excel sheet
* WriterHandler
* @param response HttpServletResponse
* @param list list BaseRowModel
* @param fileName
* @param sheetName sheet
*/
public static <T> void writeExcel(HttpServletResponse response, List<T> list,
String fileName, String sheetName,
Class<T> classType, ExcelTypeEnum excelTypeEnum)
throws ExcelPluginException{
if(sheetName == null || "".equals(sheetName)){
sheetName = "sheet1";
}
fileName = fileName+ DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");
OutputStream outputStream = getOutputStream(fileName, response, excelTypeEnum);
ExcelWriter excelWriter = EasyExcel.write(outputStream, classType).build();
WriteSheet writeSheet = EasyExcel.writerSheet(1, sheetName).build();
writeSheet.setRelativeHeadRowIndex(0);
excelWriter.write(list,writeSheet);
// 关闭流
try {
excelWriter.finish();
outputStream.flush();
outputStream.close();
}catch (Exception e){
log.error(e.getMessage(),e);
}
}
/**
* WriterOutputStream
*/
private static OutputStream getOutputStream(String fileName, HttpServletResponse response,
ExcelTypeEnum excelTypeEnum)
throws ExcelPluginException{
//创建本地文件
String filePath = fileName + excelTypeEnum.getValue();
try {
fileName = new String(filePath.getBytes(), "ISO-8859-1");
response.addHeader("Content-Disposition", "filename=" + fileName);
return response.getOutputStream();
} catch (IOException e) {
// 创建文件失败
throw new ExcelPluginException(ExcelMsg.EXCEPTION_CREATE_ERROR);
}
}
/**
* ExcelReader
* @param excel Excel
* @param excelListener new ExcelListener()
*/
private static ExcelReader getReader(MultipartFile excel,
ExcelListener excelListener) throws ExcelPluginException{
String fileName = excel.getOriginalFilename();
if (fileName == null ) {
// 文件格式错误
throw new ExcelPluginException(ExcelMsg.EXCEPTION_FILE_FORMAT);
}
if (!fileName.toLowerCase().endsWith(ExcelTypeEnum.XLS.getValue()) && !fileName.toLowerCase()
.endsWith(ExcelTypeEnum.XLSX.getValue())) {
// 文件格式错误
throw new ExcelPluginException(ExcelMsg.EXCEPTION_FILE_FORMAT);
}
InputStream inputStream;
try {
inputStream = excel.getInputStream();
return EasyExcel.read(inputStream, excelListener).build();
} catch (IOException e) {
//do something
log.error(e.getMessage(),e);
}
return null;
}
/**
* BeanCopylist
*/
public static <T> List<T> getExtendsBeanList(List<?> list,Class<T> typeClazz){
return WrapperUtil.cloneTransformInstance(list,typeClazz);
}
}

@ -0,0 +1,24 @@
package org.opsli.plugins.excel.annotation;
import org.apache.poi.ss.usermodel.IndexedColors;
import java.lang.annotation.*;
/**
* Created Date by 2020/5/9 0009.
*
* @author Parker
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CellFontFormat {
String fontName() default "Arial";
short fontHeightInPoints() default 10;
IndexedColors fontColor() default IndexedColors.BLACK;
boolean bold() default false;
}

@ -0,0 +1,40 @@
package org.opsli.plugins.excel.annotation;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import java.lang.annotation.*;
/**
* Created Date by 2020/5/9 0009.
*
* Excel
*
* @author parker
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CellStyleFormat {
/**
*
* @see HorizontalAlignment
*/
HorizontalAlignment horizontalAlignment() default HorizontalAlignment.LEFT;
/**
*
* @see org.apache.poi.xssf.usermodel.XSSFFont
* @see org.apache.poi.hssf.usermodel.HSSFFont
*/
CellFontFormat cellFont() default @CellFontFormat();
/**
*
* @see IndexedColors
*/
IndexedColors fillBackgroundColor() default IndexedColors.WHITE;
}

@ -0,0 +1,22 @@
package org.opsli.plugins.excel.exception;
import org.opsli.common.base.msg.BaseMsg;
import org.opsli.common.exception.ServiceException;
/**
* Created Date by 2020/5/9 0009.
*
* Excel
* @author Parker
*/
public class ExcelPluginException extends ServiceException {
public ExcelPluginException(Integer code, String errorMessage) {
super(code, errorMessage);
}
public ExcelPluginException(BaseMsg msg) {
super(msg);
}
}

@ -0,0 +1,60 @@
package org.opsli.plugins.excel.factory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
/**
* ExcelWriterFactory
*
* @author parker
*/
public class ExcelWriterFactory extends ExcelWriter {
private OutputStream outputStream;
private int sheetNo = 1;
public ExcelWriterFactory(OutputStream outputStream, ExcelTypeEnum typeEnum) {
super(outputStream, typeEnum);
this.outputStream = outputStream;
}
public ExcelWriterFactory write(List<?> list, String sheetName,
BaseRowModel object) {
this.sheetNo++;
try {
Sheet sheet = new Sheet(sheetNo, 0, object.getClass());
sheet.setSheetName(sheetName);
this.write(list, sheet);
} catch (Exception ex) {
try {
outputStream.flush();
} catch (IOException e) {
//do something
}
}
return this;
}
@Override
public void finish() {
super.finish();
try {
outputStream.flush();
} catch (IOException e) {
//do something
}
}
public void close(){
try {
outputStream.close();
} catch (IOException e) {
//do something
}
}
}

@ -0,0 +1,65 @@
package org.opsli.plugins.excel.listener;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* Excel
* @author parker
*/
public class ExcelListener extends AnalysisEventListener {
private static final String SERIAL_VERSION_UID = "serialVersionUID";
private List<Object> dataList = new ArrayList<>();
/**
* AnalysisContext sheet
*/
@Override
public void invoke(Object object, AnalysisContext context) {
if(!checkObjAllFieldsIsNull(object)) {
dataList.add(object);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
//do something
}
/**
*
*/
private static boolean checkObjAllFieldsIsNull(Object object) {
if (null == object) {
return true;
}
try {
for (Field f : object.getClass().getDeclaredFields()) {
f.setAccessible(true);
//只校验带ExcelProperty注解的属性
ExcelProperty property = f.getAnnotation(ExcelProperty.class);
if(property == null || SERIAL_VERSION_UID.equals(f.getName())){
continue;
}
if (f.get(object) != null && StringUtils.isNotBlank(f.get(object).toString())) {
return false;
}
}
} catch (Exception e) {
//do something
}
return true;
}
public List<Object> getDataList() {
return dataList;
}
}

@ -0,0 +1,37 @@
package org.opsli.plugins.excel.msg;
import org.opsli.common.base.msg.BaseMsg;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.plugins.mail.msg
* @Author: Parker
* @CreateTime: 2020-09-13 19:54
* @Description: Redis
*/
public enum ExcelMsg implements BaseMsg {
/** Excel 异常 */
EXCEPTION_FILE_FORMAT(90000,"文件格式错误!"),
EXCEPTION_CREATE_ERROR(90000,"创建文件失败!"),
;
private int code;
private String message;
ExcelMsg(int code, String message){
this.code = code;
this.message = message;
}
@Override
public Integer getCode() {
return this.code;
}
@Override
public String getMessage() {
return this.message;
}
}

@ -12,7 +12,7 @@ import org.opsli.common.base.msg.BaseMsg;
public enum MailMsg implements BaseMsg {
/** 未知消息异常 */
EXCEPTION_UNKNOWN(90001,"邮件发送失败"),
EXCEPTION_UNKNOWN(90200,"邮件发送失败"),
;

@ -12,11 +12,11 @@ import org.opsli.common.base.msg.BaseMsg;
public enum RedisMsg implements BaseMsg {
/** Redis异常 */
EXCEPTION_KEY_NULL(90100,"Key不可为空"),
EXCEPTION_INCREMENT(90101,"递增值必须大于0"),
EXCEPTION_DECREMENT(90102,"递减值必须大于0"),
EXCEPTION_REFLEX(90103,"反射Redis脚本失败"),
EXCEPTION_PUSH_SUB_NULL(90104,"发布消息体不可为空!"),
EXCEPTION_KEY_NULL(90300,"Key不可为空"),
EXCEPTION_INCREMENT(90301,"递增值必须大于0"),
EXCEPTION_DECREMENT(90302,"递减值必须大于0"),
EXCEPTION_REFLEX(90303,"反射Redis脚本失败"),
EXCEPTION_PUSH_SUB_NULL(90304,"发布消息体不可为空!"),
;

@ -19,6 +19,7 @@
<module>opsli-plugins-mail</module>
<module>opsli-plugins-redis</module>
<module>opsli-plugins-ehcache</module>
<module>opsli-plugins-excel</module>
</modules>
<dependencyManagement>

@ -1,9 +1,10 @@
# tomcat 配置
server:
tomcat:
#basedir: /Users/xxx
# Tomcat 编码
uri-encoding: UTF-8
# 对于 Tomcat 头清秀不限制大小
# 对于 Tomcat 头文件不限制大小
max-swallow-size: -1
servlet:
context-path: /opsli-boot

@ -49,8 +49,6 @@
<modules>
<module>opsli-api</module>
<module>opsli-base-support</module>
<!-- <module>opsli-common</module>-->
<!-- <module>opsli-core</module>-->
<module>opsli-plugins</module>
<module>opsli-modulars</module>
<module>opsli-starter</module>

Loading…
Cancel
Save