用 poi 读取 Excel 数据 : : : poi3.7)
1 、读取 Excel
Приватный список <string []> RosulveFile (inputStream IS, String Suffix, int startrow) бросает ioException, filenotFoundException {Workbook xssfworkbook = null; if ("xls" .equals (суффикс)) {xssfworkbook = new hssfworkbook (is); } else if ("xlsx" .equals (суффикс)) {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 = 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 数据处理 :
Excel 存储日期、时间均以数值类型进行存储 读取时 poi 先判断是是否是数值类型 , 再进行判断转化
1 、数值格式 (cell_type_numeric):
1. 纯数值格式 : GetNumericCellValue () 直接获取数据
2. : : 处理 yyyy-mm-dd, d/m/yyyy h: mm, hh: мм 等不含文字的日期格式
1).
2). 判断是日期或者时间
cell.getCellStyle (). getDataFormat () == hssfdataformat.getbuiltinformat ("h: mm")
Или: cell.getCellStyle (). GetDataFormat () == hssfdataformat.getBuiltinformat ("yyyy-mm-dd")
3. : : 处理 yyyy 年 m 月 d 日, H 时 мм 分, yyyy 年 m 月等含文字的日期格式
判断 cell.getCellStyle (). GetDataFormat () 值 , 解析数值格式
yyyy 年 m 月 d 日 -----> 31
M 月 D 日 ----> 58
H 时 мм 分 ---> 32
2 、字符格式 (cell_type_string): 直接获取内容
Private String parseexcel (ячейка ячейки) {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 ("yyy-mm-dd"); двойное значение = cell.getNumericCellValue (); Дата дата = org.apache.poi.ss.usermodel.dateutil .getjavadate (значение); result = sdf.format (date); } else {double value = cell.getNumericCellValue (); CellStyle Style = cell.getCellStyle (); DecimalFormat format = new DecimalFormat (); String temp = style.getDataFormatString (); // 单元格设置成常规 if (temp.equals ("general")) {format.applypattern ("#"); } result = format.format (value); } перерыв; case hssfcell.cell_type_string: // string 类型 result = cell.getrichstringcellvalue (). toString (); перерыв; case hssfcell.cell_type_blank: result = ""; по умолчанию: result = ""; перерыв; } return Result; }*: :
所有日期格式都可以通过 getDataFormat () 值来判断
yyyy-mm-dd ----- 14
yyyy 年 m 月 d 日 --- 31
yyyy 年 m 月 ------- 57
M 月 D 日 ---------- 58
HH: MM ----------- 20
H 时 мм 分 ------- 32
// 1 、判断是否是数值格式 if (cell.getCelltype () == hssfcell.cell_type_numeric) {короткий формат = 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"); } двойное значение = cell.getNumericCellValue (); Дата дата = org.apache.poi.ss.usermodel.dateutil.getJavadate (значение); result = sdf.format (date); }以上这篇 poi 对 Excel 自定义日期格式的读取 (实例代码) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。