用 Poi 读取 Excel 数据 : : 版本号 : Poi3.7 vor
1 、读取 Excel
Lista privada <string []> rosolvefile (InputStream é, sufixo de string, int startrow) lança IoException, filenotfoundException {Book XSSfworkbook = null; if ("xls" .equals (sufixo)) {xssfworkbook = new HSSFWorkbook (IS); } else if ("xlsx" .equals (sufixo)) {xssfworkbook = new XSSFWorkbook (IS); } Folha xssfsheet = xssfworkbook.getSheetat (0); if (xssfsheet == null) {return null; } ArrayList <String []> list = new ArrayList <String []> (); int lastrownum = xssfsheet.getlastrownum (); para (int rownum = startrow; rownum <= lastrownum; rownum ++) {if (xssfsheet.getRow (rownum)! = null) {linha xssfrow = xssfSheet.getRow (rownum); curto primeiroCellnum = xssfrow.getfirstCellnum (); curto lastCellnum = xssfrow.getLastcellnum (); if (FirstCellnum! = LastCellnum) {String [] valores = new String [LastCellnum]; for (int Cellnum = FirstCellnum; Cellnum <LastCellnum; Cellnum ++) {Cell XSSFCell = XSSFrow.getCell (Cellnum); if (xssfcell == null) {valores [Cellnum] = ""; } else {valores [Cellnum] = parseexcel (xSSFCell); }} list.add (valores); }}} Lista de retorno; }2 、 Excel 数据处理:
Excel 存储日期、时间均以数值类型进行存储 , 读取时 poi 先判断是是否是数值类型 , 再进行判断转化
1 、数值格式 (Cell_type_numeric):
1. 纯数值格式 : getNumericCellValue () 直接获取数据
2. 日期格式 : 处理 yyyy-mm-dd, d/m/yyyy h: mm, hh: mm 等不含文字的日期格式
1). 判断是否是日期格式 : HssfdateUtil.iscellDateFormatted (célula)
2). 判断是日期或者时间
Cell.getCellStyle (). getDataFormat () == hssfdataFormat.getBuiltInformat ("h: mm")
Ou: Cell.getCellStyle (). GetDataFormat () == hssfdataFormat.getBuiltInformat ("AAAA-MM-DD")
3. 自定义日期格式 : 处理 yyyy 年 m 月 d 日, h 时 mm 分, yyyy 年 m 月等含文字的日期格式
判断 Cell.getCellStyle (). GetDataFormat () 值 , 解析数值格式
Aaaaa 年 m 月 d 日 -----> 31
M 月 D 日 ----> 58
h 时 mm 分 ---> 32
2 、字符格式 (Cell_Type_String): 直接获取内容
ParseExcel private String (célula Cell) {String result = new String (); switch (Cell.getCellType ()) {case hssfcell.cell_type_numeric: // 数字类型 if (hsSfdateutil.iscelldateFormatted (Cell)) {// 处理日期格式、时间格式 SimpledEformat sdf = null; if (Cell.getCellStyle (). getDataFormat () == hssfdataformat .getBuiltInformat ("h: mm")) {sdf = new SimpleDateFormat ("hh: mm"); } else {// 日期 sdf = new SimpleDateFormat ("yyyy-mm-dd"); } Data date = Cell.getDateCellValue (); resultado = sdf.format (data); } else if (Cell.getCellStyle (). getDataFormat () == 58) {// 处理自定义日期格式 : : 月 d 日 (通过判断单元格的格式 id 解决 , id 的值是 58) SimpledEformat Sdf = new SimpleDeformat ("yyyyy-mm-dd"); Valor duplo = Cell.getNumericCellValue (); Data data = org.apache.poi.ss.usermodel.dateutil .getjavadate (valor); resultado = sdf.format (data); } else {Double Value = Cell.getNumericCellValue (); CellStyle Style = Cell.getCellStyle (); DecimalFormat formato = new DecimalFormat (); String temp = style.getDataFormatString (); // 单元格设置成常规 if (temp.equals ("general")) {format.applyPattern ("#"); } resultado = format.format (value); } quebrar; case hssfcell.cell_type_string: // string 类型 resultado = célula.getRichstringCellValue (). ToString (); quebrar; case hssfcell.cell_type_blank: resultado = ""; padrão: resultado = ""; quebrar; } resultado de retorno; }*万能处理方案:
所有日期格式都可以通过 getDataFormat () 值来判断
AAAA-MM-DD ----- 14
Aaaaa 年 m 月 d 日 --- 31
AAA AA AYYY 年 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 (formato == 14 || formato == 31 || formato == 57 || formato == 58) {// 日期 sdf = new SimpleDateFormat ("yyyy-mm-dd"); } else if (format == 20 || formato == 32) {// 时间 sdf = new SimpleDateFormat ("hh: mm"); } valor duplo = Cell.getNumericCellValue (); Data data = org.apache.poi.ss.usermodel.dateutil.getjavadate (valor); resultado = sdf.format (data); }以上这篇 Poi 对 Excel 自定义日期格式的读取 (实例代码) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。