From 783b9fc6bf2a5e9227d015de2f632b11729f4584 Mon Sep 17 00:00:00 2001 From: Chengliang <1546584672@qq.com> Date: Wed, 29 Jun 2022 14:18:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=BA=E5=91=98=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/resource/ResourceMapper.java | 3 +- .../mapper/resource/ResourceMapper.xml | 33 +- .../service/impl/ExportCommonServiceImpl.java | 29 +- .../service/impl/HrmResourceServiceImpl.java | 4 +- .../util/OrganizationDateUtil.java | 460 ++++++++++++++++++ .../organization/util/excel/BooleanEnum.java | 41 ++ .../organization/util/excel/ExcelComment.java | 111 +++++ .../util/excel/ExcelParseException.java | 36 ++ .../util/excel/ExcelParseHelper.java | 265 ++++++++++ .../util/excel/ExcelProperty.java | 15 + .../organization/util/excel/ExcelSupport.java | 158 ++++++ .../organization/util/excel/ExcelUtil.java | 196 ++++++++ .../web/ExportCommonController.java | 1 - 13 files changed, 1318 insertions(+), 34 deletions(-) create mode 100644 src/com/engine/organization/util/OrganizationDateUtil.java create mode 100644 src/com/engine/organization/util/excel/BooleanEnum.java create mode 100644 src/com/engine/organization/util/excel/ExcelComment.java create mode 100644 src/com/engine/organization/util/excel/ExcelParseException.java create mode 100644 src/com/engine/organization/util/excel/ExcelParseHelper.java create mode 100644 src/com/engine/organization/util/excel/ExcelProperty.java create mode 100644 src/com/engine/organization/util/excel/ExcelSupport.java create mode 100644 src/com/engine/organization/util/excel/ExcelUtil.java diff --git a/src/com/engine/organization/mapper/resource/ResourceMapper.java b/src/com/engine/organization/mapper/resource/ResourceMapper.java index 7c09c1cd..4e5dc451 100644 --- a/src/com/engine/organization/mapper/resource/ResourceMapper.java +++ b/src/com/engine/organization/mapper/resource/ResourceMapper.java @@ -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 listAll(@Param("param")HrmResourceSearchParam param); + List listAll(@Param("param")HrmResourceSearchParam param); } diff --git a/src/com/engine/organization/mapper/resource/ResourceMapper.xml b/src/com/engine/organization/mapper/resource/ResourceMapper.xml index 0e452b87..00973e8b 100644 --- a/src/com/engine/organization/mapper/resource/ResourceMapper.xml +++ b/src/com/engine/organization/mapper/resource/ResourceMapper.xml @@ -9,49 +9,52 @@ id,last_name,department_id,company_id,mobile,telephone,manager_id - + 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 - and department_id = #{param.departmentId} + and t.department_id = #{param.departmentId} - and company_id = #{param.companyId} + and t.company_id = #{param.companyId} - and mobile = #{param.mobile} + and t.mobile = #{param.mobile} - and telephone = #{param.telephone} + and t.telephone = #{param.telephone} - and manager_id = #{param.managerId} + and t.manager_id = #{param.managerId} - and mobile_call = #{param.mobileCall} + and t.mobile_call = #{param.mobileCall} - and job_title = #{param.jobTitle} + and t.job_title = #{param.jobTitle} - order by id asc; + order by t.id asc; - AND last_name like CONCAT('%',#{param.lastName},'%') + AND t.last_name like CONCAT('%',#{param.lastName},'%') - AND last_name like '%'||#{param.lastName}||'%' + AND t.last_name like '%'||#{param.lastName}||'%' - AND last_name like '%'+#{param.lastName}+'%' + AND t.last_name like '%'+#{param.lastName}+'%' diff --git a/src/com/engine/organization/service/impl/ExportCommonServiceImpl.java b/src/com/engine/organization/service/impl/ExportCommonServiceImpl.java index 3ffa5401..c03646c9 100644 --- a/src/com/engine/organization/service/impl/ExportCommonServiceImpl.java +++ b/src/com/engine/organization/service/impl/ExportCommonServiceImpl.java @@ -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 hrmResourcePOS = MapperProxyFactory.getProxy(ResourceMapper.class).listAll(param); - if (hrmResourcePOS == null) { - hrmResourcePOS = new ArrayList<>(); + List 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> rows = new LinkedList<>(); - for (HrmResourcePO po : hrmResourcePOS) { + for (HrmResourceVO vo : hrmResourceVOS) { List 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); diff --git a/src/com/engine/organization/service/impl/HrmResourceServiceImpl.java b/src/com/engine/organization/service/impl/HrmResourceServiceImpl.java index 49a020f8..5586877b 100644 --- a/src/com/engine/organization/service/impl/HrmResourceServiceImpl.java +++ b/src/com/engine/organization/service/impl/HrmResourceServiceImpl.java @@ -235,11 +235,11 @@ public class HrmResourceServiceImpl extends Service implements HrmResourceServic ArrayList 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; diff --git a/src/com/engine/organization/util/OrganizationDateUtil.java b/src/com/engine/organization/util/OrganizationDateUtil.java new file mode 100644 index 00000000..2f0f9991 --- /dev/null +++ b/src/com/engine/organization/util/OrganizationDateUtil.java @@ -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; + } + + + /** + * LocalDate转YearMonth + * + * @param localDate + * @return + */ + public static YearMonth toYearMonth(LocalDate localDate) { + Objects.requireNonNull(localDate, "localDate"); + return YearMonth.of(localDate.getYear(), localDate.getMonthValue()); + } + + + /** + * YearMonth转Date + * 注意dayOfMonth范围:1到31之间,最大值根据月份确定特殊情况,如2月闰年29,非闰年28 + * 如果要转换为当月最后一天,可以使用下面方法:toDateEndOfMonth(YearMonth) + * + * @param yearMonth + * @param dayOfMonth + * @return + */ + public static Date toDate(YearMonth yearMonth, int dayOfMonth) { + Objects.requireNonNull(yearMonth, "yearMonth"); + return localDateToDate(yearMonth.atDay(dayOfMonth)); + } + + /** + * YearMonth转Date,转换为当月第一天 + * + * @param yearMonth + * @return + */ + public static Date toDateStartOfMonth(YearMonth yearMonth) { + return toDate(yearMonth, 1); + } + + /** + * YearMonth转Date,转换为当月最后一天 + * + * @param yearMonth + * @return + */ + public static Date toDateEndOfMonth(YearMonth yearMonth) { + Objects.requireNonNull(yearMonth, "yearMonth"); + return localDateToDate(yearMonth.atEndOfMonth()); + } + + + /** + * YearMonth转LocalDate + * 注意dayOfMonth范围:1到31之间,最大值根据月份确定特殊情况,如2月闰年29,非闰年28 + * 如果要转换为当月最后一天,可以使用下面方法:toLocalDateEndOfMonth(YearMonth) + * + * @param yearMonth + * @param dayOfMonth + * @return + */ + public static LocalDate toLocalDate(YearMonth yearMonth, int dayOfMonth) { + Objects.requireNonNull(yearMonth, "yearMonth"); + return yearMonth.atDay(dayOfMonth); + } + + /** + * YearMonth转LocalDate,转换为当月第一天 + * + * @param yearMonth + * @return + */ + public static LocalDate toLocalDateStartOfMonth(YearMonth yearMonth) { + return toLocalDate(yearMonth, 1); + } + + /** + * YearMonth转LocalDate,转换为当月最后一天 + * + * @param yearMonth + * @return + */ + public static LocalDate toLocalDateEndOfMonth(YearMonth yearMonth) { + Objects.requireNonNull(yearMonth, "yearMonth"); + return yearMonth.atEndOfMonth(); + } + + /** + * String转Date + * + * @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; + } +} + + diff --git a/src/com/engine/organization/util/excel/BooleanEnum.java b/src/com/engine/organization/util/excel/BooleanEnum.java new file mode 100644 index 00000000..7a795f1b --- /dev/null +++ b/src/com/engine/organization/util/excel/BooleanEnum.java @@ -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; + } +} diff --git a/src/com/engine/organization/util/excel/ExcelComment.java b/src/com/engine/organization/util/excel/ExcelComment.java new file mode 100644 index 00000000..3d0669b6 --- /dev/null +++ b/src/com/engine/organization/util/excel/ExcelComment.java @@ -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() { + } +} diff --git a/src/com/engine/organization/util/excel/ExcelParseException.java b/src/com/engine/organization/util/excel/ExcelParseException.java new file mode 100644 index 00000000..7a246aa9 --- /dev/null +++ b/src/com/engine/organization/util/excel/ExcelParseException.java @@ -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; + } + +} diff --git a/src/com/engine/organization/util/excel/ExcelParseHelper.java b/src/com/engine/organization/util/excel/ExcelParseHelper.java new file mode 100644 index 00000000..9c7f2d57 --- /dev/null +++ b/src/com/engine/organization/util/excel/ExcelParseHelper.java @@ -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 excel中第几个sheet,从0开始 + * @param rowIndex 从第几行开始解析,第一行是0 + * @param standardCellNum 模板验证,该sheet应有多少列 + * @return + */ + public static List parse2Map(MultipartFile file, Class clazz, int sheetIndex, int rowIndex, int standardCellNum) { + List> result = parse2Map(file, sheetIndex, rowIndex, standardCellNum); + List list = new ArrayList(); + for (List rowDatas : result) { + T t = setField(clazz, rowDatas); + list.add(t); + } + return list; + } + + /** + * 将 Excel 解析为 JavaBean 对象 + * + * @param file excel文件 + * @param clazz 解析bean的类 + * @param sheetIndex excel中第几个sheet,从0开始 + * @param rowIndex 从第几行开始解析,第一行是0 + * @param standardCellNum 模板验证,该sheet应有多少列 + * @param fileName 文件名 + * @return + */ + public static List parse2Map(InputStream file, Class clazz, int sheetIndex, int rowIndex, int standardCellNum, String fileName) { + List> result = parse2Map(file, sheetIndex, rowIndex, standardCellNum, fileName); + List list = new ArrayList(); + for (List rowDatas : result) { + T t = setField(clazz, rowDatas); + list.add(t); + } + return list; + } + + /** + * 获取excel数据。 + * + * @param file 文件 + * @param sheetIndex 解析第几个sheet + * @param rowIndex 从第几行开始解析,第一行为 0,依次类推 + * @return 二维数据集合 + */ + private static List> 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> result = new ArrayList>(); + for (; rowIndex < rowCount; rowIndex++) { + List cellResult = new ArrayList(); + for (int j = 0; j < cellCount; j++) { + cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j)); + } + result.add(cellResult); + } + return result; + } + + /** + * 获取excel数据。 + * + * @param file 文件 + * @param sheetIndex 解析第几个sheet + * @param rowIndex 从第几行开始解析,第一行为 0,依次类推 + * @return 二维数据集合 + */ + private static List> 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> result = new ArrayList>(); + for (; rowIndex < rowCount; rowIndex++) { + List cellResult = new ArrayList(); + for (int j = 0; j < cellCount; j++) { + cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j)); + } + result.add(cellResult); + } + return result; + } + + + /** + * 将sheet数据转为map + * + * @param file + * @param sheetIndex sheet下标 + * @param rowIndex 从哪行开始解析 + * @return + */ + public static List> 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 sheetHeader = ExcelSupport.getSheetHeader(sheet, PARSE_EXCEL_ROW_VALID_CELL_INDEX); + + List> result = new ArrayList<>(); + for (; rowIndex < rowCount; rowIndex++) { + Map 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; + } + + + /** + * 将sheet数据转为map + * + * @param rowIndex 从哪行开始解析 + * @return + */ + public static List> parse2Map(Sheet sheet, int rowIndex) { + int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 + int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数 + + List sheetHeader = ExcelSupport.getSheetHeader(sheet, PARSE_EXCEL_ROW_VALID_CELL_INDEX); + + List> result = new ArrayList<>(); + for (; rowIndex < rowCount; rowIndex++) { + Map 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; + } + + /** + * 将sheet数据转为List + * + * @param rowIndex 从哪行开始解析 + * @return + */ + public static List> parse2List(Sheet sheet, int rowIndex) { + int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 + int cellCount = sheet.getRow(PARSE_EXCEL_ROW_VALID_CELL_INDEX).getPhysicalNumberOfCells(); // 总列数 + + List> result = new ArrayList>(); + for (; rowIndex < rowCount; rowIndex++) { + List cellResult = new ArrayList(); + for (int j = 0; j < cellCount; j++) { + cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j)); + } + result.add(cellResult); + } + return result; + } + + + /** + * 为对象的每一个属性赋值 + * + * @param clazz + * @param rowDatas + * @param + * @return + */ + private static T setField(Class clazz, List 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); + } + } + + /** + * 获取javaBean属性值 + * + * @param field javaBean的对象属性 + * @param value excel中对应的值 + * @param excelProperty javaBean中解析excel注解,包含日期格式、错误提示信息 + * @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; + } +} diff --git a/src/com/engine/organization/util/excel/ExcelProperty.java b/src/com/engine/organization/util/excel/ExcelProperty.java new file mode 100644 index 00000000..eccf1396 --- /dev/null +++ b/src/com/engine/organization/util/excel/ExcelProperty.java @@ -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中数据格式错误而造成的解析异常时,提示的错误信息 +} diff --git a/src/com/engine/organization/util/excel/ExcelSupport.java b/src/com/engine/organization/util/excel/ExcelSupport.java new file mode 100644 index 00000000..2d307995 --- /dev/null +++ b/src/com/engine/organization/util/excel/ExcelSupport.java @@ -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 sheet下标,从0开始 + * @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 sheet下标,从0开始 + * @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 getSheetHeader(Sheet sheet, int headerIndex) { + List 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); + } +} diff --git a/src/com/engine/organization/util/excel/ExcelUtil.java b/src/com/engine/organization/util/excel/ExcelUtil.java new file mode 100644 index 00000000..2911cb7d --- /dev/null +++ b/src/com/engine/organization/util/excel/ExcelUtil.java @@ -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> 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 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> 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 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> rowList, String sheetName, List 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 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; + } +} diff --git a/src/com/engine/organization/web/ExportCommonController.java b/src/com/engine/organization/web/ExportCommonController.java index a34f49a7..adaed4ab 100644 --- a/src/com/engine/organization/web/ExportCommonController.java +++ b/src/com/engine/organization/web/ExportCommonController.java @@ -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;