生活随笔
收集整理的這篇文章主要介紹了
POI对Excel自定义日期格式的读取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用POI讀取Excel數據:(版本號:POI3.7)
1、讀取Excel
Java代碼??
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數據處理:
Excel存儲日期、時間均以數值類型進行存儲,讀取時POI先判斷是是否是數值類型,再進行判斷轉化
1、數值格式(CELL_TYPE_NUMERIC):
1.純數值格式:getNumericCellValue() 直接獲取數據
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()值,解析數值格式
yyyy年m月d日----->31
m月d日---->58
h時mm分--->32
2、字符格式(CELL_TYPE_STRING):直接獲取內容
?
Java代碼??
private?String?parseExcel(Cell?cell)?{??????????String?result?=?new?String();??????????switch?(cell.getCellType())?{??????????case?HSSFCell.CELL_TYPE_NUMERIC:????????????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)?{??????????????????????????????????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();??????????????????????????????????if?(temp.equals("General"))?{??????????????????????format.applyPattern("#");??????????????????}??????????????????result?=?format.format(value);??????????????}??????????????break;??????????case?HSSFCell.CELL_TYPE_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
?
Java代碼??
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自定义日期格式的读取的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。