导入jar 包

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

//准备数据类

package com.wf.zhang.test;

public class Person {

    private String name;

    private Integer age;

    private String Adress;

    public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getAdress() {
return Adress;
} public void setAdress(String adress) {
Adress = adress;
} public Person() {
} public Person(String name, Integer age, String adress) {
super();
this.name = name;
this.age = age;
Adress = adress;
} @Override
public String toString() {
return String.format("Person [name=%s, age=%s, Adress=%s]", name, age, Adress);
} }

Person

方式一    方法好多过时的

效果

package com.wf.zhang.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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; public class POIUtil { //按照路径 列名读取文件
public static List<Map<String, String>> readExcel(String filePath, String columns[]) {
Sheet sheet = null;
Row row = null;
Row rowHeader = null;
List<Map<String, String>> list = null;
String cellData = null;
Workbook wb = null;
if (filePath == null) {
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(filePath);
if (".xls".equals(extString)) {
wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(extString)) {
wb = new XSSFWorkbook(is);
} else {
wb = null;
}
if (wb != null) {
// 用来存放表中数据
list = new ArrayList<Map<String, String>>();
// 获取第一个sheet
sheet = wb.getSheetAt(0);
// 获取最大行数
int rownum = sheet.getPhysicalNumberOfRows();
// 获取第一行
rowHeader = sheet.getRow(0);
row = sheet.getRow(0);
// 获取最大列数
int colnum = row.getPhysicalNumberOfCells();
for (int i = 1; i < rownum; i++) {
Map<String, String> map = new LinkedHashMap<String, String>();
row = sheet.getRow(i);
if (row != null) {
for (int j = 0; j < colnum; j++) {
if (columns[j].equals(getCellFormatValue(rowHeader.getCell(j)))) {
cellData = (String) getCellFormatValue(row.getCell(j));
map.put(columns[j], cellData);
}
}
} else {
break;
}
list.add(map);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
} /**
* 获取单个单元格数据
* @param cell
* @return
*/
public static Object getCellFormatValue(Cell cell) {
Object cellValue = null;
if (cell != null) {
// 判断cell类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: {
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA: {
// 判断cell是否为日期格式
if (DateUtil.isCellDateFormatted(cell)) {
// 转换为日期格式YYYY-mm-dd
cellValue = cell.getDateCellValue();
} else {
// 数字
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
case Cell.CELL_TYPE_STRING: {
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
} else {
cellValue = "";
}
return cellValue;
} //测试类
public static void main(String[] args) {
String filePath = "C:/Users/admin/Desktop/students.xls";
String columns[] = { "学号", "姓名", "年龄", "生日" };
List<Map<String, String>> list = POIUtil.readExcel(filePath, columns);
// 遍历解析出来的list
for (Map<String, String> map : list) {
for (Entry<String, String> entry : map.entrySet()) {
System.out.print(entry.getKey() + ":" + entry.getValue() + ",");
}
System.out.println();
}
} }

方式二    推荐使用

效果

package com.wf.zhang.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row; public class ReadExcel { //调用方法
public static void main(String[] args) {
importXLS();
} //读取的方法
public static List<Student> importXLS(){ ArrayList<Student> list = new ArrayList<Student>(); try {
//1、获取文件输入流
InputStream inputStream = new FileInputStream("C:/Users/admin/Desktop/students.xls");
//2、获取Excel工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
//3、得到Excel工作表对象
HSSFSheet sheetAt = workbook.getSheetAt(0);
//4、循环读取表格数据
for (Row row : sheetAt) {
//首行(即表头)不读取
if (row.getRowNum() == 0) {
continue;
} //读取当前行中单元格数据,索引从0开始
int id = (int) (row.getCell(0).getNumericCellValue());
String name = row.getCell(1).getStringCellValue();
int age = (int) row.getCell(2).getNumericCellValue();
String birth = row.getCell(3).getStringCellValue(); Date date = DateUtil.parseYYYYMMDDDate(birth); Student st = new Student();
st.setId(id);
st.setName(name);
st.setAge(age);
st.setBirth(date); list.add(st);
}
//打印
System.out.println(list.toString());
//5、关闭流
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
} }

用POI 3.17读取EXCEL数据的更多相关文章

  1. jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL

    这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...

  2. java的poi技术读取Excel数据

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  3. java的poi技术读取Excel数据到MySQL

    这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...

  4. Java POI读取Excel数据,将数据写入到Excel表格

    1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...

  5. 使用poi读取excel数据示例

    使用poi读取excel数据示例 分两种情况: 一种读取指定单元格的值 另一种是读取整行的值 依赖包: <dependency> <groupId>org.apache.poi ...

  6. poi——读取excel数据

    单元格类型 读取Excel数据 package com.java.test.poi; import java.io.File; import java.io.FileInputStream; impo ...

  7. JAVA反射机制示例,读取excel数据映射到JAVA对象中

    import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; import ja ...

  8. Delphi中使用python脚本读取Excel数据

    Delphi中使用python脚本读取Excel数据2007-10-18 17:28:22标签:Delphi Excel python原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 . ...

  9. Java读取Excel数据

    Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 下图是excel文件的路径和文件名 下图是exce ...

随机推荐

  1. Python爬虫根据关键词爬取知网论文摘要并保存到数据库中【入门必学】

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:崩坏的芝麻 由于实验室需要一些语料做研究,语料要求是知网上的论文摘要 ...

  2. 用.NET模拟天体运动

    用.NET模拟天体运动 这将是一篇罕见而偏极客的文章. 我上大学时就见过一些模拟太阳系等天体运动的软件和网站,觉得非常酷炫,比如这个(http://www.astronoo.com/en/articl ...

  3. ARTS-S centos修改hostname

    hostnamectl set-hostname newhostname 重启

  4. HDFS的架构和设计要点

    HDFS的架构和设计要点 转 大数据之路 发布于 2012/10/11 23:00 字数 4487 阅读 495 收藏 1 点赞 0 评论 0 撸了今年阿里.头条和美团的面试,我有一个重要发现.... ...

  5. centos7 启动停止命令

    apache启动systemctl start httpd停止systemctl stop httpd重启systemctl restart httpd mysql启动systemctl start ...

  6. oracle管理角色和权限

    介绍 这一部分主要看看oracle中如何管理权限和角色,权限和角色的区别在哪里. 当刚刚建立用户时,用户没有任何权限,也不能执行任何操作.如果要执行某种特定的数据库操作,则必需为其授予系统的权限:如果 ...

  7. VMware Fushion解决:与vmmon模块的版本不匹配: 需要385.0,现有330.0。

    可以按下列步骤解决: 1. 退出VMware fusion2. 打开[终端]3. 执行命令:sudo rm -rf /System/Library/Extensions/vmmon.kext ,根据提 ...

  8. 【ES6基础】let、const命令和变量的结构赋值

    ES5声明变量(2):var .function ES6声明变量(6):var.function.let.const.import和class 1.let命令和const命令 (1)let和const ...

  9. CSS | 自适应两栏布局方法

    html代码: <div class="main"> <div class="left" style="background: #0 ...

  10. win10: 搭建FTP服务器

    新建用户,可以设置多个用户,给予不同的权限 ftp创建完成后,新用户创建完成后,我们回到计算机管理-Internet Information Services(IIS)管理器来管理我们的FTP站点,点 ...