用 Poi 读取 Excel 数据 :( 版本号 : POI3.7)
1 、读取 Excel
Liste privée <String []> RosololFile (InputStream Is, String Suffix, int Startrow) lève IOException, filenotFoundException {Workbook XSSFWorkBook = null; if ("xls" .equals (suffixe)) {xssfworkbook = new hssfworkbook (is); } else if ("xlsx" .equals (suffixe)) {xssfworkbook = new XSSFWorkBook (IS); } Feuille xssfsheet = xssfworkbook.getSheetAt (0); if (xssfSheet == null) {return null; } ArrayList <String []> list = new ArrayList <String []> (); int lastrown = xssfsheet.getLastrowmum (); pour (int Rownum = StarTrow; Rownum <= lastrownm; rownum ++) {if (xsssfsheet.getRow (rownum)! = null) {row xssfrow = 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) {valeurs [cellNum] = ""; } else {Values [CellNum] = paSeExcel (xssfCell); }} list.add (valeurs); }}} liste de retour; }2 、 Excel 数据处理 :
Excel 存储日期、时间均以数值类型进行存储 , 读取时 POI 先判断是是否是数值类型 , 再进行判断转化
1 、数值格式 (Cell_type_numeric):
1. 纯数值格式 : getNumericCellValue () 直接获取数据
2. 日期格式 : 处理 Yyyy-mm-dd, d / m / yyyy h: mm, hh: mm 等不含文字的日期格式
1). 判断是否是日期格式 : hssfdateutil.iscellDateForté (cellule)
2). 判断是日期或者时间
Cell.getCellStyle (). GetDataFormat () == HssfDataFormat.getBuiltInFormat ("H: MM")
Ou: 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): 直接获取内容
String privé ParseExcel (Cell Cell) {String result = new String (); switch (Cell.getCellType ()) {case hssfcell.cell_type_numeric: // 数字类型 if (hssfdateutil.iscellDateFormatted (cell)) {// 处理日期格式、时间格式 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 (); result = sdf.format (date); } else if (Cell.getCellStyle (). getDataFormat () == 58) {// 处理自定义日期格式 : M 月 D 日 (通过判断单元格的格式 ID 解决 , ID 的值是 58) SimpledateFormat sdf = new SimpledateFormat ("yyyy-mm-dd"); double valeur = cell.getNumericCellValue (); DATE DATE = org.apache.poi.ss.serModel.dateUtil .getJavadate (valeur); result = sdf.format (date); } else {double valeur = cell.getNumericCellValue (); CellStyle Style = Cell.getCellStyle (); DecimalFormat Format = new DecimalFormat (); String temp = style.getDataFormatStRing (); // 单元格设置成常规 if (temp.equals ("général")) {format.ApplyPattern ("#"); } result = format.format (valeur); } casser; cas hssfcell.cell_type_string: // string 类型 result = cell.getRichStringCellValue (). toString (); casser; cas hssfcell.cell_type_blank: result = ""; Default: result = ""; casser; } Retour Résultat; }* : :
所有日期格式都可以通过 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"); } double valeur = cell.getNumericCellValue (); DATE DATE = org.apache.poi.ss.usermodel.dateutil.getjavadate (valeur); result = sdf.format (date); }以上这篇 poi 对 excel 自定义日期格式的读取 (实例代码) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。