用Poi读取excel数据:::poi3.7)
1 、读取エクセル
プライベートリスト<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); }シート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 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 存储日期、时间均以数值类型进行存储、读取时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.自定义日期格式:处理yyy年m
判断Cell.getCellStyle()。getDataFormat()值、解析数值格式
yyyy
m月d日----> 58
H时mm分---> 32
2 、字符格式(cell_type_string):直接获取内容
private string parseexcel(セルセル){string result = new String(); switch(cell.getCellType()){case hssfcell.cell_type_numeric:// if(cell.getCellStyle()。getDataFormat()== HSSFDATAFORMAT .GETBUILTINFORMAT( "H:MM")){SDF = new SimpledateFormat( "HH:MM"); } else {// }日付date = sell.getDateCellValue(); result = sdf.format(date); } else if(cell.getCellStyle()。getDataFormat()== 58){// double value = cell.getNumericCellValue();日付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.getDataFormattring(); //单元格设置成常规if(temp.equals( "general")){format.applypattern( "#"); } result = format.format(value); } 壊す; case hssfcell.cell_type_string:// string壊す;ケースhssfcell.cell_type_blank:result = "";デフォルト:result = "";壊す; } return result; }*万能处理方案:
所有日期格式都可以通过getDataFormat()值来判断
yyyy-mm-dd ----- 14
yyyy
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){// } else if(format == 20 || format == 32){// } double value = cell.getNumericCellValue();日付date = org.apache.poi.ss.usermodel.dateutil.getjavadate(value); result = sdf.format(date); }以上这篇poi对(实例代码)就是小编分享给大家的全部内容了、希望能给大家一个参考、也希望大家多多支持武林网。