用 poi 读取 excel 数据 :(版本号 : poi3.7)
1 、读取 Excel
Daftar Pribadi <String []> rosolvefile (InputStream IS, String Suffix, int StarTrow) melempar IoException, FileNotFoundException {workbook xsfworkbook = null; if ("xls" .Equals (sufiks)) {xssfworkbook = hssfworkbook baru (IS); } lain jika ("xlsx" .Equals (sufiks)) {xssfworkbook = new xssfworkbook (IS); } Sheet xssfsheet = xssfworkbook.getsheetat (0); if (xssfsheet == null) {return null; } ArrayList <string []> list = new ArrayList <string []> (); int lastrownum = xssfsheet.getlastrownum (); untuk (int rownum = startrow; rownum <= lastrownum; rownum ++) {if (xssfsheet.getrow (rownum)! = null) {baris xssfrow = xssfsheet.getrow (rownum); firstcellnum pendek = xssfrow.getFirstcellNum (); lastcellnum pendek = xssfrow.getLastCellNum (); if (firstcellnum! = lastcellnum) {string [] values = string baru [lastcellNum]; untuk (int cellnum = firstcellnum; cellnum <lastcellnum; cellnum ++) {cell xssfcell = xssfrow.getCell (cellnum); if (xssfcell == null) {values [cellNum] = ""; } else {values [cellnum] = parseexcel (xssfcell); }} list.add (nilai); }}} daftar pengembalian; }2 、 excel 数据处理 :
Excel 存储日期、时间均以数值类型进行存储 , 读取时 poi 先判断是是否是数值类型 , ,
1 、数值格式 (cell_type_numeric):
1. 纯数值格式 : getNumericCellValue () 直接获取数据
2. 日期格式 : 处理 yyyy-mm-dd, d/m/yyyy h: mm, hh: mm 等不含文字的日期格式
1). 判断是否是日期格式 : hssfdateutil.iscelldateFormatted (sel)
2). 判断是日期或者时间
cell.getCellstyle (). getDataFormat () == hssfdataformat.getBuiltinformat ("h: mm")
Atau: 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 (sel sel) {string result = new string (); switch (cell.getCellType ()) {case hssfcell.cell_type_numeric: // 数字类型 if (hssfdateutil.iscellDateFormatted (sel)) {// 处理日期格式、时间格式 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 (); hasil = sdf.format (tanggal); } lain jika (cell.getCellstyle (). getDataFormat () == 58) {// 处理自定义日期格式 : m 月 d 日 (通过判断单元格的格式 id 解决 , id 的值是 58) SimpledateFormat SDF = new SimpledateFormat ("yyyy-mm-dd"); nilai ganda = cell.getNumericCellValue (); Tanggal tanggal = org.apache.poi.ss.usermodel.dateutil .getJavadate (nilai); hasil = sdf.format (tanggal); } else {double value = cell.getNumericCellValue (); Cellstyle style = cell.getCellstyle (); Format decimalformat = decimalformat baru (); String temp = style.getDataFormatString (); // 单元格设置成常规 if (temp.equals ("umum")) {format.Applypattern ("#"); } result = format.format (value); } merusak; case hssfcell.cell_type_string: // string 类型 result = cell.getRichStringCellValue (). ToString (); merusak; case hssfcell.cell_type_blank: result = ""; default: result = ""; merusak; } hasil pengembalian; }*万能处理方案 :
所有日期格式都可以通过 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"); } nilai ganda = cell.getNumericCellValue (); Tanggal tanggal = org.apache.poi.ss.usermodel.dateutil.getJavadate (nilai); hasil = sdf.format (tanggal); }以上这篇 poi 对 excel 自定义日期格式的读取 (实例代码) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。