用 poi 读取 excel : : : : POI3.7)
1 、读取 Excel
القائمة الخاصة <string []> rosolvefile (inputStream Is ، String Factix ، int StarTrow) يلقي ioException ، fileNotFoundException {Workbook xssfworkbook = null ؛ if ("xls" .equals (لاحقة)) {xssfworkbook = new HSSfworkBook (IS) ؛ } if (" } sheet xssfsheet = xssfworkbook.getSheetat (0) ؛ if (xssfsheet == null) {return null ؛ } ArrayList <string []> list = new ArrayList <string []> () ؛ int lastrownum = xssfsheet.getLastRownum () ؛ لـ (int rownum = startrow ؛ rownum <= lastrownum ؛ rownum ++) {if (xssfsheet.getrow (rownum)! = null) {row xssfrow = xssfsheet.getrow (rownum) ؛ القصير firstcellnum = xssfrow.getFirstCellnum () ؛ Short lastCellNum = xssfrow.getlastcellnum () ؛ if (firstCellNum! = lastCellNum) {string [] values = new string [lastCellNum] ؛ لـ (int cellnum = firstcellnum ؛ cellnum <lastCellNum ؛ cellnum ++) {cell xssfcell = xsssfrow.getcell (cellnum) ؛ if (xssfcell == null) {values [cellnum] = "" ؛ } آخر {القيم [cellnum] = parseexcel (xssfcell) ؛ }} list.add (القيم) ؛ }}} قائمة الإرجاع ؛ }2 、 Excel : :
Excel 存储日期、时间均以数值类型进行存储 , 读取时 poi 先判断是是否是数值类型 , 再进行判断转化
1 、数值格式 (cell_type_numeric):
1. : getNumericCellValue () 直接获取数据
2. : : yyyyy-mm-dd ، d/m/yyyy h: mm ، hh: mm 等不含文字的日期格式
1).
2). 判断是日期或者时间
cell.getCellStyle ().
أو: cell.getCellStyle ().
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 (خلية الخلية) {String result = new String () ؛ Switch (cell.getCelltype ()) {case hssfcell.cell_type_numeric: // 数字类型 if (hssfdateutil.iscelldateformatted (cell)) {// 处理日期格式、时间格式 spimdateformat sdf = null ؛ if (cell.getCellStyle (). getDataFormat () == hssfdataformat .getBuiltIntFormat ("h: mm") {sdf = new SimpleDateFormat ("hh: mm") ؛ } آخر {// 日期 sdf = new SimpleDateFormat ("Yyyy-MM-DD") ؛ } تاريخ التاريخ = cell.getDateCellValue () ؛ النتيجة = sdf.format (التاريخ) ؛ } آخر إذا (cell.getCellStyle (). قيمة مزدوجة = cell.getNumericCellValue () ؛ تاريخ تاريخ = org.apache.poi.ss.usermodel.dateutil .getjavadate (القيمة) ؛ النتيجة = sdf.format (التاريخ) ؛ } آخر {double value = cell.getNumericCellValue () ؛ نمط cellstyle = cell.getCellStyle () ؛ تنسيق decimalformat = جديد decimalformat () ؛ سلسلة temp = style.getDataFormatString () ؛ // 单元格设置成常规 if (temp.equals ("general")) {format.applypattern ("#") ؛ } النتيجة = format.format (value) ؛ } استراحة؛ case hssfcell.cell_type_string: // string 类型 result = 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 分 ------- 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") ؛ } if if (format == 20 || format == 32) {// 时间 sdf = new SimplEdateFormat ("hh: mm") ؛ } قيمة مزدوجة = cell.getNumericCellValue () ؛ تاريخ تاريخ = org.apache.poi.ss.usermodel.dateutil.getjavadate (القيمة) ؛ النتيجة = sdf.format (التاريخ) ؛ }以上这篇 poi 对 excel 自定义日期格式的读取 (实例代码) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。