public class DateUtils extends org.apache.commons.lang3.time.DateUtils{
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
public static String getDate() {
return getDate("yyyy-MM-dd");
}
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
public static String formatDate(Date date, Object... pattern) {
if (date == null) {
return null;
}
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
public static String formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
public static String getMonth() {
return formatDate(new Date(), "MM");
}
public static String getDay() {
return formatDate(new Date(), "dd");
}
public static String getWeek() {
return formatDate(new Date(), "E");
}
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
public static long pastDays(Date date) {
long t = System.currentTimeMillis() - date.getTime();
return t / (24 * 60 * 60 * 1000);
}
public static long pastHour(Date date) {
long t = System.currentTimeMillis() - date.getTime();
return t / (60 * 60 * 1000);
}
public static long pastMinutes(Date date) {
long t = System.currentTimeMillis() - date.getTime();
return t / (60 * 1000);
}
public static String formatDateTime(long timeMillis) {
long day = timeMillis / (24 * 60 * 60 * 1000);
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
}
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
public static String getFirstDayOfMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取当前月第一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
String first = format.format(c.getTime());
return first;
}
public static String getNextfMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 1);
String first = format.format(c.getTime());
return first;
}
public static String getForwardMonth(int n) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, n);
String month = format.format(c.getTime());
return month;
}
public class EasyExcelUtil {
private static final Logger logger = LoggerFactory.getLogger(EasyExcelUtil.class);
public final static String EXCEL_EXPORT_SUFFIX = ".xlsx";
private Class clazz;
public EasyExcelUtil(Class clazz) {
this.clazz = clazz;
}
public static void main(String[] args) {
EasyExcelUtil util = new EasyExcelUtil(ExportTest.class);
List dataList = new ArrayList<>();
long currentTimeMillis = System.currentTimeMillis();
System.out.println();
for (int i = 0; i < 20; i++) {
dataList.add(new ExportTest("1111", null, 1, 11));
}
//util.exportToExcel(null, null, "插入耗时测试", dataList, "sheet", "我是表头,我很长。。。。。000000000" , null);
util.exportToExcel(null, null, "插入耗时测试", dataList, "sheet", "我 问我飒飒大苏打撒旦撒旦撒阿" +"\"+
"三大苏打实打实阿斯顿撒旦", null);
long timeMillis = System.currentTimeMillis();
System.out.println("插入耗时" + (timeMillis - currentTimeMillis) / 1000 + "秒");
}
public Boolean exportToExcel(HttpServletRequest request, HttpServletResponse response, String fileName, List dataList, String sheetName, String title, List headers) {
List> lists = new ArrayList>();
for (Object obj : dataList) {
List
public class ExcelHeightColWidthStyleStrategy extends AbstractRowWriteHandler {
public static final String KEY_SHEET_NAME = "sheetName";
public static final String KEY_ROW_INDEX = "rowIndex";
public static final String KEY_COL_INDEX = "colIndex";
public static final String KEY_ROW_HEIGHT = "rowHeight";
public static final String KEY_COL_WIDTH = "colWidth";
private List sheetNameList;
private List
public class ExcelWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
private static final int MAX_COLUMN_WIDTH = 50;
private final Map> CACHE = new HashMap>(8);
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List cellDataList, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead) {
if (CollectionUtils.isEmpty(cellDataList))
return;
if (!isHead) {
return;
}
Map maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
if (maxColumnWidthMap == null) {
maxColumnWidthMap = new HashMap(10);
CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
}
Integer columnWidth = dataLength(cellDataList, cell, isHead);
if (columnWidth < 0) {
return;
}
if (columnWidth > MAX_COLUMN_WIDTH) {
columnWidth = MAX_COLUMN_WIDTH;
}
Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 100);
}
}
private Integer dataLength(List cellDataList, Cell cell, Boolean isHead) {
if (isHead) {
return cell.getStringCellValue().getBytes().length;
}
CellData cellData = cellDataList.get(0);
CellDataTypeEnum type = cellData.getType();
if (type == null) {
return -1;
}
switch (type) {
case STRING:
return cellData.getStringValue().getBytes().length;
case BOOLEAN:
return cellData.getBooleanValue().toString().getBytes().length;
case NUMBER:
return cellData.getNumberValue().toString().getBytes().length;
default:
return -1;
}
}
}