java poi导入功能

package com.suning.mercury.service.mock.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.LIGHT_TURQUOISE;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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;

public class ImportExcelUtil {
private final static String excel2003L =”.xls”; //2003- 版本的excel
private final static String excel2007U =”.xlsx”; //2007+ 版本的excel

static SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static short[] yyyyMMdd = {14, 31, 57, 58, 179, 184, 185, 186, 187, 188};
static short[] HHmmss = {20, 32, 190, 191, 192};
static List<short[]> yyyyMMddList = Arrays.asList(yyyyMMdd);
static List<short[]> hhMMssList = Arrays.asList(HHmmss);

@SuppressWarnings("unused")
public static  List<List<String>> getBankListByExcel(InputStream in,String fileName) throws Exception{
    List<List<String>> list = null;
    //创建Excel工作薄
    Workbook work = getWorkbook(in,fileName);
    if(null == work){
        throw new Exception("创建Excel工作薄为空!");
    }

    Sheet sheet = null;
    Row row = null;
    Cell cell = null;
    list = new ArrayList<List<String>>();
    //遍历Excel中所有的sheet
    for (int i = 0; i < work.getNumberOfSheets(); i++) {
        sheet = work.getSheetAt(i);
        int totalCell = sheet.getRow(0).getPhysicalNumberOfCells();
        //遍历当前sheet中的所有行
        for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum()+1; j++) {
            row = sheet.getRow(j);
            if(row==null || validateRow(row) || row.getPhysicalNumberOfCells() < totalCell){continue;} //3个条件,有一个为true就不会往list里加,不仅过滤空行还过滤了列数不够的行,  
            //遍历所有的列
            List<String> li = new ArrayList<String>();
            for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                cell = row.getCell(y);
                li.add(getCellData(cell));
            }
            list.add(li);
        }
        break;

    }

// work.close();
return list;

}

private static boolean validateRow(Row row) throws Exception{
    if (row.getCell(0).getCellType() == Cell.CELL_TYPE_BLANK || "".equals(getCellData(row.getCell(0)))) {
        return true;
    }
    return false;//
}

public static  Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
    Workbook wb = null;
     String fileType = fileName.substring(fileName.lastIndexOf("."));  
    if(excel2003L.equals(fileType)){
        wb = new HSSFWorkbook(inStr);  //2003-
    }else if(excel2007U.equals(fileType)){
        wb = new XSSFWorkbook(inStr);  //2007+
    }else{
        throw new Exception("解析的文件格式有误!");
    }
    return wb;
}

/**
 * 获取单元中值(字符串类型)
 * @param cell
 * @return
 * @throws Exception
 */
public static String getCellData(Cell cell) throws Exception {
    String cellValue = "";
    if (cell != null) {
        try {
            switch (cell.getCellType()) {
             case Cell.CELL_TYPE_BLANK://空白
                 cellValue = "";
                 break;
             case Cell.CELL_TYPE_NUMERIC: //数值型 0----日期类型也是数值型的一种

                 if (DateUtil.isCellDateFormatted(cell)) {
                     short format = cell.getCellStyle().getDataFormat();
                     if (yyyyMMddList.contains(format)) {
                         sFormat = new SimpleDateFormat("yyyy-MM-dd");
                     }else if (hhMMssList.contains(format)) {
                         sFormat = new SimpleDateFormat("HH:mm:ss");
                     }
                     Date date = cell.getDateCellValue();
                     cellValue = sFormat.format(date);

                 }else{
                     cell.setCellType(Cell.CELL_TYPE_STRING);
                     cellValue = replaceBlank(cell.getStringCellValue());

                 }
                break;
             case Cell.CELL_TYPE_STRING: //字符串型 1
                 cellValue = replaceBlank(cell.getStringCellValue());
                 break;
             case Cell.CELL_TYPE_FORMULA: //公式型 2
                 cell.setCellType(Cell.CELL_TYPE_STRING);
                 cellValue = replaceBlank(cell.getStringCellValue());
                 break;
             case Cell.CELL_TYPE_BOOLEAN: //布尔型 4
                 cellValue = String.valueOf(cell.getBooleanCellValue());
                 break;

             case Cell.CELL_TYPE_ERROR: //错误 5
                 cellValue = "!#REF!";
                 break;
            }
        } catch (Exception e) {
            throw new Exception("读取Excel单元格数据出错:" + e.getMessage());
        }

    }
    return cellValue;

}

public static String replaceBlank(String source) {
    String dest = "";
    if (source != null) {
        Pattern p = Pattern.compile("\t|\r|\n");
        Matcher m = p.matcher(source);
        dest = m.replaceAll("");
    }
    return dest.trim();
}


public static void main(String[] args) throws Throwable {
    File f= new File("d:" + File.separator + "学生表.xlsx");
    InputStream input = new FileInputStream(f);      
    String fileName = "学生表";
    List<List<String>> list = getBankListByExcel(input,fileName+".xlsx");
    System.out.println("list="+list);
    String name ="";
    String age = list.get(1)+"";
    String addr = "";
    for(List list0:list){
        System.out.println(list0);
    }

}

}

猜你喜欢

转载自blog.csdn.net/shannon8/article/details/81288089