用poi讀取excel:poi:poi3.7)
1 、讀取 excel
私有列表<string []> RosOvelFile(InputStream IS,字符串後綴,int startrow)拋出IOException,filenotfoundexception {workbook xsssfworkbook = null; if(“ xls” .equals(suffix)){xssfworkbook = new HSSFWorkbook(is); } else if(“ xlsx” .equals(suffix)){xssfworkbook = new XSSFWorkBook(is); }表XSSFSheet = xssfworkbook.getSheetat(0); if(xssfsheet == null){return null; } arraylist <string []> list = new arraylist <string []>(); int lastronnum = xssfsheet.getLastronnum(); for(int rownum = startrow; rownum <= lastrownum; rownum ++){if(xssfsheet.getrow(rownum)!= null){row xssfrow = xssfrow = xssfsheet.getrow(ronnum);簡短的FirstCellnum = XSSFROW.GEGFIRSTCELLNUM();短lastCellnum = XSSFROW.GetLastCellnum(); if(firstCellnum!= lastCellnum){string [] values = new String [lastCellnum]; for(int cellnum = firstcellnum; cellnum <lastCellnum; cellnum ++){cell xssfcell = xssfcell = xssfrow.getCell(cellnum);如果(xssfcell == null){values [cellnum] =“”; } else {values [cellnum] = parseexcel(xssfcell); }} list.add(values); }}}返回列表; }2,excel數據處理:
excel 存儲日期、時間均以數值類型進行存儲,讀取時poi先判斷是是否是數值類型,再進行判斷轉化
1 、數值格式(cell_type_numeric):
1.純數值格式:getNumericCellValue()直接獲取數據
2.日期格式:處理Yyyy-MM-DD,D/M/Yyyy H:MM,HH:MM等不含文字的日期格式
1)。
2)。判斷是日期或者時間
cell.getCellStyle()。 getDataFormat()== hssfdataformat.getBuiltInformat(“ H:MM”)
或: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):直接獲取內容
私有字符串Parseexcel(單元格){字符串結果= new String(); switch(cell.getCellType()){case hssfcell.cell_type_numeric:// lif(hssfdateutil.iscelldateformatted(cell)){// simpleDateTateFormat sdf = null; if(cell.getCellStyle()。getDataFormat()== hssfdataformat .getBuiltInformat(“ h:mm”)){sdf = new simpledateateFormat(“ hh:mm”); } else {//日期sdf = new simpledateFormat(“ yyyy-mm-dd”); } date date = cell.getDateCellValue();結果= sdf.format(date); } else if(cell.getCellStyle()。getDataFormat()== 58){//處理自定義日期格式:m月d日(通過判斷單元格的格式iD解決,ID的值是58)simpledateateformat sdf = new simpledateateFormat(new simpledateateFormat(雙值= cell.getNumericCellValue();日期= org.apache.poi.ss.s.usermodel.dateutil.getjavadate(value);結果= sdf.format(date); } else {double value = cell.getNumericCellValue(); CellStyle樣式= Cell.GetCellstyle(); DemalFormat格式=新的Demalformat();字符串temp = style.getDataFormatString(); //單元格設置成常規if(temp.equals(“ enstry”)){format.applypattern(“#”); }結果= format.format(value); } 休息; case hssfcell.cell_type_string:// string類型結果= cell.getRichStringCellValue()。 toString();休息; case hssfcell.cell_type_blank:result =“”;默認值:結果=“”;休息; }返回結果; }*萬能處理方案:
所有日期格式都可以通過getDataFormat()值來判斷
YYYY-MM-DD ------ 14
yyyy年m月d日--- 31
Yyyy年m月-------- 57
M月d日----------- 58
HH:mm ------------- 20
h時mm分-------- 32
// 1、判斷是否是數值格式如果(cell.getCelltype()== hssfcell.cell_type_numeric){簡短格式= cell.getCellStyle()。 getDataformat(); SimpleDateFormat SDF = null; if(格式== 14 ||格式== 31 ||格式== 57 ||格式== 58){//日期sdf = new simpledateFormat(“ yyyy-mm-dd”); } else if(格式== 20 ||格式== 32){// sdf = new SimpleDateFormat(“ HH:MM”); } double value = cell.getNumericCellValue();日期= org.apache.poi.ss.usermodel.dateutil.getjavadate(value);結果= sdf.format(date); }以上這篇poi對excel自定義日期格式的讀取(實例代碼)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。 ,也希望大家多多支持武林網。