POI读取excel工具类 返回实体bean集合(xls,xlsx通用)

本文举个简单的实例 读取上图的 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通用)的更多相关文章
- poi读取excel工具类
package com.manage.utils; import ch.qos.logback.core.net.SyslogOutputStream; import com.google.gson. ...
- POI读取excel工具类(xls,xlsx通用)
package com.boot.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotF ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- java里poi操作Excel工具类【我改】
参考原文: https://www.cnblogs.com/yizhang/p/7244917.html 我改: package test; import java.io.File; import j ...
- 使用POI导出EXCEL工具类并解决导出数据量大的问题
POI导出工具类 工作中常常会遇到一些图表需要导出的功能,在这里自己写了一个工具类方便以后使用(使用POI实现). 项目依赖 <dependency> <groupId>org ...
- 使用回调方式写POI导入excel工具类
场景是这样的:为了做一个excel导入的功能,为了尽可能的写一个通用的工具类,将与poi有关的东西都封装起来,以便以其他人员只用关心自己的业务,不用和poi打交道. 写到最后,现在还是会有poi的东西 ...
- POI生成Excel工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInp ...
- java poi读取excel公式,返回计算值(转)
http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi; 2 3 import o ...
- java操作excel 工具类
java操作excel 可参考https://blog.csdn.net/xunwei0303/article/details/53213130 直接上代码: 一.java生成excel文件: pac ...
随机推荐
- C# 并行任务——Parallel类
一.Parallel类 Parallel类提供了数据和任务的并行性: 二.Paraller.For() Paraller.For()方法类似于C#的for循环语句,也是多次执行一个任务.使用Paral ...
- CSS实现文字和图片的水平垂直居中
关于文字和图片的水平垂直居中,在前端界绝对算是一个老生常谈的问题了,尤其是垂直居中,什么千奇百怪的解法都能想的出来.下面我就总结一些比较常用的方法: 一.文本的水平垂直居中: 1.水平居中: 是不是很 ...
- STL的空间配置器std_alloc 笔记
STL的空间配置器std_alloc 笔记 C++的内存分配基本操作是 ::operator new(),内存释放是 ::operator delete(),这里个全局函数相当于C的malloc和fr ...
- ajax、PHP、session做购物车
购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- Cordova各个插件使用介绍系列(四)—canvas2ImagePlugin保存二维码到手机本地
详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-4-canvas2imageplugin/ 在前面几篇 ...
- 【解决】使用compass watch xxx.scss 失败
原始日期:2016-01-25 16:49 在上一篇博客,我们终于安装好了compass,不过紧接着使用compass watch app.scss 结果失败,经过查询资料,是compass的版本问题 ...
- Markdown: 编译pdf
在网上发布博文的时候希望能顺便在本地保存一份记录,这样总结的东西很多的时候就可以写成一本给自己看的小书了.在linux下面有两个选择latex和markdown,虽然latex非常强大,但是很少有博客 ...
- 打开vmvare出现The VMware Authorization Service is not running。
win+r再输入cmd打开doc,输入services.msc打开服务,吧Vmware Authorization Service 更改为自动或者将其启动即可.
- java--while、do while、for三种循环体
1.for可以记录执行次数: 2.while.do while的i放在sum的后面和for得到的执行次数和结果是一致的. 1.从执行结果来看,放在前面,虽然执行次数和i放在sum的后面是相同,但是结果 ...
- 懒人的小技巧, 批处理修改IP
相信很多人都有这样的麻烦, 工作单位的IP网段与住的不一致, 自己的笔记本在单位和回家的时候每次都要更改IP, 很麻烦, 偷个懒, 做了个批处理来修改IP,方便一点. 还有就是可以把工作的时候才需要 ...