用 poi 读取 excel 数据 (版本号 : : poi3.7)
1 、读取 Excel
private list <string []> RosolveFile (InputStream ist, String -Suffix, int Startrow) löst IOException aus, FilenotFoundException {Workbook XSSFWorkbook = null; if ("xls" .equals (Suffix)) {xssfworkbook = new HSSFWorkbook (IS); } else if ("xlsx" .Equals (Suffix)) {xssfworkbook = new XSSFWorkbook (IS); } Sheet xssfsheet = xssfworkbook.getsheetat (0); if (xssfsheet == null) {return null; } ArrayList <string []> list = new ArrayList <string []> (); int lastrownum = xssfsheet.getLastrownum (); für (int rownum = startrow; Rownum <= lastrownum; Rownum ++) {if (xssfsheet.getRow (Rownum)! Short FirstCellnum = xsssfrow.getFirstcellnum (); kurzlastcellnum = xsssfrow.getLastcellnum (); if (firstcellnum! für (int cellnum = firstcellnum; cellnum <lastcellnum; cellnum ++) {cell xssfcell = xsssfrow.getCell (CellNum); if (xssfcell == null) {values [cellnum] = ""; } else {values [cellnum] = parseExcel (xssfcell); }} list.add (Werte); }}} Rückgabeliste; }2 、 Excel 数据处理 :
Excel 存储日期、时间均以数值类型进行存储 , 读取时 poi 先判断是是否是数值类型 , 再进行判断转化
1 、数值格式 (cell_type_numeric):
1. 纯数值格式 : GetnumericcellValue () 直接获取数据
2. : : 处理 处理yjyy-mm-dd, d/m/yjyy h: mm, hh: mm 等不含文字的日期格式
1). 判断是否是日期格式:
2). 判断是日期或者时间
cell.getCellStyle (). getDataFormat () == HSSFDataFormat.getBuiltInformat ("H: mm")
Oder: cell.getCellStyle (). GetDataFormat () == HSSFDataFormat.getBuiltInformat ("yyyy-mm-dd")
3. : : 处理 yyyy 年 m 月 d 日, h 时 mm 分, yyyy 年 m 月等含文字的日期格式
判断 cell.getCellStyle (). GetDataFormat () 值 , 解析数值格式
yjyy 年 m 月 d 日 ------> 31
M 月 D 日 ----> 58
H 时 mm 分 ---> 32
2 、字符格式 (cell_type_string): 直接获取内容
private string parseexcel (cell cell) {string result = new String (); Switch (cell.getCellType ()) {case hssfcell.cell_type_numeric: // 数字类型 if (hssfdatutil.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 (Datum); } else if (cell.getCellStyle (). getDataFormat () == 58) {// 处理自定义日期格式 : M 月 d 日 (通过判断单元格的格式 id 解决 id 的值是 58) SimpleDateFormat Sdf = new SimpleDateFormat ("Yyyy-Mm-Dd"); double value = cell.getNumericcellValue (); Datum Datum = org.apache.poi.ss.usermodel.dateUtil .getJavadate (Wert); result = sdf.format (Datum); } else {double value = cell.getNumericCellValue (); CellStyle style = cell.getCellStyle (); DecimalFormat format = new DecimalFormat (); String temp = style.getDataFormatString (); // 单元格设置成常规 if (temp.equals ("allgemeine") {format.appypattern ("#"); } result = format.format (value); } brechen; case hssfcell.cell_type_string: // string 类型 result = cell.getrichstringcellValue (). toString (); brechen; case hssfcell.cell_type_blank: result = ""; Standard: result = ""; brechen; } Rückgabeergebnis; }*万能处理方案 :
所有日期格式都可以通过 getDataFormat () 值来判断
yjyy-mm-dd ----- 14
yjyy 年 m 月 d 日 --- 31
yjyy 年 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 value = cell.getNumericcellValue (); Datum Datum = org.apache.poi.ss.usermodel.dateutil.getJavadate (Wert); result = sdf.format (Datum); }以上这篇 poi 对 excel 自定义日期格式的读取 (实例代码) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。 也希望大家多多支持武林网。