人员导出

pull/126/MERGE^2
Chengliang 3 years ago
parent 0ad6157c26
commit 783b9fc6bf

@ -2,6 +2,7 @@ package com.engine.organization.mapper.resource;
import com.engine.organization.entity.hrmresource.param.HrmResourceSearchParam;
import com.engine.organization.entity.hrmresource.po.HrmResourcePO;
import com.engine.organization.entity.hrmresource.vo.HrmResourceVO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -14,5 +15,5 @@ import java.util.List;
**/
public interface ResourceMapper {
List<HrmResourcePO> listAll(@Param("param")HrmResourceSearchParam param);
List<HrmResourceVO> listAll(@Param("param")HrmResourceSearchParam param);
}

@ -9,49 +9,52 @@
id,last_name,department_id,company_id,mobile,telephone,manager_id
</sql>
<select id="listAll" resultType="com.engine.organization.entity.hrmresource.po.HrmResourcePO">
SELECT
<include refid="baseColumns"/>
FROM jcl_org_hrmresource
<select id="listAll" resultType="com.engine.organization.entity.hrmresource.vo.HrmResourceVO">
SELECT t.last_name as lastName,d.dept_name as departmentName,
c.comp_name as companyName,t.mobile,t.telephone,t1.last_name as managerName
from jcl_org_hrmresource t
left join JCL_ORG_DEPT d on t.department_id = d.id
left join jcl_org_comp c on t.company_id = c.id
left join jcl_org_hrmresource t1 on t.manager_id = t1.id
where 1 = 1
<include refid="likeSql"/>
<if test="param.departmentId != null and param.departmentId != ''">
and department_id = #{param.departmentId}
and t.department_id = #{param.departmentId}
</if>
<if test="param.companyId != null and param.companyId != ''">
and company_id = #{param.companyId}
and t.company_id = #{param.companyId}
</if>
<if test="param.mobile != null and param.mobile != ''">
and mobile = #{param.mobile}
and t.mobile = #{param.mobile}
</if>
<if test="param.telephone != null and param.telephone != ''">
and telephone = #{param.telephone}
and t.telephone = #{param.telephone}
</if>
<if test="param.managerId != null and param.managerId != ''">
and manager_id = #{param.managerId}
and t.manager_id = #{param.managerId}
</if>
<if test="param.mobileCall != null and param.mobileCall != ''">
and mobile_call = #{param.mobileCall}
and t.mobile_call = #{param.mobileCall}
</if>
<if test="param.jobTitle != null and param.jobTitle != ''">
and job_title = #{param.jobTitle}
and t.job_title = #{param.jobTitle}
</if>
order by id asc;
order by t.id asc;
</select>
<sql id="likeSql">
<if test="param.lastName != null and param.lastName != ''">
AND last_name like CONCAT('%',#{param.lastName},'%')
AND t.last_name like CONCAT('%',#{param.lastName},'%')
</if>
</sql>
<sql id="likeSql" databaseId="oracle">
<if test="param.lastName != null and param.lastName != ''">
AND last_name like '%'||#{param.lastName}||'%'
AND t.last_name like '%'||#{param.lastName}||'%'
</if>
</sql>
<sql id="likeSql" databaseId="sqlserver">
<if test="param.lastName != null and param.lastName != ''">
AND last_name like '%'+#{param.lastName}+'%'
AND t.last_name like '%'+#{param.lastName}+'%'
</if>
</sql>

@ -2,12 +2,12 @@ package com.engine.organization.service.impl;
import com.engine.core.impl.Service;
import com.engine.organization.entity.hrmresource.param.HrmResourceSearchParam;
import com.engine.organization.entity.hrmresource.po.HrmResourcePO;
import com.engine.organization.entity.hrmresource.vo.HrmResourceVO;
import com.engine.organization.mapper.resource.ResourceMapper;
import com.engine.organization.service.ExportCommonService;
import com.engine.organization.util.HrmI18nUtil;
import com.engine.organization.util.db.MapperProxyFactory;
import com.engine.salary.util.excel.ExcelUtil;
import com.engine.organization.util.excel.ExcelUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.*;
@ -25,9 +25,9 @@ public class ExportCommonServiceImpl extends Service implements ExportCommonServ
param.setPageSize(null);
param.setCurrent(null);
List<HrmResourcePO> hrmResourcePOS = MapperProxyFactory.getProxy(ResourceMapper.class).listAll(param);
if (hrmResourcePOS == null) {
hrmResourcePOS = new ArrayList<>();
List<HrmResourceVO> hrmResourceVOS = MapperProxyFactory.getProxy(ResourceMapper.class).listAll(param);
if (hrmResourceVOS == null) {
hrmResourceVOS = new ArrayList<>();
}
// 1.工作簿名称
String sheetName = HrmI18nUtil.getI18nLabel(85368, "人员档案数据");
@ -37,23 +37,22 @@ public class ExportCommonServiceImpl extends Service implements ExportCommonServ
String[] header = {
HrmI18nUtil.getI18nLabel( 93270, "姓名"),
HrmI18nUtil.getI18nLabel( 93272, "部门"),
HrmI18nUtil.getI18nLabel( 93273, "公积金人数"),
HrmI18nUtil.getI18nLabel( 93274, "分部"),
HrmI18nUtil.getI18nLabel( 93275, "移动电话"),
HrmI18nUtil.getI18nLabel( 93278, "办公室电话"),
HrmI18nUtil.getI18nLabel( 93279, "直接上级")};
excelSheetData.add(Arrays.asList(new Object[]{header}));
excelSheetData.add(Arrays.asList(header));
//工作簿数据
//数据
List<List<Object>> rows = new LinkedList<>();
for (HrmResourcePO po : hrmResourcePOS) {
for (HrmResourceVO vo : hrmResourceVOS) {
List<Object> row = new LinkedList<>();
row.add(po.getLastName());
row.add(po.getDepartmentId());
row.add(po.getCompanyId());
row.add(po.getMobile());
row.add(po.getTelephone());
row.add(po.getManagerId());
row.add(vo.getLastName());
row.add(vo.getDepartmentName());
row.add(vo.getCompanyName());
row.add(vo.getMobile());
row.add(vo.getTelephone());
row.add(vo.getManagerName());
rows.add(row);
}
excelSheetData.addAll(rows);

@ -235,11 +235,11 @@ public class HrmResourceServiceImpl extends Service implements HrmResourceServic
ArrayList<MenuBtn> rightMenuList = new ArrayList<>();
topMenuList.add(MenuBtn.builder().isBatch("1").isTop("1").menuFun("new").menuIcon("icon-coms-New-Flow").menuName("新建人员").type("BTN_Addnew").build());
topMenuList.add(MenuBtn.builder().isBatch("1").isTop("1").menuFun("import").menuIcon("icon-coms-leading-in").menuName("导入人员").type("BTN_Import").build());
topMenuList.add(MenuBtn.builder().isBatch("1").isTop("1").menuFun("export").menuIcon("icon-coms02-coms2-export").menuName("导出").type("BTN_Export").build());
topMenuList.add(MenuBtn.builder().isBatch("1").isTop("1").menuFun("export").menuIcon("icon-coms02-coms2-export").menuName("全部导出").type("BTN_Export").build());
btnDatas.put("topMenu", topMenuList);
rightMenuList.add(MenuBtn.builder().isBatch("1").isTop("1").menuFun("new").menuIcon("icon-coms-New-Flow").menuName("新建人员").type("BTN_Addnew").build());
rightMenuList.add(MenuBtn.builder().isBatch("1").isTop("1").menuFun("import").menuIcon("icon-coms-leading-in").menuName("导入人员").type("BTN_Import").build());
rightMenuList.add(MenuBtn.builder().isBatch("1").isTop("1").menuFun("export").menuIcon("icon-coms02-coms2-export").menuName("导出").type("BTN_Export").build());
rightMenuList.add(MenuBtn.builder().isBatch("1").isTop("1").menuFun("export").menuIcon("icon-coms02-coms2-export").menuName("全部导出").type("BTN_Export").build());
rightMenuList.add(MenuBtn.builder().isBatch("0").isTop("0").menuFun("custom").menuIcon("icon-coms-task-list").menuName("显示列定制").type("BTN_COLUMN").build());
btnDatas.put("rightMenu", rightMenuList);
return btnDatas;

@ -0,0 +1,460 @@
package com.engine.organization.util;
import com.engine.salary.common.LocalDateRange;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import weaver.general.BaseBean;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class OrganizationDateUtil {
public static final ZoneId CTT = ZoneId.of(ZoneId.SHORT_IDS.get("CTT"));
public static final ZoneOffset SHANGHAI_ZONE_OFF_SET = ZoneOffset.ofHours(8);
public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
public static final FastDateFormat DATETIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
public static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final String MONTH_FORMATTER_PATTERN = "yyyy-MM";
public static final String DATE_FORMATTER_PATTERN = "yyyy-MM-dd";
public static final String DATE_TIME_FORMATTER_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* yyyy-MM
**/
private static final String MONTH_REGEX = "^([1-9]\\d{3})-(([0]{0,1}[1-9])|([1][0-2]))$";
/**
* yyyy-MM-dd
**/
private static final String DAY_REGEX = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$";
/**
*
*/
private static final String DAY_BAR_REGEX = "^[1-9]\\d{3}/([1-9]|1[0-2])/([1-9]|[1-2][0-9]|3[0-1])$";
public static Long localDate2EpochMilli(LocalDate localDate) {
if (localDate == null) {
return NumberUtils.LONG_ZERO;
}
return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static Long localDateTime2EpochMilli(LocalDateTime localDateTime) {
if (localDateTime == null) {
return NumberUtils.LONG_ZERO;
}
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static Long localDateTime2EpochMilli(Date localDateTime) {
if (localDateTime == null) {
return NumberUtils.LONG_ZERO;
}
return localDateTime.getTime();
}
public static String getFormatYearMonth(LocalDate localDate) {
if (localDate == null) {
return StringUtils.EMPTY;
}
try {
return localDate.format(MONTH_FORMATTER);
} catch (Exception e) {
log.warn("格式化月份错误", e);
return StringUtils.EMPTY;
}
}
public static String getFormatYearMonth(Date localDate) {
if (localDate == null) {
return StringUtils.EMPTY;
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(MONTH_FORMATTER_PATTERN);
return simpleDateFormat.format(localDate);
} catch (Exception e) {
log.warn("格式化月份错误", e);
return StringUtils.EMPTY;
}
}
public static String getFormatLocalDate(LocalDate localDate) {
if (localDate == null) {
return StringUtils.EMPTY;
}
try {
return localDate.format(DATE_FORMATTER);
} catch (Exception e) {
log.warn("格式化日期错误", e);
return StringUtils.EMPTY;
}
}
public static String getFormatLocalDate(LocalDateTime localDateTime) {
if (localDateTime == null) {
return StringUtils.EMPTY;
}
try {
return localDateTime.format(DATE_FORMATTER);
} catch (Exception e) {
log.warn("格式化日期错误", e);
return StringUtils.EMPTY;
}
}
public static String getFormatLocalDateTime(LocalDateTime localDateTime) {
if (localDateTime == null) {
return StringUtils.EMPTY;
}
try {
return localDateTime.format(DATE_TIME_FORMATTER);
} catch (Exception e) {
log.warn("格式化日期错误", e);
return StringUtils.EMPTY;
}
}
public static String getFormatLocalDateTime(Date localDateTime) {
if (localDateTime == null) {
return StringUtils.EMPTY;
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_FORMATTER_PATTERN);
return simpleDateFormat.format(localDateTime);
} catch (Exception e) {
log.warn("格式化日期错误", e);
return StringUtils.EMPTY;
}
}
public static LocalDateTime dateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
public static LocalDate dateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
public static Date localDateToDate(LocalDate localDate) {
if (null == localDate) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
if (null == localDateTime) {
return null;
}
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static String getFormatLocalDate(Date date) {
if (date == null) {
return StringUtils.EMPTY;
}
LocalDateTime localDateTime = dateToLocalDateTime(date);
return getFormatLocalDate(localDateTime);
}
public static YearMonth localDate2YearMonth(Date localDate) {
if (localDate == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(localDate);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
return YearMonth.of(year, month);
}
public static YearMonth String2YearMonth(String localDate) {
if (checkDay(localDate)) {
return null;
}
return YearMonth.parse(localDate);
}
public static LocalDateRange localDate2Range(Date localDate) {
if (localDate == null) {
return null;
}
return LocalDateRange.builder()
.fromDate(getFirstDayDateOfMonth(localDate))
.endDate(getLastDayOfMonth(localDate))
.build();
}
public static LocalDateRange localDate2YearRange(Date localDate) {
if (localDate == null) {
return null;
}
return LocalDateRange.builder()
.fromDate(getFirstDayDateOfYear(localDate))
.endDate(getLastDayOfYear(localDate))
.build();
}
public static Date getFirstDayDateOfMonth(final Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, last);
return cal.getTime();
}
public static Date getLastDayOfMonth(final Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, last);
return cal.getTime();
}
public static Date getFirstDayDateOfYear(final Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int last = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
cal.set(Calendar.DAY_OF_YEAR, last);
return cal.getTime();
}
public static Date getLastDayOfYear(final Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
cal.set(Calendar.DAY_OF_YEAR, last);
return cal.getTime();
}
public static String getMonthBegin(String specifiedDay) {
int year;
int month;
Pattern pattern = Pattern.compile("\\d+-\\d+");
Matcher matcher = pattern.matcher(specifiedDay);
if (StringUtils.isEmpty(specifiedDay) || !matcher.matches()) {
return null;
} else {
year = Integer.parseInt(specifiedDay.split("-")[0]);
month = Integer.parseInt(specifiedDay.split("-")[1]);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date startDate = calendar.getTime();
return sdf.format(startDate);
}
public static String getYearMonth(int yearNum, int monthNum) {
LocalDateTime dateTime = LocalDateTime.now();
int year = dateTime.getYear() + yearNum;
int month = dateTime.getMonthValue() + monthNum;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date startDate = calendar.getTime();
return sdf.format(startDate);
}
/**
*
*
* @param yearMonth
* @return
*/
public static boolean checkYearMonth(String yearMonth) {
return Pattern.matches(MONTH_REGEX, yearMonth);
}
/**
*
*
* @param day
* @return
*/
public static boolean checkDay(String day) {
return Pattern.matches(DAY_REGEX, day) || Pattern.matches(DAY_BAR_REGEX, day);
}
public static Date parse(String date, String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
return format.parse(date);
} catch (ParseException e) {
new BaseBean().writeLog(String.format("日期解析异常: %s, %s", date, pattern));
}
return null;
}
/**
* LocalDateYearMonth
*
* @param localDate
* @return
*/
public static YearMonth toYearMonth(LocalDate localDate) {
Objects.requireNonNull(localDate, "localDate");
return YearMonth.of(localDate.getYear(), localDate.getMonthValue());
}
/**
* YearMonthDate
* dayOfMonth13122928
* 使toDateEndOfMonth(YearMonth)
*
* @param yearMonth
* @param dayOfMonth
* @return
*/
public static Date toDate(YearMonth yearMonth, int dayOfMonth) {
Objects.requireNonNull(yearMonth, "yearMonth");
return localDateToDate(yearMonth.atDay(dayOfMonth));
}
/**
* YearMonthDate
*
* @param yearMonth
* @return
*/
public static Date toDateStartOfMonth(YearMonth yearMonth) {
return toDate(yearMonth, 1);
}
/**
* YearMonthDate
*
* @param yearMonth
* @return
*/
public static Date toDateEndOfMonth(YearMonth yearMonth) {
Objects.requireNonNull(yearMonth, "yearMonth");
return localDateToDate(yearMonth.atEndOfMonth());
}
/**
* YearMonthLocalDate
* dayOfMonth13122928
* 使toLocalDateEndOfMonth(YearMonth)
*
* @param yearMonth
* @param dayOfMonth
* @return
*/
public static LocalDate toLocalDate(YearMonth yearMonth, int dayOfMonth) {
Objects.requireNonNull(yearMonth, "yearMonth");
return yearMonth.atDay(dayOfMonth);
}
/**
* YearMonthLocalDate
*
* @param yearMonth
* @return
*/
public static LocalDate toLocalDateStartOfMonth(YearMonth yearMonth) {
return toLocalDate(yearMonth, 1);
}
/**
* YearMonthLocalDate
*
* @param yearMonth
* @return
*/
public static LocalDate toLocalDateEndOfMonth(YearMonth yearMonth) {
Objects.requireNonNull(yearMonth, "yearMonth");
return yearMonth.atEndOfMonth();
}
/**
* StringDate
*
* @param date
* @return
*/
public static Date stringToDateTime(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date parse = null;
if (date != null) {
try {
parse = sdf.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
return parse;
}
public static Date stringToDate(String date) {
return dateStrToLocalDate(date);
}
//格式化日期
public static String strToDateLong(String strDate) {
Date date = new Date();
try {
date = new SimpleDateFormat("yyyyMMddHHmmss").parse(strDate + "000000");//先按照原格式转换为时间
} catch (ParseException e) {
e.printStackTrace();
}
String str = new SimpleDateFormat("yyyy-MM").format(date);//再将时间转换为对应格式字符串
return str;
}
private static Date dateStrToLocalDate(String date) {
Date localDate = null;
try {
date = date.substring(0, 10);
if (date.contains("/")) {
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
localDate = format.parse(date);
} else if (date.contains("-")) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
localDate = format.parse(date);
}
} catch (Exception e) {
log.error("日期解析异常,{}", date);
localDate = null;
}
return localDate;
}
}

@ -0,0 +1,41 @@
package com.engine.organization.util.excel;
public enum BooleanEnum {
True0("是", Boolean.TRUE),
True1("Y", Boolean.TRUE),
True2("TRUE", Boolean.TRUE),
True3("1", Boolean.TRUE),
True4("YES", Boolean.TRUE),
True5("T", Boolean.TRUE),
False0("否", Boolean.FALSE),
False1("N", Boolean.FALSE),
False2("FALSE", Boolean.FALSE),
False3("0", Boolean.FALSE),
False4("NO", Boolean.FALSE),
False5("F", Boolean.FALSE);
private String name;
private Boolean value;
private BooleanEnum(String name, Boolean value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getValue() {
return value;
}
public void setValue(Boolean value) {
this.value = value;
}
}

@ -0,0 +1,111 @@
package com.engine.organization.util.excel;
/**
*
*/
public class ExcelComment {
int dx1 = 0;
int dy1 = 0;
int dx2 = 0;
int dy2 = 0;
int col1 = 0;
int row1 = 0;
int col2 = 0;
int row2 = 0;
String content;
public int getDx1() {
return dx1;
}
public void setDx1(int dx1) {
this.dx1 = dx1;
}
public int getDy1() {
return dy1;
}
public void setDy1(int dy1) {
this.dy1 = dy1;
}
public int getDx2() {
return dx2;
}
public void setDx2(int dx2) {
this.dx2 = dx2;
}
public int getDy2() {
return dy2;
}
public void setDy2(int dy2) {
this.dy2 = dy2;
}
public int getCol1() {
return col1;
}
public void setCol1(int col1) {
this.col1 = col1;
}
public int getRow1() {
return row1;
}
public void setRow1(int row1) {
this.row1 = row1;
}
public int getCol2() {
return col2;
}
public void setCol2(int col2) {
this.col2 = col2;
}
public int getRow2() {
return row2;
}
public void setRow2(int row2) {
this.row2 = row2;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public ExcelComment(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2, String content) {
this.dx1 = dx1;
this.dy1 = dy1;
this.dx2 = dx2;
this.dy2 = dy2;
this.col1 = col1;
this.row1 = row1;
this.col2 = col2;
this.row2 = row2;
this.content = content;
}
public ExcelComment(int col1, int row1, int col2, int row2, String content) {
this.col1 = col1;
this.row1 = row1;
this.col2 = col2;
this.row2 = row2;
this.content = content;
}
public ExcelComment() {
}
}

@ -0,0 +1,36 @@
package com.engine.organization.util.excel;
import org.apache.commons.lang3.exception.ContextedRuntimeException;
public class ExcelParseException extends ContextedRuntimeException{
private static final long serialVersionUID = -8696742623977630854L;
public ExcelParseException(String message) {
super(message);
this.msgCode = DEFAULT_CODE;
}
public ExcelParseException(String message, Throwable cause) {
super(message, cause);
this.msgCode = DEFAULT_CODE;
}
/**
*
*/
private static final String DEFAULT_CODE = "EXCP0000";
/**
*
*/
private String msgCode;
public String getMsgCode() {
return msgCode;
}
public void setMsgCode(String msgCode) {
this.msgCode = msgCode;
}
}

@ -0,0 +1,265 @@
package com.engine.organization.util.excel;
import com.engine.salary.util.excel.ExcelParseException;
import com.engine.salary.util.excel.ExcelProperty;
import com.engine.salary.util.excel.ExcelSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.exception.ContextedRuntimeException;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import static com.engine.salary.util.excel.ExcelSupport.EXCEL_TYPE_XLSX;
/**
* Excel
**/
public class ExcelParseHelper {
//待校验的行号
private static final int PARSE_EXCEL_ROW_VALID_CELL_INDEX = 0;
//字符开始下标
private static final int CHARACTER_FIRST_INDEX = 0;
/**
* Excel JavaBean
*
* @param file excel
* @param clazz bean
* @param sheetIndex excelsheet0
* @param rowIndex ,0
* @param standardCellNum sheet
* @return
*/
public static <T> List<T> parse2Map(MultipartFile file, Class<T> clazz, int sheetIndex, int rowIndex, int standardCellNum) {
List<List<String>> result = parse2Map(file, sheetIndex, rowIndex, standardCellNum);
List<T> list = new ArrayList<T>();
for (List<String> rowDatas : result) {
T t = setField(clazz, rowDatas);
list.add(t);
}
return list;
}
/**
* Excel JavaBean
*
* @param file excel
* @param clazz bean
* @param sheetIndex excelsheet0
* @param rowIndex ,0
* @param standardCellNum sheet
* @param fileName
* @return
*/
public static <T> List<T> parse2Map(InputStream file, Class<T> clazz, int sheetIndex, int rowIndex, int standardCellNum, String fileName) {
List<List<String>> result = parse2Map(file, sheetIndex, rowIndex, standardCellNum, fileName);
List<T> list = new ArrayList<T>();
for (List<String> rowDatas : result) {
T t = setField(clazz, rowDatas);
list.add(t);
}
return list;
}
/**
* <strong>excel</strong>
*
* @param file
* @param sheetIndex sheet
* @param rowIndex 0
* @return
*/
private static List<List<String>> parse2Map(MultipartFile file, int sheetIndex, int rowIndex, int standardCellNum) {
Sheet sheet = ExcelSupport.parseFile(file, sheetIndex);
int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数
int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数
Validate.isTrue(standardCellNum == cellCount, "Error in excel template! Page %s sheet should have %s column data, existing in %s column , please check the template!", sheetIndex, standardCellNum, cellCount);
List<List<String>> result = new ArrayList<List<String>>();
for (; rowIndex < rowCount; rowIndex++) {
List<String> cellResult = new ArrayList<String>();
for (int j = 0; j < cellCount; j++) {
cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j));
}
result.add(cellResult);
}
return result;
}
/**
* <strong>excel</strong>
*
* @param file
* @param sheetIndex sheet
* @param rowIndex 0
* @return
*/
private static List<List<String>> parse2Map(InputStream file, int sheetIndex, int rowIndex, int standardCellNum, String fileName) {
Sheet sheet = ExcelSupport.parseFile(file, sheetIndex, fileName);
int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数
int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数
Validate.isTrue(standardCellNum == cellCount, "Error in excel template! Page %s sheet should have %s column data, existing in %s column , please check the template!", sheetIndex, standardCellNum, cellCount);
List<List<String>> result = new ArrayList<List<String>>();
for (; rowIndex < rowCount; rowIndex++) {
List<String> cellResult = new ArrayList<String>();
for (int j = 0; j < cellCount; j++) {
cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j));
}
result.add(cellResult);
}
return result;
}
/**
* sheetmap
*
* @param file
* @param sheetIndex sheet
* @param rowIndex
* @return
*/
public static List<Map<String, Object>> parse2Map(InputStream file, int sheetIndex, int rowIndex) {
Sheet sheet = ExcelSupport.parseFile(file, sheetIndex, EXCEL_TYPE_XLSX);
int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数
int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数
List<String> sheetHeader = ExcelSupport.getSheetHeader(sheet, PARSE_EXCEL_ROW_VALID_CELL_INDEX);
List<Map<String, Object>> result = new ArrayList<>();
for (; rowIndex < rowCount; rowIndex++) {
Map<String, Object> cellResult = new HashMap<>();
for (int j = 0; j < cellCount; j++) {
String key = sheetHeader.get(j);
cellResult.put(key, ExcelSupport.getCellValue(sheet, rowIndex, j));
}
result.add(cellResult);
}
return result;
}
/**
* sheetmap
*
* @param rowIndex
* @return
*/
public static List<Map<String, Object>> parse2Map(Sheet sheet, int rowIndex) {
int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数
int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数
List<String> sheetHeader = ExcelSupport.getSheetHeader(sheet, PARSE_EXCEL_ROW_VALID_CELL_INDEX);
List<Map<String, Object>> result = new ArrayList<>();
for (; rowIndex < rowCount; rowIndex++) {
Map<String, Object> cellResult = new HashMap<>();
for (int j = 0; j < cellCount; j++) {
String key = sheetHeader.get(j);
cellResult.put(key, ExcelSupport.getCellValue(sheet, rowIndex, j));
}
result.add(cellResult);
}
return result;
}
/**
* sheetList
*
* @param rowIndex
* @return
*/
public static List<List<String>> parse2List(Sheet sheet, int rowIndex) {
int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数
int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数
List<List<String>> result = new ArrayList<List<String>>();
for (; rowIndex < rowCount; rowIndex++) {
List<String> cellResult = new ArrayList<String>();
for (int j = 0; j < cellCount; j++) {
cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j));
}
result.add(cellResult);
}
return result;
}
/**
*
*
* @param clazz
* @param rowDatas
* @param <T>
* @return
*/
private static <T> T setField(Class<T> clazz, List<String> rowDatas) {
try {
T obj = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
if (excelProperty == null) {
continue;
}
int index = excelProperty.index();
Object value = getFieldValue(field, rowDatas.get(index), excelProperty);
field.setAccessible(true);
field.set(obj, value);
}
return obj;
} catch (Exception e) {
throw new ExcelParseException(e.getMessage(), e);
}
}
/**
* <strong>javaBean</strong>
*
* @param field javaBean
* @param value excel
* @param excelProperty javaBeanexcel
* @return
*/
@SuppressWarnings("rawtypes")
private static Object getFieldValue(Field field, String value, ExcelProperty excelProperty) {
if (StringUtils.isEmpty(value)) {
return null;
}
Object val = null;
try {
Class typeClass = field.getType();
if (typeClass == Integer.class) {
val = Integer.valueOf(value);
} else if (typeClass == Long.class) {
val = Long.valueOf(value);
} else if (typeClass == Float.class) {
val = Float.valueOf(value);
} else if (typeClass == Double.class) {
val = Double.valueOf(value);
} else if (typeClass == Date.class) {
val = ExcelSupport.getDate(value, excelProperty.format());
} else if (typeClass == Short.class) {
val = Short.valueOf(value);
} else if (typeClass == Character.class) {
val = value.charAt(CHARACTER_FIRST_INDEX);
} else if (typeClass == BigDecimal.class) {
val = new BigDecimal(value);
} else {
val = value;
}
} catch (Exception e) {
throw new ContextedRuntimeException(excelProperty.msg(), e);
}
return val;
}
}

@ -0,0 +1,15 @@
package com.engine.organization.util.excel;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelProperty {
public int index(); // 指定 JavaBean 的属性对应 excel 的第几列
public String format() default "yyyy-MM-dd"; // 当 JavaBean 的属性为 Date 类型时,指定 Date 的格式化模式
public String msg() default "解析错误";//当因excel中数据格式错误而造成的解析异常时提示的错误信息
}

@ -0,0 +1,158 @@
package com.engine.organization.util.excel;
import org.apache.commons.lang3.Validate;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import static org.apache.poi.ss.usermodel.CellType.STRING;
public class ExcelSupport {
private static final String pattern = "yyyy-MM-dd HH:mm:ss";
//excel类型
public static final String EXCEL_TYPE_XLSX = "xlsx";
public static final String EXCEL_TYPE_XLS = "xls";
/**
* sheet
*
* @param file
* @param sheetIndex sheet0
* @return sheet
*/
public static Sheet parseFile(MultipartFile file, int sheetIndex) {
Workbook workBook = null;
try (InputStream ins = file.getInputStream();) {
String fileName = file.getOriginalFilename();
if (fileName.endsWith(EXCEL_TYPE_XLSX)) {
workBook = new XSSFWorkbook(ins);
} else if (fileName.endsWith(EXCEL_TYPE_XLS)) {
workBook = new HSSFWorkbook(ins);
} else {
throw new IllegalArgumentException("File format error! Only xlsx and xls types are supported");
}
return workBook.getSheetAt(sheetIndex);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
/**
* sheet
*
* @param sheetIndex sheet0
* @return sheet
*/
public static Sheet parseFile(InputStream ins, int sheetIndex,String fileName) {
Workbook workBook = null;
try {
if (fileName.endsWith(EXCEL_TYPE_XLSX)) {
workBook = new XSSFWorkbook(new BufferedInputStream(ins));
} else if (fileName.endsWith(EXCEL_TYPE_XLS)) {
workBook = new HSSFWorkbook(new BufferedInputStream(ins));
} else {
throw new IllegalArgumentException("File format error! Only xlsx and xls types are supported");
}
return workBook.getSheetAt(sheetIndex);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
/**
* sheet
*
* @param sheet
* @param headerIndex
* @return
*/
public static List<String> getSheetHeader(Sheet sheet, int headerIndex) {
List<String> headers = new ArrayList<>();
Row headerRow = sheet.getRow(headerIndex);
Iterator cellIterator = headerRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = (Cell) cellIterator.next();
String stringCellValue = cell.getStringCellValue();
headers.add(stringCellValue);
}
return headers;
}
/**
*
*
* @param sheet sheet
* @param rowIndex 0
* @param cellIndex 0
* @return
*/
public static String getCellValue(Sheet sheet, int rowIndex, int cellIndex) {
Validate.notNull(sheet.getRow(rowIndex), "Line %s is empty and cannot be resolved", rowIndex);
return getCellValue(sheet.getRow(rowIndex).getCell(cellIndex));
}
/**
*
*/
public static String getCellValue(Cell cell) {
String cellValue = "";
if (cell != null) {
switch (cell.getCellType()) {
case NUMERIC: // 数值类型
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = getDateStr(cell.getDateCellValue(), pattern);
} else {
cell.setCellType(STRING);
cellValue = cell.getStringCellValue();
}
break;
case STRING: // 字符串类型
cellValue = cell.getStringCellValue();
break;
case BOOLEAN: // 布尔类型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case FORMULA: // 公式类型
cellValue = String.valueOf(cell.getCellFormula());
break;
case BLANK: // 空白类型
cellValue = "";
break;
case ERROR:
cellValue = "";
break;
default:
cellValue = cell.toString().trim();
break;
}
}
return cellValue.trim();
}
public static Date getDate(String dateStr, String pattern) {
try {
return new SimpleDateFormat(pattern).parse(dateStr);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
private static String getDateStr(Date date, String pattern) {
return new SimpleDateFormat(pattern).format(date);
}
}

@ -0,0 +1,196 @@
package com.engine.organization.util.excel;
import com.engine.organization.util.OrganizationDateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;
import java.util.Date;
import java.util.List;
public class ExcelUtil {
/**
* excel
*
* @param rowList
* @return
*/
public static XSSFWorkbook genWorkbook(List<List<String>> rowList, String sheetName) {
XSSFWorkbook workbook = new XSSFWorkbook();
// 设置title样式
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
XSSFFont titleFont = workbook.createFont();
titleFont.setFontName("仿宋");
titleFont.setFontHeightInPoints((short) 15);
titleCellStyle.setFont(titleFont);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 设置主体样式
XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);// 设置字体大小
cellStyle.setFont(font);// 选择需要用到的字体格式
cellStyle.setWrapText(true);
XSSFSheet sheet = workbook.createSheet(sheetName);
//自适应宽度
sheet.autoSizeColumn(0, true);
//默认列宽
sheet.setDefaultColumnWidth(40);
//默认行高
sheet.setDefaultRowHeightInPoints(18);
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
List<String> infoList = rowList.get(rowIndex);
XSSFRow row = sheet.createRow(rowIndex);
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
XSSFCell cell = row.createCell(cellIndex);
cell.setCellType(CellType.STRING);
if (rowIndex == 0) {
cell.setCellStyle(titleCellStyle);
} else {
cell.setCellStyle(cellStyle);
}
cell.setCellValue(infoList.get(cellIndex));
// sheet.setColumnWidth(cellIndex, 35 * 256);
}
}
return workbook;
}
public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName) {
XSSFWorkbook workbook = new XSSFWorkbook();
// 设置title样式
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
XSSFFont titleFont = workbook.createFont();
titleFont.setFontName("仿宋");
titleFont.setFontHeightInPoints((short) 15);
titleCellStyle.setFont(titleFont);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 设置主体样式
XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);// 设置字体大小
cellStyle.setFont(font);// 选择需要用到的字体格式
cellStyle.setWrapText(true);
XSSFSheet sheet = workbook.createSheet(sheetName);
//自适应宽度
sheet.autoSizeColumn(0, true);
//默认列宽
sheet.setDefaultColumnWidth(100);
//默认行高
sheet.setDefaultRowHeightInPoints(18);
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
List<Object> infoList = rowList.get(rowIndex);
XSSFRow row = sheet.createRow(rowIndex);
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
XSSFCell cell = row.createCell(cellIndex);
if (rowIndex == 0) {
cell.setCellStyle(titleCellStyle);
} else {
cell.setCellStyle(cellStyle);
}
Object o = infoList.get(cellIndex);
if (o instanceof String) {
cell.setCellType(CellType.STRING);
cell.setCellValue(String.valueOf(o));
} else if (o instanceof Boolean) {
cell.setCellType(CellType.BOOLEAN);
cell.setCellValue(String.valueOf(o));
} else if (o instanceof Date) {
cell.setCellType(CellType.STRING);
cell.setCellValue(OrganizationDateUtil.getFormatLocalDate((Date) o));
} else {
cell.setCellType(CellType.STRING);
cell.setCellValue(o == null ? "" : o.toString());
}
}
}
return workbook;
}
public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName, List<ExcelComment> comments) {
XSSFWorkbook workbook = new XSSFWorkbook();
// 设置title样式
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
XSSFFont titleFont = workbook.createFont();
titleFont.setFontName("仿宋");
titleFont.setFontHeightInPoints((short) 15);
titleCellStyle.setFont(titleFont);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 设置主体样式
XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);// 设置字体大小
cellStyle.setFont(font);// 选择需要用到的字体格式
cellStyle.setWrapText(true);
XSSFSheet sheet = workbook.createSheet(sheetName);
//自适应宽度
sheet.autoSizeColumn(0, true);
//默认列宽
sheet.setDefaultColumnWidth(20);
//默认行高
sheet.setDefaultRowHeightInPoints(18);
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
List<Object> infoList = rowList.get(rowIndex);
XSSFRow row = sheet.createRow(rowIndex);
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
XSSFCell cell = row.createCell(cellIndex);
if (rowIndex == 0) {
cell.setCellStyle(titleCellStyle);
} else {
cell.setCellStyle(cellStyle);
}
Object o = infoList.get(cellIndex);
if (o instanceof String) {
cell.setCellType(CellType.STRING);
cell.setCellValue(String.valueOf(o));
} else if (o instanceof Boolean) {
cell.setCellType(CellType.BOOLEAN);
cell.setCellValue(String.valueOf(o));
} else if (o instanceof Date) {
cell.setCellType(CellType.STRING);
cell.setCellValue(OrganizationDateUtil.getFormatLocalDate((Date) o));
} else {
cell.setCellType(CellType.STRING);
cell.setCellValue(String.valueOf(o));
}
}
}
if (CollectionUtils.isNotEmpty(comments)) {
for (ExcelComment c : comments) {
XSSFDrawing patr = sheet.createDrawingPatriarch();
XSSFComment cellComment = patr.createCellComment(new XSSFClientAnchor(c.dx1, c.dy1, c.dx2, c.dy2, c.col1, c.row1, c.col2, c.row2));
cellComment.setString(c.content);
}
}
return workbook;
}
}

@ -5,7 +5,6 @@ import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.hrmresource.param.HrmResourceSearchParam;
import com.engine.organization.wrapper.ExportCommonWrapper;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.ietf.jgss.GSSContext;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

Loading…
Cancel
Save