用 poi 读取 Excel : : : poi3.7)
1 、读取 엑셀
개인 목록 <string []> rosolveFile (inputStream is, String 접미사, int startrow)는 ioexception, filenotfoundException {workbook xssfworkbook = null; if ( "xls".equals (wiffix)) {xssfworkbook = new hssfworkbook (is); } else if ( "xlsx".equals (wiffix)) {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 xssfrow = xsfsheet.getrow (rownum); 짧은 FirstCellNum = xssfrow.getFirstCellNum (); 짧은 lastcellnum = xssfrow.getLastCellNum (); if (firstCellNum! = lastCellNum) {String [] value = new String [lastCellNum]; for (int cellnum = firstCellNum; CellNum <lastCellNum; CellNum ++) {Cell XSSFCELL = XSSfrow.getCell (CellNum); if (xssfcell == null) {value [cellnum] = ""; } else {value [cellnum] = parseexcel (xssfcell); }} list.add (값); }}} 리턴 목록; }2 数据处理 Excel el :
Excel 存储日期、时间均以数值类型进行存储 存储日期、时间均以数值类型进行存储, 存储日期、时间均以数值类型进行存储 poi 先判断是是否是数值类型, 再进行判断转化
1 p (cell_type_numeric) :
1. um : 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 m ---> 32
2 p (cell_type_string) : 直接获取内容
개인 문자열 parseexcel (셀 셀) {문자열 result = new String (); switch (cell.getCellType ()) {case hssfcell.cell_type_numeric : // 数字类型 if (hssfdateutil.iscelldateformatted (cell)) {// simplededateformat 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 (); 결과 = sdf.format (날짜); } else if (cell.getCellStyle (). getDataFormat () == 58) {// 处理自定义日期格式 : m 月 d 日 (通过判断单元格的格式 id 解决, id 的值是 58) simpledateformat sdf = new SimpledateFormat ( "yyyy-mm-dd"); double value = cell.getNumericCellValue (); 날짜 날짜 = org.apache.poi.ss.usermodel.dateutil .getjavadate (value); 결과 = sdf.format (날짜); } else {double value = cell.getNumericCellValue (); CellStyle Style = cell.getCellStyle (); DecimalFormat Format = New DecimalFormat (); 문자열 temp = style.getDataFormatString (); // 单元格设置成常规 if (temp.equals ( "일반")) {format.applyPattern ( "#"); } result = format.format (값); } 부서지다; case hssfcell.cell_type_string : // string 类型 결과 = cell.getRichstringCellValue (). toString (); 부서지다; CASE HSSFCELL.CELL_TYPE_BLANK : result = ""; 기본값 : result = ""; 부서지다; } 반환 결과; }*万能处理方案 :
getDataFormat () 值来判断 值来判断
yyyy-mm-dd ----- 14
yyyy 年 m 月 d 日 --- 31
yyyy 年 m 月 ------- 57
m ---- d 日 ----------- 58
HH : MM ---------- 20
H 分 mm m ------- 32
// 1 (if (cell.getCellType () == hssfcell.cell_type_numeric) {짧은 형식 = cell.getCellStyle (). getDataFormat (); simpledateformat sdf = null; if (format == 14 || format == 31 || 형식 == 57 || 형식 == 58) {// 日期 sdf = new SimpledateFormat ( "yyyy-mm-dd"); } else if (format == 20 || format == 32) {// 时间 sdf = new SimpledateFormat ( "hh : mm"); } double value = cell.getNumericCellValue (); 날짜 날짜 = org.apache.poi.ss.usermodel.dateutil.getjavadate (value); 결과 = sdf.format (날짜); }poi 对 Excel 自定义日期格式的读取 (实例代码 实例代码) 就是小编分享给大家的全部内容了 就是小编分享给大家的全部内容了, 希望能给大家一个参考, 也希望大家多多支持武林网。 也希望大家多多支持武林网。