创建模板-------放到maven工程-----sec/main/resources/model目录下
第一步:在maven中导入poi 依赖
第二步:controller层
/**
* 体检对账对账--下载表格mecExportExcel(导出excel)
*
* @param flag
* @author lipeng
* @date 2008.10.29
*/
@RequestMapping("/mecExportExcel")
public void mecExportExcel(String flag, HttpServletResponse responses) {
List<Map<String, Object>> list = null;
Map<String, Object> map = new HashMap<>();
if (StringUtils.isNotBlank(flag)) {
map.put("flag", flag);
list = accountService.mecExportExcel(map);
} else {// 查询所有
map.put("flag", "NULL");
list = accountService.mecExportExcel(map);//查询数据源list
}
exportExcelMEC("tjdzz.xls", list, responses);
}
String format3 = name + “_” + CalendarUtil.format3() + “.xlsx”;// CalendarUtil.format3():获取毫秒值用于导出的excel做名称拼接使用,
package com.quhai.service.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
/**
* 获取时间 日 周 月 季度 年度
*
* @author 李鹏 2017年3月12日19:15:36 yyyy:年 MM:月 dd:日 hh:1~12小时制(1-12)
* HH:24小时制(0-23) mm:分 ss:秒 S:毫秒 E:星期几 D:一年中的第几天
* F:一月中的第几个星期(会把这个月总共过的天数除以7) w:一年中的第几个星期 W:一月中的第几星期(会根据实际情况来算) a:上下午标识
* k:和HH差不多,表示一天24小时制(1-24)。 K:和hh差不多,表示一天12小时制(0-11)。 z:表示时区
*/
public class CalendarUtil {
public static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT1 = "yyyy-MM-dd HH:mm";
public static final String FORMAT2 = "yyyy-MM-dd";
public static final String FORMAT3 = "yyyy年MM月dd日 HH时mm分ss秒";
public static final String FORMAT4 = "yyyy年MM月dd日 HH时mm分";
public static final String FORMAT5 = "yyyy年MM月dd日";
/**
* Description: 格式化时间 yyyy-MM-dd HH:mm:ss
*
* @param now
* Date格式时间
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format(Date now) {
return sdfm(now, FORMAT);
}
/**
* Description: 格式化时间 当前时间 yyyy-MM-dd HH:mm:ss
*
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format() {
return sdfm(new Date(), FORMAT);
}
/**
* Description: 格式化时间 yyyy-MM-dd HH:mm
*
* @param now
* Date格式时间
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format1(Date now) {
return sdfm(now, "yyyy-MM-dd HH:mm");
}
/**
* Description: 格式化时间 当前时间 yyyy-MM-dd HH:mm
*
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format1() {
return sdfm(new Date(), "yyyy-MM-dd HH:mm");
}
/**
* Description: 格式化时间 yyyy-MM-dd
*
* @param now
* Date格式时间
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format2(Date now) {
return sdfm(now, "yyyy-MM-dd");
}
/**
* Description: 格式化时间 当前时间yyyy-MM-dd
*
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format2() {
return sdfm(new Date(), "yyyy-MM-dd");
}
/**
* Description: 格式化时间 yyyyMMddHHmmss
*
* @param now
* Date格式时间
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format3(Date now) {
return sdfm(now, "yyyyMMddHHmmss");
}
/**
* Description: 格式化时间 当前时间 yyyyMMddHHmmss
*
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format3() {
return sdfm(new Date(), "yyyyMMddHHmmss");
}
/**
* Description: 格式化时间 yyyyMMdd
*
* @param now
* Date格式时间
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format4(Date now) {
return sdfm(now, "yyyyMMdd");
}
/**
* Description: 格式化时间 当前时间 yyyyMMdd
*
* @return
* @author CY
* @date 2018年9月28日
*/
public static String format4() {
return sdfm(new Date(), "yyyyMMdd");
}
/**
* Description: 格式化时间 当前时间
*
* @param format
* 转格式
* @return
* @author CY
* @date 2018年9月28日
*/
public static String sdfm(String format) {
SimpleDateFormat outFormat = new SimpleDateFormat(format);
return outFormat.format(new Date());
}
/**
* Description: 格式化时间
*
* @param now
* Date格式时间
* @param format
* 转格式
* @return
* @author CY
* @date 2018年9月28日
*/
public static String sdfm(Date now, String format) {
SimpleDateFormat outFormat = new SimpleDateFormat(format);
return outFormat.format(now);
}
/**
* 获取当前时间 Calendar格式
*
* @return
*/
public static Calendar setNowTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
return calendar;
}
/**
* 返回 yyyy-MM-dd HH:mm:ss格式的时间
*
* @return
*/
public static String getNowTime(Calendar calendar) {
return getNowTime(calendar, FORMAT);
}
public static String getNowTime(Calendar calendar, String format) {
SimpleDateFormat dateFormat = null;
if (format == null || "".equals(format)) {
dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");
} else {
dateFormat = new SimpleDateFormat(format);
}
return dateFormat.format(calendar.getTime());
}
/**
* 获取昨天
*/
public static String getYesterday(String format) {
Calendar cal = Calendar.getInstance();
try {
cal.add(Calendar.DATE, -1);
} catch (Exception e) {
e.printStackTrace();
}
return new SimpleDateFormat(StringUtils.isBlank(format) ? "yyyy-MM-dd HH:mm:ss" : format).format(cal.getTime());
}
/**
* 获取当年起始时间
*/
public static String getStartYear() {
return getStartYear(FORMAT);
}
public static String getStartYear(String format) {
Calendar today = setNowTime();
try {
today.set(Calendar.MONTH, 0);
today.set(Calendar.DAY_OF_MONTH, today.getActualMinimum(Calendar.DAY_OF_MONTH));
} catch (Exception e) {
e.printStackTrace();
}
return getNowTime(today,format);
}
/**
* 获取当年结束时间
*/
public static String getEndYear() {
return getEndYear(FORMAT);
}
public static String getEndYear(String format) {
Calendar today = setNowTime();
try {
today.set(Calendar.MONTH, 11);
today.set(Calendar.DAY_OF_MONTH, today.getMaximum(Calendar.DAY_OF_MONTH));
} catch (Exception e) {
e.printStackTrace();
}
return getNowTime(today,format);
}
/**
* 获取当前季度 起始时间
*
* @return
*/
public static String getStartQuarter() {
return getStartQuarter(FORMAT);
}
public static String getStartQuarter(String format) {
Calendar today = setNowTime();
int currentMonth = today.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3) {
today.set(Calendar.MONTH, 0);
} else if (currentMonth >= 4 && currentMonth <= 6) {
today.set(Calendar.MONTH, 3);
} else if (currentMonth >= 7 && currentMonth <= 9) {
today.set(Calendar.MONTH, 4);
} else if (currentMonth >= 10 && currentMonth <= 12) {
today.set(Calendar.MONTH, 9);
}
today.set(Calendar.DATE, 1);
} catch (Exception e) {
e.printStackTrace();
}
return getNowTime(today, format);
}
/**
* 获取当季的结束时间
*/
public static String getEndQuarter() {
return getEndQuarter(FORMAT);
}
public static String getEndQuarter(String format) {
Calendar today = setNowTime();
int currentMonth = today.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3) {
today.set(Calendar.MONTH, 2);
today.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
today.set(Calendar.MONTH, 5);
today.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
today.set(Calendar.MONTH, 8);
today.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
today.set(Calendar.MONTH, 11);
today.set(Calendar.DATE, 31);
}
} catch (Exception e) {
e.printStackTrace();
}
return getNowTime(today, format);
}
/**
* 获得当月起始时间
*
* @return
*/
public static String getStartMounth() {
Calendar today = setNowTime();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
today.set(Calendar.DAY_OF_MONTH, today.getActualMinimum(Calendar.DAY_OF_MONTH));
return getNowTime(today);
}
public static String getStartMounth(String format) {
Calendar today = setNowTime();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
today.set(Calendar.DAY_OF_MONTH, today.getActualMinimum(Calendar.DAY_OF_MONTH));
return new SimpleDateFormat(format).format(today.getTime());
}
/**
* 获得当月结束时间
*
* @return
*/
public static String getEndMounth() {
Calendar endToday = setNowTime();
endToday.set(Calendar.HOUR_OF_DAY, 23);
endToday.set(Calendar.MINUTE, 59);
endToday.set(Calendar.SECOND, 59);
endToday.set(Calendar.MILLISECOND, 59);
endToday.set(Calendar.DAY_OF_MONTH, endToday.getActualMaximum(endToday.DAY_OF_MONTH));
return getNowTime(endToday);
}
public static String getEndMounth(String format) {
Calendar endToday = setNowTime();
endToday.set(Calendar.HOUR_OF_DAY, 23);
endToday.set(Calendar.MINUTE, 59);
endToday.set(Calendar.SECOND, 59);
endToday.set(Calendar.MILLISECOND, 59);
endToday.set(Calendar.DAY_OF_MONTH, endToday.getActualMaximum(endToday.DAY_OF_MONTH));
return new SimpleDateFormat(format).format(endToday.getTime());
}
/**
* 获得当天的起始时间
*
* @return
*/
public static String getStartDate() {
return getStartDate(FORMAT);
}
public static String getStartDate(String format) {
Calendar today = setNowTime();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
return getNowTime(today,format);
}
/**
* 获取当天截止时间
*
* @return
*/
public static String getEndDate() {
return getEndDate(FORMAT);
}
public static String getEndDate(String format) {
Calendar endToday = setNowTime();
endToday.set(Calendar.HOUR_OF_DAY, 23);
endToday.set(Calendar.MINUTE, 59);
endToday.set(Calendar.SECOND, 59);
endToday.set(Calendar.MILLISECOND, 59);
return getNowTime(endToday,format);
}
/**
* 获取当前日期与周一相差的天数
*
* @return
*/
public static int getMondayPlus() {
Calendar day = Calendar.getInstance();
int dayOfWeek = day.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 1) { // 一周中第一天(周日)
return -6;
} else {
return 2 - dayOfWeek;
}
}
/**
* 取得指定日期所在周的第一天
*
* @param date
* @return
*/
public static String getFirstDayOfWeek() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获取星期一开始时间戳
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return simpleDateFormat.format(cal.getTime()) + " 0:0:0";
}
public static String getFirstDayOfWeek(String format) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获取星期一开始时间戳
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return new SimpleDateFormat(format).format(cal.getTime());
}
/**
* 取得指定日期所在周的第一天
*
* @param date
* @return
*/
public static String getEndDayOfWeek() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获取星期日结束时间戳
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return simpleDateFormat.format(cal.getTime()) + " 23:59:59";
}
public static String getEndDayOfWeek(String format) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获取星期日结束时间戳
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return new SimpleDateFormat(format).format(cal.getTime());
}
/**
* 某年第几周的开始时间
*
* @param yearNo
* @param weekNo
* @return
*/
public static String getYearWeekFirstDay(int yearNo, int weekNo) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY); // 设置每周的第一天为星期一
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 每周从周一开始
// 上面两句代码配合,才能实现,每年度的第一个周,是包含第一个星期一的那个周。
cal.setMinimalDaysInFirstWeek(7); // 设置每周最少为7天
cal.set(Calendar.YEAR, yearNo);
cal.set(Calendar.WEEK_OF_YEAR, weekNo - 1);
// 分别取得当前日期的年、月、日
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
}
/**
* 某年第几周的结束时间
*
* @param yearNo
* @param weekNo
* @return
*/
public static String getYearWeekEndDay(int yearNo, int weekNo) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.SUNDAY); // 设置每周的第一天为星期一
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);// 每周从周一开始
// 上面两句代码配合,才能实现,每年度的第一个周,是包含第一个星期一的那个周。
cal.setMinimalDaysInFirstWeek(7); // 设置每周最少为7天
cal.set(Calendar.YEAR, yearNo);
cal.set(Calendar.WEEK_OF_YEAR, weekNo);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
}
/**
* 当前时间
*
* @param format
* 指定返回格式 null 返回标准格式"yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String getToday(String format) {
if (StringUtils.isBlank(format))
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
else
return new SimpleDateFormat(format).format(new Date());
}
/**
* 获取指定时间的当月的最后一天 如果是当月取当前日期
*
* @param month
* 指定时间
* @param format
* 指定返回格式 null 返回标准格式"yyyy-MM-dd HH:mm:ss"
* @return
* @throws ParseException
*/
public static String getEndDayOfMonth(String time, String format) {
Calendar cal = Calendar.getInstance();
try {
Date parse = new SimpleDateFormat("yyyyMM").parse(time.replaceAll("-", "").substring(0, 6));
cal.setTime(parse);
cal.roll(cal.DATE, -1);
} catch (ParseException e) {
e.printStackTrace();
}
Date sj = cal.getTime().getTime() >= new Date().getTime() ? new Date() : cal.getTime();
if (StringUtils.isBlank(format))
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sj);
else
return new SimpleDateFormat(format).format(sj);
}
/**
* 获取指定时间的当月的最后一天
*
* @param month
* 指定时间
* @param format
* 指定返回格式 null 返回标准格式"yyyy-MM-dd HH:mm:ss"
* @return
* @throws ParseException
*/
public static String getEndDayOfMonthByTime(String time, String format) {
Calendar cal = Calendar.getInstance();
try {
Date parse = new SimpleDateFormat("yyyyMM").parse(time.replaceAll("-", "").substring(0, 6));
cal.setTime(parse);
cal.roll(cal.DATE, -1);
} catch (ParseException e) {
e.printStackTrace();
}
Date sj = cal.getTime();
if (StringUtils.isBlank(format))
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sj);
else
return new SimpleDateFormat(format).format(sj);
}
/**
* 获取日期的指定字段值
*
* @param element
* @param time
* @return
*/
public static String getElement(String element, String time) {
Calendar cal = Calendar.getInstance();
try {
if (StringUtils.isNotBlank(time)) {
Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
cal.setTime(parse);
}
} catch (ParseException e) {
e.printStackTrace();
}
if ("YEAR".equalsIgnoreCase(element)) {
return cal.get(Calendar.YEAR) + "";
}
if ("MONTH".equalsIgnoreCase(element)) {
return cal.get(Calendar.MONTH) + 1 + "";
}
if ("DATE".equalsIgnoreCase(element)) {
return cal.get(Calendar.DATE) + "";
}
if ("H".equalsIgnoreCase(element)) {
return cal.get(Calendar.HOUR_OF_DAY) + "";
}
if ("M".equalsIgnoreCase(element)) {
return cal.get(Calendar.MINUTE) + "";
}
if ("S".equalsIgnoreCase(element)) {
return cal.get(Calendar.SECOND) + "";
}
return null;
}
/**
* 把日期往后增加指定时间
*
* @param element
* year month date week 必填
* @param time
* 指定时间 默认当前时间
* @param num
* 指定增加数量 默认+1
* @param format
* 指定返回格式 null 返回标准格式"yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String addTime(String element, String time, int num, String format) {
return addTime(element, time, num, format, false);
}
public static String addTime(String element, String time, int num, String format, boolean fushu) {
Date date = null;// 取时间
if (StringUtils.isNotBlank(time)) {
try {
date = new SimpleDateFormat(StringUtils.isBlank(format) ? FORMAT : format).parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
} else {
date = new Date();
}
if (fushu && num <= 0) {
num = 1;
}
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
if ("YEAR".equalsIgnoreCase(element))
calendar.add(calendar.YEAR, num);// 把日期往后增加一年.整数往后推,负数往前移动
if ("MONTH".equalsIgnoreCase(element))
calendar.add(calendar.MONTH, num);// 把日期往后增加一个月.整数往后推,负数往前移动
if ("DATE".equalsIgnoreCase(element))
calendar.add(calendar.DATE, num);// 把日期往后增加一天.整数往后推,负数往前移动
if ("WEEK".equalsIgnoreCase(element))
calendar.add(calendar.WEEK_OF_MONTH, num);// 把日期往后增加1周.整数往后推,负数往前移动
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
return new SimpleDateFormat(StringUtils.isBlank(format) ? FORMAT : format).format(date);
}
/**
* Title: getAgeByBirth Description: 计算年龄 真实年龄
*
* @param day
* @return
* @author CY
* @date 2018年10月25日
*/
public static int getAgeByBirth(String day) {
int age = 0;
try {
Date birthday = new SimpleDateFormat(FORMAT2).parse(day);
Calendar now = Calendar.getInstance();
now.setTime(new Date());
// 当前时间
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
if (birth.after(now)) {
// 如果传入的时间,在当前时间的后面,返回0岁
age = 0;
} else {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
age += 1;
}
}
return age;
} catch (Exception e) {
// 兼容性更强,异常后返回数据
return 0;
}
}
/**
* Description: before比较两个时间的日期顺序 date1在前(小于)date2时返回true 其他返回false
*
* @param date1
* 时间
* @param date2
* 时间
* @param format
* 时间格式 默认 yyyy-MM-dd
* @return
* @author CY
* @date 2018年11月14日
* @version 1.1
*/
public static boolean before(String date1, String date2, String format) {
try {
format = format != null ? format : FORMAT2;
SimpleDateFormat sdfm = new SimpleDateFormat(format);
return sdfm.parse(date1).before(sdfm.parse(date2));
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
/**
* Description: before比较两个时间的日期顺序 date1在后(大于)date2时返回true 其他返回false
*
* @param date1
* 时间
* @param date2
* 时间
* @param format
* 时间格式 默认 yyyy-MM-dd
* @return
* @author CY
* @date 2018年11月14日
* @version 1.1
*/
public static boolean after(String date1, String date2, String format) {
try {
format = format != null ? format : FORMAT2;
SimpleDateFormat sdfm = new SimpleDateFormat(format);
return sdfm.parse(date1).after(sdfm.parse(date2));
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
/**
* Description: 是否是日期格式
*
* @param str
* @return
* @author CY
* @date 2018年12月17日
* @version 1.1
*/
public static boolean isValidDate(String str) {
return isValidDate(str, FORMAT);
}
public static boolean isValidDate(String str, String format) {
boolean convertSuccess = true;
SimpleDateFormat format1 = new SimpleDateFormat(format);
try {
format1.setLenient(false);
format1.parse(str);
} catch (ParseException e) {
convertSuccess = false;
}
return convertSuccess;
}
//
/**
* Description: 格式化时间yyyyMM yyyy-MM-dd 格式化 yyyyMM
*
* @param str
* @return
* @author lipeng
* @date 2011年01月09日
* @version 1.1
*/
public static String getdateval(String type) {
String yyyyMM = "";
Date date = null;
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMM");
try {
date = format1.parse(type);
yyyyMM = format2.format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// format1.format(date)
return yyyyMM;
}
/**
*
* 1 第一季度 2 第二季度 3 第三季度 4 第四季度
*
* @param date
* @return
* @author lipeng
* @date 2011年01月09日
*/
public static int getSeason(Date date) {
int season = 0;
Calendar c = Calendar.getInstance();
c.setTime(date);
int month = c.get(Calendar.MONTH);
switch (month) {
case Calendar.JANUARY:
case Calendar.FEBRUARY:
case Calendar.MARCH:
season = 1;
break;
case Calendar.APRIL:
case Calendar.MAY:
case Calendar.JUNE:
season = 2;
break;
case Calendar.JULY:
case Calendar.AUGUST:
case Calendar.SEPTEMBER:
season = 3;
break;
case Calendar.OCTOBER:
case Calendar.NOVEMBER:
case Calendar.DECEMBER:
season = 4;
break;
default:
break;
}
return season;
}
public static void main(String[] args) throws Exception {
}
}
//导出excel具体方法
//封装excel
public void exportExcelMEC(String path, List<Map<String, Object>> list, HttpServletResponse responses) {
OutputStream out = null;
String name="趣嗨聚会优惠码详单";
String format3 = name + "" + CalendarUtil.format3() + ".xlsx";
//获取模板在容器中的绝对路径
String filePath = OrderController.class.getClassLoader().getResource("model" + "/" + path).getPath();
Workbook workbook;
try {
workbook = WorkbookFactory.create(new FileInputStream(new File(filePath)));
Sheet sheet = workbook.getSheetAt(0);
Row rowCellStyle = sheet.getRow(1);
XSSFCellStyle columnOne = (XSSFCellStyle) rowCellStyle.getCell(0).getCellStyle();//获取模板第1列单元格样式(下标从0开始)
XSSFCellStyle columnOne1 = (XSSFCellStyle) rowCellStyle.getCell(1).getCellStyle();//获取模板第2列单元格样式(下标从0开始)
XSSFCellStyle columnOne2 = (XSSFCellStyle) rowCellStyle.getCell(2).getCellStyle();//获取模板第3列单元格样式(下标从0开始)
XSSFCellStyle columnOne3 = (XSSFCellStyle) rowCellStyle.getCell(3).getCellStyle();//获取模板第4列单元格样式(下标从0开始)
XSSFCellStyle columnOne4 = (XSSFCellStyle) rowCellStyle.getCell(4).getCellStyle();//获取模板第5列单元格样式(下标从0开始)
XSSFCellStyle columnOne5 = (XSSFCellStyle) rowCellStyle.getCell(5).getCellStyle();//获取模板第6列单元格样式(下标从0开始)
XSSFCellStyle columnOne6 = (XSSFCellStyle) rowCellStyle.getCell(6).getCellStyle();//获取模板第7列单元格样式(下标从0开始)
int num = 0;
for (int i = 0; i < list.size(); i++) {
XSSFRow dataRow = (XSSFRow) sheet.createRow(sheet.getLastRowNum() + 1);
Map<String, Object> map = list.get(i);
XSSFCell cell0 = dataRow.createCell(0);
cell0.setCellValue(Integer.toString(num += 1));// 序号
cell0.setCellStyle(columnOne);// 填充样式
XSSFCell cell1=dataRow.createCell(1);
String couponCode = (String) map.get("couponCode");
if (StringUtils.isNotBlank(couponCode)) {
cell1.setCellValue(couponCode);
cell1.setCellStyle(columnOne1);// 填充样式
} else {
cell1.setCellValue("");
cell1.setCellStyle(columnOne1);// 填充样式
}
//
XSSFCell cell2=dataRow.createCell(2);
String baoming = (String) map.get("baoMing").toString();
//
if (StringUtils.isNotBlank(baoming)) {
cell2.setCellValue(baoming);
cell2.setCellStyle(columnOne2);// 填充样式
} else {
cell2.setCellValue("");
cell2.setCellStyle(columnOne2);// 填充样式
}
//
XSSFCell cell3=dataRow.createCell(3);
String ruChang = map.get("ruChang").toString();
if (StringUtils.isNotBlank(ruChang)) {
cell3.setCellValue(ruChang);
cell3.setCellStyle(columnOne3);// 填充样式
} else {
cell3.setCellValue("");
cell3.setCellStyle(columnOne3);// 填充样式
}
XSSFCell cell4=dataRow.createCell(4);
String nickname = (String) map.get("nickname");
if (StringUtils.isNotBlank(nickname)) {
cell4.setCellValue(nickname);
cell4.setCellStyle(columnOne4);// 填充样式
} else {
cell4.setCellValue("");
cell4.setCellStyle(columnOne4);// 填充样式
}
XSSFCell cell5=dataRow.createCell(5);
String appTime = (String) map.get("baoMingDate").toString();
if (StringUtils.isNotBlank(appTime)) {
cell5.setCellValue(appTime);
cell5.setCellStyle(columnOne5);// 填充样式
} else {
cell5.setCellValue("");
cell5.setCellStyle(columnOne5);// 填充样式
}
XSSFCell cell6=dataRow.createCell(6);
String usedTime = (String) map.get("ruChangTime").toString();
if (StringUtils.isNotBlank(appTime)) {
cell6.setCellValue(usedTime);
cell6.setCellStyle(columnOne6);// 填充样式
} else {
cell6.setCellValue("");
cell6.setCellStyle(columnOne6);// 填充样式
}
}
//*************
//***************************
responses.reset();
responses.setContentType("application/msexcel");
responses.setCharacterEncoding("UTF-8");
responses.setHeader("Content-Disposition",
"attchment;filename=" + new String((format3.getBytes()), "iso-8859-1"));
out = responses.getOutputStream();
workbook.write(out);
out.flush();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}