1. 程式人生 > >用poi讀寫Excel並匯入資料

用poi讀寫Excel並匯入資料

首先寫個工具類 用來讀寫Excel的

...


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
import org.springframework.web.multipart.MultipartFile;

import cn.smarthse.modules.platform.core.framework.bean.OssClientBean;
/**
 * excel讀寫工具類
 */
public class POIUtil {
	private static Logger logger  = Logger.getLogger(POIUtil.class);
	
	//讀取excel
    public static Workbook readExcel(String filePath,OssClientBean ossclient){
        Workbook wb = null;
        
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = ossclient.getInputStreamByUrl(filePath);
            if(".xls".equals(extString)){
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else{
                return wb = null;
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
    
    //得到格式化後的資料
    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if(cell!=null){
            //判斷cell型別
            switch(cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC:{
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            }
            case Cell.CELL_TYPE_FORMULA:{
                //判斷cell是否為日期格式
                if(DateUtil.isCellDateFormatted(cell)){
                    //轉換為日期格式YYYY-mm-dd
                    cellValue = cell.getDateCellValue();
                }else{
                    //數字
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_STRING:{
                cellValue = cell.getRichStringCellValue().getString();
                break;
            }
            default:
                cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue;
    }
}

接下來開始寫主體裡的東西啦!

filePath = ossclient.getUrl(filePath);
		 	Workbook wb =null;
	        Sheet sheet = null;
	        Row row = null;
	        List<Map<String,String>> list = null;
	        String cellData = null;
	        String columns[] = {"fileNo","status","department","releaseDate","executeDate","cancelDate","replaceRelationship","remake","lawClassName","projName","xx","xx1"};
	        wb = readExcel(filePath,ossclient);
	        if(wb != null){
	            //用來存放表中資料
	            list = new ArrayList<Map<String,String>>();
	            //獲取第一個sheet
	            sheet = wb.getSheetAt(0);
	            //獲取最大行數
	            int rownum = sheet.getPhysicalNumberOfRows();
	            //獲取第一行
	            row = sheet.getRow(0);
	            //獲取最大列數
	            int colnum = row.getPhysicalNumberOfCells();
	            for (int i = 1; i<rownum; i++) {
	                Map<String,String> map = new LinkedHashMap<String,String>();
	                row = sheet.getRow(i);
	                if(row !=null){
	                    for (int j=0;j<colnum;j++){
	                        cellData = (String) getCellFormatValue(row.getCell(j));
	                        map.put(columns[j], cellData);
	                    }
	                }else{
	                    break;
	                }
	                list.add(map);
	            }
	        }

這裡面的filePath 其實就是一個絕對路徑

list裡面的map就是每行的資料

獲取到map裡面的資料後 想必大家也會了  能做各種校驗  最後將正確資料匯入到資料庫