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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章