码迷,mamicode.com
首页 > 其他好文 > 详细

POI读取excel工具类 返回实体bean集合

时间:2017-07-25 13:38:18      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:根据   set   lan   word   ooxml   orm   gen   pat   parse   

技术分享

本文举个简单的实例 读取上图的 excel文件到 List<User>集合

首先 导入POi 相关 jar包

在pom.xml 加入

        <!-- poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <!--poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>            

创建 user 实体 

技术分享

下面就是工具类

根据自己的需要更改实体对象即可

package com.boot.utils;

import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.ArrayList;  
import java.util.List;  
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;  
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.ss.usermodel.WorkbookFactory;  
  

import com.boot.entity.User;


public class ExcelReader {
    /**
     * 根据excal路径生成实体集合
     * @author Changhai
     * @data 2017-7-5
     * @param filePath
     * @return
     */
    public static List<?> getList(String filePath){
        InputStream is;
        try {
            is = new FileInputStream(filePath);
            return getList(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 根据输入流生成实体集合
     * @param is
     * @author Changhai
     * @data 2017-7-5
     * @return
     * @throws IOException
     */
    public static List<User> getList(InputStream is)
            throws IOException {
        List<List<String>> list = ExcelReader.readExcel(is);
        
        //-----------------------遍历数据到实体集合开始-----------------------------------
        List<User> listBean = new ArrayList<User>();
        for (int i = 1; i < list.size(); i++) {// i=1是因为第一行不要
            User uBean = new User();
            List<String> listStr = list.get(i);
            for (int j = 0; j < listStr.size(); j++) {
                switch(j){
                    case 0:uBean.setName(listStr.get(j));break;// 第一列
                    case 1:uBean.setPassword(listStr.get(j));break;// 第二列
                    case 2:uBean.setId(Integer.parseInt(listStr.get(j).substring(0,listStr.get(j).indexOf("."))));
                }
            }
            listBean.add(uBean);
        }
        //----------------------------遍历数据到实体集合结束----------------------------------
        return listBean;
    }

    /**
     * Excel读取 操作
     */
    public static List<List<String>> readExcel(InputStream is)
            throws IOException {
        Workbook wb = null;
        try {  
              wb = WorkbookFactory.create(is);        
            } catch (FileNotFoundException e) {  
              e.printStackTrace();  
            } catch (InvalidFormatException e) {  
              e.printStackTrace();  
            } catch (IOException e) {  
              e.printStackTrace();  
            }  

        /** 得到第一个sheet */
        Sheet sheet = wb.getSheetAt(0);
        /** 得到Excel的行数 */
        int totalRows = sheet.getPhysicalNumberOfRows();

        /** 得到Excel的列数 */
        int totalCells = 0;
        if (totalRows >= 1 && sheet.getRow(0) != null) {
            totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }

        List<List<String>> dataLst = new ArrayList<List<String>>();
        /** 循环Excel的行 */
        for (int r = 0; r < totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null)
                continue;
            List<String> rowLst = new ArrayList<String>();
            /** 循环Excel的列 */
            for (int c = 0; c < totalCells; c++) {
                Cell cell = row.getCell(c);
                String cellValue = "";
                if (null != cell) {
                    // 以下是判断数据的类型
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC: // 数字
                        cellValue = cell.getNumericCellValue() + "";
                        break;
                    case Cell.CELL_TYPE_STRING: // 字符串
                        cellValue = cell.getStringCellValue();
                        break;
                    case Cell.CELL_TYPE_BOOLEAN: // Boolean
                        cellValue = cell.getBooleanCellValue() + "";
                        break;
                    case Cell.CELL_TYPE_FORMULA: // 公式
                        cellValue = cell.getCellFormula() + "";
                        break;
                    case Cell.CELL_TYPE_BLANK: // 空值
                        cellValue = "";
                        break;
                    case Cell.CELL_TYPE_ERROR: // 故障
                        cellValue = "非法字符";
                        break;
                    default:
                        cellValue = "未知类型";
                        break;
                    }
                }
                rowLst.add(cellValue);
            }
            /** 保存第r行的第c列 */
            dataLst.add(rowLst);
        }
        return dataLst;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            //根据流
            InputStream is = new FileInputStream("d:\\user.xlsx");
            List<User> list = (List<User>) ExcelReader.getList(is);
            //根据文件路径
            //List<User> list = (List<User>) ExcelReader.getList("d:\\user.xlsx");
            for (int i = 0; i < list.size(); i++) {
                User cBean = list.get(i);
                System.out.println(cBean.getName()+ "~~" + cBean.getPassword() + "~~" + cBean.getId());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

少于150字不让上传,少于150字不让上传,少于150字不让上传,少于150字不让上传,少于150字不让上传,少于150字不让上传,少于150字不让上传,少于150字不让上传,少于150字不让上传,少于150字不让上传

POI读取excel工具类 返回实体bean集合

标签:根据   set   lan   word   ooxml   orm   gen   pat   parse   

原文地址:http://www.cnblogs.com/changhai/p/7233434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!