用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自定义日期格式的读取(实例代码)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。,也希望大家多多支持武林网。