后端通用生成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;
}
}