java簡單易懂execl上傳工具類

package com.demo.Utils;

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;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/*
 * @author:崔小白
 */
public class sscExecl {

    //總行數
    private int totalRows = 0;
    //總條數
    private int totalCells = 0;
    //構造方法
    public sscExecl(){}
    //獲取總行數
    public int getTotalRows()  { return totalRows;}
    //獲取總列數
    public int getTotalCells() {  return totalCells;}

    /**
     * 讀EXCEL文件,獲取信息集合
     * @param file
     * @return  list
     */
    public List<實體類> getExcelInfo(MultipartFile file) throws IOException {

        //初始化信息的集合
        List<實體類> list=new ArrayList<實體類>();
        //初始化輸入流
        InputStream is = null;
        Workbook wb = null;
        try{
            is = file.getInputStream();
            //根據excel內容讀取信息
            wb = new XSSFWorkbook(is);
            //讀取Excel信息
            list=readExcelValue(wb);
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        } finally{
            if(is !=null)
            {
                try{
                    is.close();
                }catch(IOException e){
                    is = null;
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    /**
     * 讀取Excel信息
     * @param wb
     * @return list
     */
    private List<實體類> readExcelValue(Workbook wb){
        //得到第一個shell
        Sheet sheet=wb.getSheetAt(0);

        //得到Excel的行數
        this.totalRows=sheet.getPhysicalNumberOfRows();

        //得到Excel的列數(前提是有行數)
        if(totalRows>=1 && sheet.getRow(0) != null){//判斷行數大於一,並且第一行必須有標題
            this.totalCells=sheet.getRow(0).getPhysicalNumberOfCells();
        }else{
            return null;
        }

        List<實體類> list=new ArrayList<實體類>();//聲明一個對象集合
        實體類 lists;//再次聲明一個對象存儲數據

        //循環Excel行數,從第二行開始。
        for(int r=1;r<totalRows;r++){
            Row row = sheet.getRow(r);
            if (row == null) continue;
            lists = new 實體類();

            //循環Excel的列
            for(int c = 0; c <this.totalCells; c++){
                Cell cell = row.getCell(c);
                if (null != cell){
                    if(c==0){
                        lists.setName(String.valueOf(cell));//得到行中第一個值
                    }else if(c==1){
                        lists.setProfile(String.valueOf(cell));//得到行中第二個值
                    }else if(c==2){
                        lists.setMotto(String.valueOf(cell));//得到行中第三個值
                    }else if(c==3){
                        lists.setAddr(String.valueOf(cell));//得到行中第三個值
                    }else if(c==4){
                        lists.setEmail(String.valueOf(cell));//得到行中第三個值
                    }else if(c==5){
                        lists.setWebsite(String.valueOf(cell));//得到行中第三個值
                    }
                }
            }
            //添加對象到集合中
            list.add(lists);
        }
        return list;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章