1:相关maven 依赖
easypoi 官方传送门http://easypoi.mydoc.io/#text_186900
springboot 相关依赖省略
easypoi 相关依赖
cn.afterturn
easypoi-base
3.0.3
cn.afterturn
easypoi-web
3.0.3
cn.afterturn
easypoi-annotation
3.0.3
2:自己封装的工具类(关键 请看方法上带有注释的;不带注释的直接调用的是easypoi 的封装好的方法 )
package com.clouderwork.common.excel;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.clouderwork.contant.GlobalConstant;
import com.clouderwork.enums.YESNOEnum;
import com.clouderwork.params.order.ExportField;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.*;
/**
* @author : heyanfeng
* create at: 2019-05-27 19:11
* @description: excel
*/
@Slf4j
public class ExcelUtils {
public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass,String fileName, HttpServletResponse response){
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List
3:动态表头相关的参数bean
package com.clouderwork.params.order;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import java.util.List;
/**
* @program family_dr_api
* @description: 管理后台患者订单
* @author: luqiang
* @create: 2019/05/24 19:14
*/
@ApiModel(value = "管理后台患者订单")
@Data
public class BackPatientOrderParam {
@ApiModelProperty("导出字段,逗号分割")
private List exportField;
}
ExportField.class
package com.clouderwork.params.order;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* @author : heyanfeng
* create at: 2019-05-27 22:00
* @description:
*/
@ApiModel(value = "管理后台患者订单导出的字段")
@Data
public class ExportField {
@ApiModelProperty("实体映射的字段")
private String entityAttrName;
@ApiModelProperty("导出字段,中文描述")
private String filedChineseName;
@ApiModelProperty("组装sql 字段")
private String filed;
@ApiModelProperty("sql-group-by去除字段:0 否 1是")
private Integer groupBy;
@ApiModelProperty("合并单元格")
private List children;
}
4:demo 演示
controller
public void serviceExport(@Valid @RequestBody BackPatientOrderParam backPatientOrderParam) {
serviceOrderService.serviceExport(backPatientOrderParam);
}
service
public void serviceExport(BackPatientOrderParam params) {
//根据传递的sql 参数动态组装查询的字段
String fileds = ExcelUtils.getExcelSql(params.getExportField());
// 获取response
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//数据请替换自己业务的数据
List list = serviceOrderMapperExt.serviceExport(startYear, endYear,
params.getServiceName(), params.getDoctorName(),
params.getDoctorGroupId(), params.getOrderStatus(),
startPayTime, endPayTime, startCompleteTime, endCompleteTime,
params.getProvinceCode(), params.getCityCode(),
params.getCountyCode(), params.getRecommendUserName(), fileds);
ExcelUtils.DynamicExcel("服务订单" + new DateTime().toString("yyyy-MM-dd") + ".xlsx", null,
"sheet1", params.getExportField(), response, list);
}
前端传过来的参数
说明(指定 children可以 合并单元格)多个字段驻向数组中追加
export const filedData = [{
"entityAttrName": "orderNo",
"value": "a.order_no",
"filed": "a.order_no",
"filedChineseName": "订单号码"
},
{
"entityAttrName": "merge",
"filed": "",
"value": "",
"filedChineseName": "基础服务费",
"children": [{
"entityAttrName": "basePrice",
"filed": "d.base_price",
"value": "d.base_price",
"filedChineseName": "基础服务费"
} ]
}
]