后端通用生成excel 以及 协会信息数据统计带数据导出

This commit is contained in:
yuezhihang
2022-03-03 15:26:42 +08:00
parent 48a0181942
commit b9e440c92c
6 changed files with 348 additions and 11 deletions
@@ -2,17 +2,23 @@ package com.adc.da.slrs.processModel.utils;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.adc.da.excel.poi.excel.entity.ExportParams;
import com.adc.da.excel.poi.excel.entity.enums.ExcelType;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class ExcelExportUtilByWk {
@@ -71,6 +77,85 @@ public class ExcelExportUtilByWk {
}
public static Workbook exportWithData(ExportParams sheetName, Class<?> tClass , List<?> datas) {
Workbook wb=null;
if(sheetName.getType().equals(ExcelType.XSSF)){
wb = new XSSFWorkbook();
}else {
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
Sheet sheet = wb.createSheet(sheetName.getSheetName());
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
Row row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
//声明列对象
Cell cell = null;
Map<Integer,Excel> map=new HashMap<>();
Map<Integer,Field> mapField=new HashMap<>();
Field[] fields=tClass.getDeclaredFields();
for (Field field:fields) {
field.setAccessible(true);
Excel excel=field.getAnnotation(Excel.class);
if (null!=excel) {
map.put(Integer.valueOf(excel.orderNum()), excel);
mapField.put(Integer.valueOf(excel.orderNum()), field);
}
}
//创建标题
DataFormat df= wb.createDataFormat();
for (Integer i:map.keySet()) {
sheet.setColumnWidth(i, (int) (map.get(i).width()*256));
cell = row.createCell(i);
cell.setCellValue(map.get(i).name());
if (null!=map.get(i).exportFormat()) {
CellStyle style1 = wb.createCellStyle();
style1.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
style1.setDataFormat(df.getFormat(map.get(i).exportFormat()));
sheet.setDefaultColumnStyle(i, style1);
}
cell.setCellStyle(style);
}
//赋值数据
int rowNum=1;
for (Object o:datas) {
Row row1=sheet.createRow(rowNum);
for (Integer i:map.keySet()) {
Cell cell1=row1.createCell(i);
Field field= null;
try {
field = o.getClass().getDeclaredField(mapField.get(i).getName());
} catch (NoSuchFieldException e) {
log.warn(e.getMessage());
}
field.setAccessible(true);
try {
Object fieldVal = field.get(o);
if (null!=map.get(i).exportFormat() && !"".equals(map.get(i).exportFormat())) {
cell1.setCellValue("null".equals(String.valueOf(fieldVal)) ? "" : TrainFormDate.dealWithTimeZoneyyyy(String.valueOf(fieldVal),map.get(i).exportFormat()));
}else {
cell1.setCellValue("null".equals(String.valueOf(fieldVal)) ? "" : String.valueOf(fieldVal));
}
} catch (IllegalAccessException e) {
log.warn(e.getMessage());
}
}
System.out.println(o.toString());
rowNum++;
}
return wb;
}
public static HSSFWorkbook putValue(HSSFSheet sheet, HSSFWorkbook wb, Map<String, Object> dicTypeEO) {
JSONArray array=JSONArray.parseArray(JSON.toJSONString(dicTypeEO.get("BUSSSTANDCLASS")));
@@ -0,0 +1,169 @@
package com.adc.da.slrs.processModel.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;
//日期格式化工具
public class TrainFormDate {
public static String dealWithTimeZone(String time) {
DateFormat formate1 = new SimpleDateFormat("yyyy/MM/dd");
if (null == time) {
return null;
}
time = time.replace("", "/");
time = time.replace("", "/");
time = time.replace("", "");
time = time.replace("-", "/");
time = time.replace(" ", "");
String times="";
String[] da=time.split("/");
for (String s:da) {
if (s.trim().length()==1){
s="0"+s;
}
times+=s+"/";
}
times=times.trim().substring(0,times.length()-1);
Pattern p = Pattern.compile("^(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.)(?:0?2\\1(?:29))$)|(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]))$");
if (p.matcher(times).matches()) {
return times;
} else {
DateFormat formate2 = new SimpleDateFormat("EEEMMMddHH:mm:sszzzyyyy", Locale.ENGLISH);
Date date = null;
try {
date = formate2.parse(time);
String dateString = formate1.format(date);
return dateString;
} catch (ParseException e1) {
try {
int days = Integer.parseInt(time);
Calendar c = Calendar.getInstance();
c.set(1900, 0, 1);
c.add(Calendar.DATE, days - 2);
Date realdate = c.getTime();
String realday = formate1.format(realdate);
return realday;
}
catch (Exception e2){
return null;
}
}
}
}
public static String dealWithTimeZoneyyyy(String time,String pattern) {
DateFormat formate1 = new SimpleDateFormat(pattern);
if (null == time) {
return null;
}
time = time.replace("/","-");
time = time.replace("", "-");
time = time.replace("", "-");
time = time.replace("", "");
time = time.replace(" ", "");
String times="";
String[] da=time.split("-");
for (String s:da) {
if (s.trim().length()==1){
s="0"+s;
}
times+=s+"-";
}
times=times.trim().substring(0,times.length()-1);
Pattern p = Pattern.compile("^(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.)(?:0?2\\1(?:29))$)|(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]))$");
if (p.matcher(times).matches()) {
return times;
} else {
DateFormat formate2 = new SimpleDateFormat("EEEMMMddHH:mm:sszzzyyyy", Locale.ENGLISH);
Date date = null;
try {
date = formate2.parse(time);
String dateString = formate1.format(date);
return dateString;
} catch (ParseException e1) {
try {
int days = Integer.parseInt(time);
Calendar c = Calendar.getInstance();
c.set(1900, 0, 1);
c.add(Calendar.DATE, days - 2);
Date realdate = c.getTime();
String realday = formate1.format(realdate);
return realday;
}
catch (Exception e2){
return null;
}
}
}
}
public static String dealWithTimeZoneHHMM(String time) {
if (time==null||time.equals("")){
return null;
}
DateFormat formate1 = new SimpleDateFormat("HH:mm");
time = time.replace(" ", "");
Pattern p = Pattern.compile("^(([0-9])|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$");
if (p.matcher(time).matches()) {
return time;
} else {
DateFormat formate2 = new SimpleDateFormat("EEEMMMddHH:mm:sszzzyyyy", Locale.ENGLISH);
Date date = null;
try {
date = formate2.parse(time);
String dateString = formate1.format(date);
return dateString;
} catch (ParseException e1) {
return null;
}
}
}
public static String dealWithTimeZoneHHMMSS(String time) {
if (time==null||time.equals("")){
return null;
}
DateFormat formate1 = new SimpleDateFormat("HH:mm:ss");
time = time.replace(" ", "");
Pattern p = Pattern.compile("^(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$");
if (p.matcher(time).matches()) {
return time;
} else {
DateFormat formate2 = new SimpleDateFormat("EEEMMMddHH:mm:sszzzyyyy", Locale.ENGLISH);
Date date = null;
try {
date = formate2.parse(time);
String dateString = formate1.format(date);
return dateString;
} catch (ParseException e1) {
return null;
}
}
}
//转换标点符号
public static String changeCE(String change) {
change = change.replace(",", "");
return change;
}
}
@@ -2,7 +2,6 @@ package com.adc.da.slrs.sarLawsStandInfo.controller;
import com.adc.da.common.ReadExcel;
import com.adc.da.excel.poi.excel.ExcelExportUtil;
import com.adc.da.exception.AdcDaBaseException;
import com.adc.da.person.service.IPersonCollectEOService;
import com.adc.da.slrs.sarLawsStandInfo.entity.SarLawsStandInfoPage;
@@ -12,14 +11,12 @@ import com.adc.da.slrs.sarStandardsInfo.entity.SarAdvanceSearchVO;
import com.adc.da.http.PageInfo;
import com.adc.da.http.ResponseMessage;
import com.adc.da.http.Result;
import com.adc.da.slrs.sarStandardsInfo.entity.StandardsInfoExcelVO;
import com.adc.da.slrs.sarUser.service.ITsUserService;
import com.adc.da.sys.entity.DicTypeEO;
import com.adc.da.sys.service.IDicTypeEOService;
import com.adc.da.util.utils.StringUtils;
import com.adc.da.utils.util.LawsStandExportUtil;
import com.adc.da.utils.util.SarAdvanceSearchUtil;
import com.adc.da.utils.util.StandExportUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
@@ -1,18 +1,12 @@
package com.adc.da.slrs.sarStandardComplianceAssessResult.controller;
import cn.hutool.core.map.MapUtil;
import com.adc.da.base.web.BaseController;
import com.adc.da.http.ResponseMessage;
import com.adc.da.http.Result;
import com.adc.da.slrs.sarSarAccessInfo.entity.SarSarAccessInfo;
import com.adc.da.slrs.sarStandItems.entity.SarStandItemsDto;
import com.adc.da.slrs.sarStandardComplianceAssessResult.entity.*;
import com.adc.da.slrs.sarStandardComplianceAssessResult.service.ISarInterpretationForeignStandardService;
import com.adc.da.slrs.sarStandardComplianceAssessResult.service.ISarInterpretationNationalStandardService;
import com.adc.da.slrs.sarStandardComplianceAssessResult.service.ISarStandardComplianceProductAssessService;
import com.adc.da.slrs.sarStandardsInfo.entity.SarStandardsInfo;
import com.adc.da.slrs.sarStandardsInfo.service.ISarStandardsInfoService;
import com.adc.da.util.UUIDUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -1,17 +1,35 @@
package com.adc.da.slrs.wgdCensusZ.controller;
import com.adc.da.excel.poi.excel.ExcelExportUtil;
import com.adc.da.excel.poi.excel.entity.ExportParams;
import com.adc.da.excel.poi.excel.entity.enums.ExcelType;
import com.adc.da.http.PageInfo;
import com.adc.da.http.ResponseMessage;
import com.adc.da.http.Result;
import com.adc.da.slrs.msgDynamicInfo.common.ReadExcel;
import com.adc.da.slrs.msgDynamicInfo.dto.MsgDynamicInfoExportDto;
import com.adc.da.slrs.msgDynamicInfo.page.MsgDynamicInfoEOPage;
import com.adc.da.slrs.processModel.utils.ExcelExportUtilByWk;
import com.adc.da.slrs.wgdCensusZ.service.IWgdAssocInfoService;
import com.adc.da.util.exception.AdcDaBaseException;
import com.adc.da.util.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.adc.da.slrs.wgdCensusZ.entity.WgdAssocInfo;
import io.swagger.annotations.Api;
import com.adc.da.base.web.BaseController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@@ -34,6 +52,40 @@ public class WgdAssocInfoController extends BaseController<WgdAssocInfo> {
return Result.success(iWgdAssocInfoService.getAllWgdAssocInfos());
}
@GetMapping("exportAllAssocInfos")
public void exportAllAssocInfos(String idList, String exportName, HttpServletResponse response, HttpServletRequest request){//查询所有
OutputStream os = null;
Workbook workbook = null;
try {
if(StringUtils.isEmpty(exportName) ||exportName.equals("null")){
exportName="协会信息数据统计";
}
response.setHeader("Content-Disposition",
"attachment; filename=" + ReadExcel.encodeFileName(exportName+".xlsx", request));
response.setContentType("application/force-download");
ExportParams exportParams = new ExportParams();
exportParams.setType(ExcelType.XSSF);
exportParams.setSheetName(exportName);
List<WgdAssocInfo> datas=new ArrayList<>();
if (null!=idList) {
List<String> ids = new ArrayList<>(Arrays.asList(idList.split(",")));
QueryWrapper<WgdAssocInfo> wrapper=new QueryWrapper<>();
wrapper.in("id",ids);
datas = iWgdAssocInfoService.list(wrapper);
} else {
datas = iWgdAssocInfoService.getAllWgdAssocInfos();
}
workbook = ExcelExportUtilByWk.exportWithData(exportParams,WgdAssocInfo.class,datas);
os = response.getOutputStream();
workbook.write(os);
os.flush();
} catch (IOException e) {
throw new AdcDaBaseException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(os);
}
}
@ApiOperation("查询")
@GetMapping("queryAllAssocInfos")
public ResponseMessage<Object> queryAllWgdAssocInfos(WgdAssocInfo wgdAssocInfo){//多条件查询
@@ -3,6 +3,8 @@ package com.adc.da.slrs.wgdCensusZ.entity;
import java.util.Date;
import com.adc.da.base.page.BasePage;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@@ -28,117 +30,155 @@ public class WgdAssocInfo extends BasePage {
private String id;
@ApiModelProperty(value = "协会名称-标委会")
@Excel(name = "协会名称-标委会", orderNum = "0",width = 20.0)
private String scName;
@ApiModelProperty(value = "协会名称-分标委")
@Excel(name = "协会名称-分标委", orderNum = "1",width = 20.0)
private String sscName;
@ApiModelProperty(value = "协会名称-工作组")
@Excel(name = "协会名称-分标委", orderNum = "2",width = 20.0)
private String wgName;
@ApiModelProperty(value = "会员角色")
@Excel(name = "会员角色", orderNum = "3",width = 20.0)
private String role;
@ApiModelProperty(value = "参与人")
@Excel(name = "参与人", orderNum = "4",width = 20.0)
private String attend;
@ApiModelProperty(value = "标准")
@Excel(name = "标准", orderNum = "5",width = 20.0)
private String standard;
@ApiModelProperty(value = "标准负责人")
@Excel(name = "标准负责人", orderNum = "6",width = 20.0)
private String charge;
@ApiModelProperty(value = "责任领导")
@Excel(name = "责任领导", orderNum = "7",width = 20.0)
private String leader;
@ApiModelProperty(value = "初始入会时间")
@Excel(name = "初始入会时间", orderNum = "8",width = 20.0 , exportFormat = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date iniTime;
@ApiModelProperty(value = "本届入会时间")
@Excel(name = "本届入会时间", orderNum = "9",width = 20.0 , exportFormat = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date curTime;
@ApiModelProperty(value = "费用标准")
@Excel(name = "费用标准", orderNum = "10",width = 20.0)
private String costStandard;
@ApiModelProperty(value = "费用列支")
@Excel(name = "费用列支", orderNum = "11",width = 20.0)
private String costOutlay;
@ApiModelProperty(value = "上次缴纳时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Excel(name = "上次缴纳时间", orderNum = "12",width = 20.0 , exportFormat = "yyyy-MM-dd HH:mm:ss")
private Date lastPayTime;
@ApiModelProperty(value = "缴费年限")
@Excel(name = "缴费年限", orderNum = "13",width = 20.0)
private String payPeriod;
@ApiModelProperty(value = "经办人")
@Excel(name = "经办人", orderNum = "14",width = 20.0)
private String handler;
@ApiModelProperty(value = "部门")
@Excel(name = "部门", orderNum = "15",width = 20.0)
private String department;
@ApiModelProperty(value = "协会性质")
@Excel(name = "协会性质", orderNum = "16",width = 20.0)
private String asProper;
@ApiModelProperty(value = "协会状态")
@Excel(name = "协会状态", orderNum = "17",width = 20.0)
private String asStatus;
@ApiModelProperty(value = "协会联络人")
@Excel(name = "协会联络人", orderNum = "18",width = 20.0)
private String asContact;
@ApiModelProperty(value = "协会联络人电话")
@Excel(name = "协会联络人电话", orderNum = "19",width = 20.0)
private String tel;
@ApiModelProperty(value = "协会联络人邮箱")
@Excel(name = "协会联络人邮箱", orderNum = "20",width = 20.0)
private String mail;
@ApiModelProperty(value = "2017缴费")
@Excel(name = "2017缴费", orderNum = "21",width = 20.0)
private String p17;
@ApiModelProperty(value = "2018缴费")
@Excel(name = "2018缴费", orderNum = "22",width = 20.0)
private String p18;
@ApiModelProperty(value = "2019缴费")
@Excel(name = "2019缴费", orderNum = "23",width = 20.0)
private String p19;
@ApiModelProperty(value = "2020缴费")
@Excel(name = "2020缴费", orderNum = "24",width = 20.0)
private String p20;
@ApiModelProperty(value = "2021缴费")
@Excel(name = "2021缴费", orderNum = "25",width = 20.0)
private String p21;
@ApiModelProperty(value = "2022缴费")
@Excel(name = "2022缴费", orderNum = "26",width = 20.0)
private String p22;
@ApiModelProperty(value = "2023缴费")
@Excel(name = "2023缴费", orderNum = "27",width = 20.0)
private String p23;
@ApiModelProperty(value = "2024缴费")
@Excel(name = "2024缴费", orderNum = "28",width = 20.0)
private String p24;
@ApiModelProperty(value = "2025缴费")
@Excel(name = "2025缴费", orderNum = "29",width = 20.0)
private String p25;
@ApiModelProperty(value = "2026缴费")
@Excel(name = "2026缴费", orderNum = "30",width = 20.0)
private String p26;
@ApiModelProperty(value = "2027缴费")
@Excel(name = "2027缴费", orderNum = "31",width = 20.0)
private String p27;
@ApiModelProperty(value = "2028缴费")
@Excel(name = "2028缴费", orderNum = "32",width = 20.0)
private String p28;
@ApiModelProperty(value = "2029缴费")
@Excel(name = "2029缴费", orderNum = "33",width = 20.0)
private String p29;
@ApiModelProperty(value = "2030缴费")
@Excel(name = "2030缴费", orderNum = "34",width = 20.0)
private String p30;
@ApiModelProperty(value = "2031缴费")
@Excel(name = "2031缴费", orderNum = "35",width = 20.0)
private String p31;
@ApiModelProperty(value = "2032缴费")
@Excel(name = "2032缴费", orderNum = "36",width = 20.0)
private String p32;