解析Excel一个工具类!

package com.wukong.cft.common.util;


import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
 * 解析Excel代码分享
 * @author Love
 *
 */
public class ExcalRead {
 //给这个方法传入一个文件流返回一个list集合
 public static ArrayList<ArrayList<String>> readExcel(InputStream inputStream) { 
        ArrayList<ArrayList<String>> Row =new ArrayList<ArrayList<String>>(); 
         
        try { 
            Workbook workBook = null; 
            try { 
            workBook = new XSSFWorkbook(inputStream); 
            } catch (Exception ex) { 
            workBook = new HSSFWorkbook(inputStream); 
        }  
             
             
            for (int numSheet = 0; numSheet < workBook.getNumberOfSheets(); numSheet++) { 
                Sheet sheet = workBook.getSheetAt(numSheet); 
                if (sheet == null) { 
                    continue; 
                } 
                // 循环行Row 
                for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) { 
                    Row row = sheet.getRow(rowNum); 
                    if (row == null) { 
                        continue; 
                    } 
                     
                    // 循环列Cell 
                    ArrayList<String> arrCell =new ArrayList<String>(); 
                    for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) { 
                        Cell cell = row.getCell(cellNum); 
                        if (cell == null) { 
                            continue; 
                        } 
                        arrCell.add(getValue(cell)); 
                    } 
                    Row.add(arrCell); 
                } 
            } 
        } catch (IOException e) { 
            System.out.println("e:"+e); 
        } 
     
        return Row; 
    } 
 //解析你Excel里面的值
    private static String getValue(Cell cell) { 
     if (cell == null)
   return "";
  switch (cell.getCellType()) {
  case HSSFCell.CELL_TYPE_NUMERIC:
   if(HSSFDateUtil.isCellDateFormatted(cell)){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
   }
   return cell.getNumericCellValue()+"";
  case HSSFCell.CELL_TYPE_STRING:
   System.out.println(cell.getStringCellValue());
   return cell.getStringCellValue()+"";
  case HSSFCell.CELL_TYPE_FORMULA:
   return cell.getCellFormula()+"";
  case HSSFCell.CELL_TYPE_BLANK:
   return "";
  case HSSFCell.CELL_TYPE_BOOLEAN:
   return cell.getBooleanCellValue() + "";
  case HSSFCell.CELL_TYPE_ERROR:
   return cell.getErrorCellValue() + "";
  }
  return "";
    } 
}

你可能感兴趣的:(解析Excel一个工具类!)