POI對Excel自定義日期格式的讀取(實例代碼)
用POI讀取Excel數(shù)據(jù):(版本號:POI3.7)
1、讀取Excel
private List<String[]> rosolveFile(InputStream is, String suffix,
int startRow) throws IOException, FileNotFoundException {
Workbook xssfWorkbook = null;
if ("xls".equals(suffix)) {
xssfWorkbook = new HSSFWorkbook(is);
} else if ("xlsx".equals(suffix)) {
xssfWorkbook = new XSSFWorkbook(is);
}
Sheet xssfSheet = xssfWorkbook.getSheetAt(0);
if (xssfSheet == null) {
return null;
}
ArrayList<String[]> list = new ArrayList<String[]>();
int lastRowNum = xssfSheet.getLastRowNum();
for (int rowNum = startRow; rowNum <= lastRowNum; rowNum++) {
if (xssfSheet.getRow(rowNum) != null) {
Row xssfRow = xssfSheet.getRow(rowNum);
short firstCellNum = xssfRow.getFirstCellNum();
short lastCellNum = xssfRow.getLastCellNum();
if (firstCellNum != lastCellNum) {
String[] values = new String[lastCellNum];
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Cell xssfCell = xssfRow.getCell(cellNum);
if (xssfCell == null) {
values[cellNum] = "";
} else {
values[cellNum] = parseExcel(xssfCell);
}
}
list.add(values);
}
}
}
return list;
}
2、Excel數(shù)據(jù)處理:
Excel存儲日期、時間均以數(shù)值類型進行存儲,讀取時POI先判斷是是否是數(shù)值類型,再進行判斷轉(zhuǎn)化
1、數(shù)值格式(CELL_TYPE_NUMERIC):
1.純數(shù)值格式:getNumericCellValue() 直接獲取數(shù)據(jù)
2.日期格式:處理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式
1).判斷是否是日期格式:HSSFDateUtil.isCellDateFormatted(cell)
2).判斷是日期或者時間
cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")
OR: cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd")
3.自定義日期格式:處理yyyy年m月d日,h時mm分,yyyy年m月等含文字的日期格式
判斷cell.getCellStyle().getDataFormat()值,解析數(shù)值格式
yyyy年m月d日----->31
m月d日---->58
h時mm分--->32
2、字符格式(CELL_TYPE_STRING):直接獲取內(nèi)容
private String parseExcel(Cell cell) {
String result = new String();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:// 數(shù)字類型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 處理日期格式、時間格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 處理自定義日期格式:m月d日(通過判斷單元格的格式id解決,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 單元格設(shè)置成常規(guī)
if (temp.equals("General")) {
format.applyPattern("#");
}
result = format.format(value);
}
break;
case HSSFCell.CELL_TYPE_STRING:// String類型
result = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result;
}
*萬能處理方案:
所有日期格式都可以通過getDataFormat()值來判斷
yyyy-MM-dd----- 14
yyyy年m月d日--- 31
yyyy年m月------- 57
m月d日 ---------- 58
HH:mm----------- 20
h時mm分 ------- 32
//1、判斷是否是數(shù)值格式
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
short format = cell.getCellStyle().getDataFormat();
SimpleDateFormat sdf = null;
if(format == 14 || format == 31 || format == 57 || format == 58){
//日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}else if (format == 20 || format == 32) {
//時間
sdf = new SimpleDateFormat("HH:mm");
}
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
}
以上這篇POI對Excel自定義日期格式的讀取(實例代碼)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
myatisplus的saveOrUpdate的提交總是update問題
這篇文章主要介紹了myatisplus的saveOrUpdate的提交總是update問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Springboot項目實現(xiàn)將類從@ComponentScan中排除
這篇文章主要介紹了Springboot項目實現(xiàn)將類從@ComponentScan中排除,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
詳解SpringBoot 解決攔截器注入Service為空問題
這篇文章主要介紹了詳解SpringBoot 解決攔截器注入Service為空問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
詳解多云架構(gòu)下的JAVA微服務(wù)技術(shù)解析
本文介紹了基于開源自建和適配云廠商開發(fā)框架兩種構(gòu)建多云架構(gòu)的思路,以及這些思路的優(yōu)缺點2021-05-05

