使用POI获取时间单元格的格式化后的值
- 1、一些常量
- 2、时间格式化器
- 3、 获取格式化后时间字符串的方法
注意:
- 个人认为,解析时间单元格的值直接返回java.util.Date是比较好的一种方法。因为从下面的格式化方法中可以看到,好多日期格式其实是我们在java程序或是数据库中很少使用的,所以没有必要发大功夫把时间单元格的值格式化成对应的字符串返回。
- 常量中定义的一些值的集合是Cell.getCellStyle().getDataFormat()和Cell.getCellStyle().getDataFormatString()方法返回的值,这个读者可以自己编写代码测一测,这里不保证已经包含全部的可能值(漏掉值的可能性不大)。
- 因为时间格式单元格和数值单元格的Cell.getCellStyle()方法返回的都是NUMERIC,所以要获取Cell.getCellStyle().getDataFormat()或Cell.getCellStyle().getDataFormatString()进行格式转换。
- 使用DateUtil.isValidExcelDate(value)可以判断单元格的值是否可以转换成时间类型,切记该方法不能识别单元格的值是时间还是数值,他只是判断这个数值是否符合时间毫秒值(我是这样理解的)。
- 使用DateUtil.getJavaDate(value)可以将double类型的时间值转化成java.util.Date
1、一些常量
package com.dsanjun.poi.constant;
import java.util.Arrays;
import java.util.List;
public interface Constants {
String COMMON_DATE_FORMAT_XQ = "星期";
String COMMON_DATE_FORMAT_Z = "周";
List<Short> EXCEL_FORMAT_INDEX_07_TIME = Arrays.asList(new Short[] { 18, 19, 20, 21, 32, 33, 45, 46, 47, 55, 56,
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186 });
List<Short> EXCEL_FORMAT_INDEX_07_DATE = Arrays.asList(new Short[] { 14, 15, 16, 17, 22, 30, 31, 57, 58, 187, 188,
189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208 });
List<Short> EXCEL_FORMAT_INDEX_03_TIME = Arrays.asList(new Short[] { 18, 19, 20, 21, 32, 33, 45, 46, 47, 55, 56,
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186 });
List<Short> EXCEL_FORMAT_INDEX_03_DATE = Arrays.asList(new Short[] { 14, 15, 16, 17, 22, 30, 31, 57, 58, 187, 188,
189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208 });
List<String> EXCEL_FORMAT_INDEX_DATE_NYRSFM_STRING = Arrays.asList("yyyy/m/d\\ h:mm;@", "m/d/yy h:mm",
"yyyy/m/d\\ h:mm\\ AM/PM", "[$-409]yyyy/m/d\\ h:mm\\ AM/PM;@", "yyyy/mm/dd\\ hh:mm:dd",
"yyyy/mm/dd\\ hh:mm", "yyyy/m/d\\ h:m", "yyyy/m/d\\ h:m:s", "yyyy/m/d\\ h:mm", "m/d/yy h:mm;@",
"yyyy/m/d\\ h:mm\\ AM/PM;@");
List<String> EXCEL_FORMAT_INDEX_DATE_NYR_STRING = Arrays.asList("m/d/yy", "[$-F800]dddd\\,\\ mmmm\\ dd\\,\\ yyyy",
"[DBNum1][$-804]yyyy\"年\"m\"月\"d\"日\";@", "yyyy\"年\"m\"月\"d\"日\";@", "yyyy/m/d;@", "yy/m/d;@", "m/d/yy;@",
"[$-409]d/mmm/yy", "[$-409]dd/mmm/yy;@", "reserved-0x1F", "reserved-0x1E", "mm/dd/yy;@", "yyyy/mm/dd",
"d-mmm-yy", "[$-409]d\\-mmm\\-yy;@", "[$-409]d\\-mmm\\-yy", "[$-409]dd\\-mmm\\-yy;@",
"[$-409]dd\\-mmm\\-yy", "[DBNum1][$-804]yyyy\"年\"m\"月\"d\"日\"", "yy/m/d", "mm/dd/yy", "dd\\-mmm\\-yy");
List<String> EXCEL_FORMAT_INDEX_DATE_NY_STRING = Arrays.asList("[DBNum1][$-804]yyyy\"年\"m\"月\";@",
"[DBNum1][$-804]yyyy\"年\"m\"月\"", "yyyy\"年\"m\"月\";@", "yyyy\"年\"m\"月\"", "[$-409]mmm\\-yy;@",
"[$-409]mmm\\-yy", "[$-409]mmm/yy;@", "[$-409]mmm/yy", "[$-409]mmmm/yy;@", "[$-409]mmmm/yy",
"[$-409]mmmmm/yy;@", "[$-409]mmmmm/yy", "mmm-yy", "yyyy/mm", "mmm/yyyy", "[$-409]mmmm\\-yy;@",
"[$-409]mmmmm\\-yy;@", "mmmm\\-yy", "mmmmm\\-yy");
List<String> EXCEL_FORMAT_INDEX_DATE_YR_STRING = Arrays.asList("[DBNum1][$-804]m\"月\"d\"日\";@",
"[DBNum1][$-804]m\"月\"d\"日\"", "m\"月\"d\"日\";@", "m\"月\"d\"日\"", "[$-409]d/mmm;@", "[$-409]d/mmm", "m/d;@",
"m/d", "d-mmm", "d-mmm;@", "mm/dd", "mm/dd;@", "[$-409]d\\-mmm;@", "[$-409]d\\-mmm");
List<String> EXCEL_FORMAT_INDEX_DATE_XQ_STRING = Arrays.asList("[$-804]aaaa;@", "[$-804]aaaa");
List<String> EXCEL_FORMAT_INDEX_DATE_Z_STRING = Arrays.asList("[$-804]aaa;@", "[$-804]aaa");
List<String> EXCEL_FORMAT_INDEX_DATE_Y_STRING = Arrays.asList("[$-409]mmmmm;@", "mmmmm", "[$-409]mmmmm");
List<String> EXCEL_FORMAT_INDEX_TIME_STRING = Arrays.asList("mm:ss.0", "h:mm", "h:mm\\ AM/PM", "h:mm:ss",
"h:mm:ss\\ AM/PM", "reserved-0x20", "reserved-0x21", "[DBNum1]h\"时\"mm\"分\"", "[DBNum1]上午/下午h\"时\"mm\"分\"",
"mm:ss", "[h]:mm:ss", "h:mm:ss;@", "[$-409]h:mm:ss\\ AM/PM;@", "h:mm;@", "[$-409]h:mm\\ AM/PM;@",
"h\"时\"mm\"分\";@", "h\"时\"mm\"分\"\\ AM/PM;@", "h\"时\"mm\"分\"ss\"秒\";@", "h\"时\"mm\"分\"ss\"秒\"_ AM/PM;@",
"上午/下午h\"时\"mm\"分\";@", "上午/下午h\"时\"mm\"分\"ss\"秒\";@", "[DBNum1][$-804]h\"时\"mm\"分\";@",
"[DBNum1][$-804]上午/下午h\"时\"mm\"分\";@", "h:mm AM/PM", "h:mm:ss AM/PM", "[$-F400]h:mm:ss\\ AM/PM");
Short EXCEL_FORMAT_INDEX_DATA_EXACT_NY = 57;
Short EXCEL_FORMAT_INDEX_DATA_EXACT_YR = 58;
List<Short> EXCEL_FORMAT_INDEX_TIME_EXACT = Arrays.asList(new Short[] { 55, 56 });
String[] WEEK_DAYS = { "日", "一", "二", "三", "四", "五", "六" };
}
2、时间格式化器
package com.dsanjun.poi.constant;
import java.text.SimpleDateFormat;
import org.apache.poi.ss.usermodel.DataFormatter;
public interface DateFormaters {
SimpleDateFormat COMMON_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat COMMON_TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat COMMON_DATE_FORMAT_NYR = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat COMMON_DATE_FORMAT_NY = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat COMMON_DATE_FORMAT_YR = new SimpleDateFormat("MM-dd");
SimpleDateFormat COMMON_DATE_FORMAT_Y = new SimpleDateFormat("MM");
DataFormatter EXCEL_07_DATA_FORMAT = new DataFormatter();
}
3、 获取格式化后时间字符串的方法
public static String getFormatDateStringValue(Short dataFormat, String dataFormatString, double value) {
if (!DateUtil.isValidExcelDate(value)) {
return null;
}
Date date = DateUtil.getJavaDate(value);
if (Constants.EXCEL_FORMAT_INDEX_DATE_NYRSFM_STRING.contains(dataFormatString)) {
return DateFormaters.COMMON_DATE_FORMAT.format(date);
}
if (Constants.EXCEL_FORMAT_INDEX_DATE_NYR_STRING.contains(dataFormatString)) {
return DateFormaters.COMMON_DATE_FORMAT_NYR.format(date);
}
if (Constants.EXCEL_FORMAT_INDEX_DATE_NY_STRING.contains(dataFormatString)
|| Constants.EXCEL_FORMAT_INDEX_DATA_EXACT_NY.equals(dataFormat)) {
return DateFormaters.COMMON_DATE_FORMAT_NY.format(date);
}
if (Constants.EXCEL_FORMAT_INDEX_DATE_YR_STRING.contains(dataFormatString)
|| Constants.EXCEL_FORMAT_INDEX_DATA_EXACT_YR.equals(dataFormat)) {
return DateFormaters.COMMON_DATE_FORMAT_YR.format(date);
}
if (Constants.EXCEL_FORMAT_INDEX_DATE_Y_STRING.contains(dataFormatString)) {
return DateFormaters.COMMON_DATE_FORMAT_Y.format(date);
}
if (Constants.EXCEL_FORMAT_INDEX_DATE_XQ_STRING.contains(dataFormatString)) {
return Constants.COMMON_DATE_FORMAT_XQ + CommonUtils.dateToWeek(date);
}
if (Constants.EXCEL_FORMAT_INDEX_DATE_Z_STRING.contains(dataFormatString)) {
return Constants.COMMON_DATE_FORMAT_Z + CommonUtils.dateToWeek(date);
}
if (Constants.EXCEL_FORMAT_INDEX_TIME_STRING.contains(dataFormatString)
|| Constants.EXCEL_FORMAT_INDEX_TIME_EXACT.contains(dataFormat)) {
return DateFormaters.COMMON_TIME_FORMAT.format(DateUtil.getJavaDate(value));
}
if (DateUtil.isADateFormat(dataFormat, dataFormatString)) {
return DateFormaters.COMMON_TIME_FORMAT.format(value);
}
return null;
}