本文举个简单的实例 读取上图的 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 = ; i < list.size(); i++) {// i=1是因为第一行不要
User uBean = new User();
List<String> listStr = list.get(i);
for (int j = ; j < listStr.size(); j++) {
switch(j){
case :uBean.setName(listStr.get(j));break;// 第一列
case :uBean.setPassword(listStr.get(j));break;// 第二列
case :uBean.setId(Integer.parseInt(listStr.get(j).substring(,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();
/** 得到Excel的行数 */
int totalRows = sheet.getPhysicalNumberOfRows(); /** 得到Excel的列数 */
int totalCells = ;
if (totalRows >= && sheet.getRow() != null) {
totalCells = sheet.getRow().getPhysicalNumberOfCells();
} List<List<String>> dataLst = new ArrayList<List<String>>();
/** 循环Excel的行 */
for (int r = ; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null)
continue;
List<String> rowLst = new ArrayList<String>();
/** 循环Excel的列 */
for (int c = ; c < totalCells; c++) {
Cell cell = row.getCell(c);
String cellValue = "";
if (null != cell) {

              HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
              cellValue= hSSFDataFormatter.formatCellValue(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 = ; 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集合(xls,xlsx通用)的更多相关文章

  1. poi读取excel工具类

    package com.manage.utils; import ch.qos.logback.core.net.SyslogOutputStream; import com.google.gson. ...

  2. POI读取excel工具类(xls,xlsx通用)

    package com.boot.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotF ...

  3. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  4. java里poi操作Excel工具类【我改】

    参考原文: https://www.cnblogs.com/yizhang/p/7244917.html 我改: package test; import java.io.File; import j ...

  5. 使用POI导出EXCEL工具类并解决导出数据量大的问题

    POI导出工具类 工作中常常会遇到一些图表需要导出的功能,在这里自己写了一个工具类方便以后使用(使用POI实现). 项目依赖 <dependency> <groupId>org ...

  6. 使用回调方式写POI导入excel工具类

    场景是这样的:为了做一个excel导入的功能,为了尽可能的写一个通用的工具类,将与poi有关的东西都封装起来,以便以其他人员只用关心自己的业务,不用和poi打交道. 写到最后,现在还是会有poi的东西 ...

  7. POI生成Excel工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInp ...

  8. java poi读取excel公式,返回计算值(转)

    http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi;  2  3  import o ...

  9. java操作excel 工具类

    java操作excel 可参考https://blog.csdn.net/xunwei0303/article/details/53213130 直接上代码: 一.java生成excel文件: pac ...

随机推荐

  1. Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect

    Spring所有的切面和通知器都必须放在一个<aop:config>内(可以配置包含多个<aop:config>元素),每个<aop:config>包含pointc ...

  2. Thrift中required和optional

    最近在搞Thrift,对其字段声明中的required关键字有所误解,仔细调试了一下才明白其真实含义. required的意思不是说声明对象时,必须填这个值,而是Thrift在传输(序列化)过程中无论 ...

  3. [Linux] PHP程序员玩转Linux系列-腾讯云硬盘扩容挂载

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...

  4. cron的用法

    linux中的Cron命令是Linux的内置服务,用于定时的循环的服务. 1.启动.重启.关闭这个服务: /sbin/service crond start //启动服务 /sbin/service ...

  5. [0] TFS 分支/标签

    比较常见的版本控制分支策略有三种:不稳定主干策略.稳定主干策略.敏捷发布策略. 下面是对这几种策略的摘录: 不稳定主干策略 使用用主干作为新功能开发主线,分支用作发布. 被广泛的应用于开源项目. 比较 ...

  6. docker私有库搭建过程(Registry)

    实验环境: CentOS7 1611 Docker 1.12.6 registry   2.0 1.安装并运行registry 安装: [root@docker01 ~]# docker pull r ...

  7. Intellij IDEA查看方法的调用栈

    在IDEA中,先双击选定要查看的方法,使用快捷键Ctrl+Alt+h,在右侧就会显示该方法的详细信息,再双击右侧的方法,就定位到方法的代码区.如下图:

  8. 基于邮件系统的远程实时监控系统的实现 Python版

    人生苦短,我用Python~ 界内的Python宣传标语,对Python而言,这是种标榜,实际上,Python确实是当下最好用的开发语言之一. 在相继学习了C++/C#/Java之后,接触Python ...

  9. c# MySqlHelper_ExecuteSqlTran

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;u ...

  10. Ajax 向后台提交一个 JavaScript 对象数组?

    var postArray= new Array(); var temp = new Object(); temp.id='1'; temp.name='test'; postArray.push(t ...