用POI讀取Excel數據:(版本號: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數據處理:
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):直接獲取內容
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) {         // 處理自定義日期格式: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();         // 單元格設置成常規         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、判斷是否是數值格式 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自定義日期格式的讀取(實例代碼)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答