用 Poi 读取 Excel 数据 :(版本号 : Poi3.7)
1 、读取 Excel
Lista privada <String []> RosolveFile (InputStream IS, String Suffix, Int StarTrow) lanza IOException, FileNotFoundException {Workbook xssfworkbook = null; if ("xls" .equals (sufix)) {xssfworkbook = new hssfworkbook (is); } else if ("xlsx" .equals (sufix)) {xssfworkbook = new xssfworkbook (is); } Hoja 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) {fila xssfrow = xssfsheet.getrow (robo); corto FirstCellnum = xssfrow.getFirstCellnum (); corto LastCellnum = xssfrow.getLastCellNum (); if (FirstCellnum! = LastCellNum) {String [] valores = new String [LastCellNum]; para (int cellNum = FirstCellnum; CellNum <LastCellNum; CellNum ++) {Cell XSSFCELL = XSSFROW.GETCELL (CELLNUM); if (xssfcell == null) {valores [celular] = ""; } else {valores [CellNum] = parseExcel (xssfcell); }} list.add (valores); }}} Lista de retorno; }2 、 Excel 数据处理 :
Excel 存储日期、时间均以数值类型进行存储 , 读取时 Poi 先判断是是否是数值类型 , 再进行判断转化
1 、数值格式 (Cell_Type_Numeric):
1. : : getNumericCellValue () 直接获取数据
2. : : 处理a yyyy-mm-dd, d/m/yyyy h: mm, hh: mm 等不含文字的日期格式
1). : : Hssfdateutil.iscelldateFormatted (celda)
2). 判断是日期或者时间
Cell.getCellStyle (). GetDataFormat () == HSSFDataFormat.getBuiltInformat ("H: MM")
O: Cell.getCellStyle (). GetDataFormat () == HSSFDataFormat.getBuiltInformat ("yyyy-mm-dd")
3. : : 处理 aa yyy 年 m 月 d 日, h 时 mm 分, aaa yyy 年 m 月等含文字的日期格式
判断 cell.getCellStyle (). GetDataFormat () 值 解析数值格式 解析数值格式
yyyy 年 m 月 d 日 -----> 31
m 月 d 日 ----> 58
h 时 mm 分 ---> 32
2 、字符格式 (Cell_Type_String): 直接获取内容
cadena privada parseExcel (celular) {string result = new String (); switch (cell.getCellType ()) {case hssfcell.cell_type_numeric: // 数字类型 if (hssfdateutil.iscelldateformatted (celular)) {// 处理日期格式、时间格式 SimpleDateFormat sdf = null; if (cell.getCellStyle (). getDataFormat () == hssfdataformat .getBuiltInformat ("h: mm")) {sdf = new SimpleDateFormat ("HH: MM"); } else {// 日期 sdf = new SimpleDateFormat ("yyyy-mm-dd"); } Fecha date = cell.getDateCellValue (); resultado = sdf.format (fecha); } else if (cell.getCellStyle (). getDataFormat () == 58) {// 处理自定义日期格式 : M 月 D 日 (通过判断单元格的格式 ID 解决 , ID 的值是 58) SimpleFormat sdf = new SimpleDateFormat ("yyyyy-mm-dd"); valor doble = celular.getNumericCellValue (); Fecha fecha = org.apache.poi.ss.usermodel.dateutil .getJavadate (valor); resultado = sdf.format (fecha); } else {Double Value = Cell.GetNumericCellValue (); CellStyle Style = Cell.getCellStyle (); DecimalFormat Format = new DecimalFormat (); Cadena temp = style.getDataFormatString (); // 单元格设置成常规 if (temp.equals ("general")) {format.applypattern ("#"); } resultado = format.format (valor); } romper; case hssfcell.cell_type_string: // string 类型 result = cell.getrichstringcellValue (). toString (); romper; case hssfcell.cell_type_blank: result = ""; predeterminado: resultado = ""; romper; } resultado de retorno; }*万能处理方案.
所有日期格式都可以通过 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) {Formato corto = 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 Value = Cell.getNumericCellValue (); Fecha fecha = org.apache.poi.ss.usermodel.dateutil.getJavadate (valor); resultado = sdf.format (fecha); }以上这篇 Poi 对 Excel 自定义日期格式的读取 (实例代码) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。