通过poi实现解析并读取excel文件(包含xls、xlsx后缀)
StudentBean.java类,用来存放学生信息
package com.demoexcel.util;public class StudentBean {private String no; //学号private String name; //姓名private String age; //年龄private float score; //成绩public StudentBean() {super();}public StudentBean(String no, String name, String age, float score) {super();this.no = no;this.name = name;this.age = age;this.score = score;}public String getNo() {return no;}public void setNo(String no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public float getScore() {return score;}public void setScore(float score) {this.score = score;}
}
ReadExcel.java类,用来解析并读取excel文件
package com.demoexcel.util;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.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ReadExcel {/*** 读取excel中的数据* @param path* @return List<StudentBean>* @author zhang 2015-08-18 00:08*/public List<StudentBean> readExcel(String path) {if (path != null && !path.equals("")) {String ext = getExt(path);if (ext!=null && !ext.equals("")) {if (ext.equals("xls")) {return readXls(path);} else if (ext.equals("xlsx")) {return readXlsx(path);}}}return new ArrayList<StudentBean>();}/*** 读取后缀为xls的excel文件的数据* @param path* @return List<StudentBean>* @author zhang 2015-08-18 00:10*/private List<StudentBean> readXls(String path) {HSSFWorkbook hssfWorkbook = null;try {InputStream is = new FileInputStream(path);hssfWorkbook = new HSSFWorkbook(is);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}StudentBean studentBean = null;List<StudentBean> list = new ArrayList<StudentBean>();if (hssfWorkbook != null) {// Read the Sheetfor (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);if (hssfSheet == null) {continue;}// Read the Rowfor (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {HSSFRow hssfRow = hssfSheet.getRow(rowNum);if (hssfRow != null) {studentBean = new StudentBean();HSSFCell no = hssfRow.getCell(0);HSSFCell name = hssfRow.getCell(1);HSSFCell age = hssfRow.getCell(2);HSSFCell score = hssfRow.getCell(3);studentBean.setNo(getValue(no));studentBean.setName(getValue(name));studentBean.setAge(getValue(age));studentBean.setScore(Float.valueOf(getValue(score)));list.add(studentBean);}}}}return list;}/*** 读取后缀为xlsx的excel文件的数据* @param path* @return List<StudentBean>* @author zhang 2015-08-18 00:08*/private List<StudentBean> readXlsx(String path) {XSSFWorkbook xssfWorkbook = null;try {InputStream is = new FileInputStream(path);xssfWorkbook = new XSSFWorkbook(is);} catch (IOException e) {e.printStackTrace();}StudentBean studentBean = null;List<StudentBean> list = new ArrayList<StudentBean>();if(xssfWorkbook!=null){// Read the Sheetfor (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);if (xssfSheet == null) {continue;}// Read the Rowfor (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {XSSFRow xssfRow = xssfSheet.getRow(rowNum);if (xssfRow != null) {studentBean = new StudentBean();XSSFCell no = xssfRow.getCell(0);XSSFCell name = xssfRow.getCell(1);XSSFCell age = xssfRow.getCell(2);XSSFCell score = xssfRow.getCell(3);studentBean.setNo(getValue(no));studentBean.setName(getValue(name));studentBean.setAge(getValue(age));studentBean.setScore(Float.valueOf(getValue(score)));list.add(studentBean);}}}}return list;}/*** 获取文件扩展名* @param path* @return String* @author zhang 2015-08-17 23:26*/private String getExt(String path) {if (path == null || path.equals("") || !path.contains(".")) {return null;} else {return path.substring(path.lastIndexOf(".") + 1, path.length());}}/*** 判断后缀为xlsx的excel文件的数据类型* @param xssfRow* @return String* @author zhang 2015-08-18 00:12*/@SuppressWarnings("static-access")private String getValue(XSSFCell xssfRow) {if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {return String.valueOf(xssfRow.getBooleanCellValue());} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {return String.valueOf(xssfRow.getNumericCellValue());} else {return String.valueOf(xssfRow.getStringCellValue());}}/*** 判断后缀为xls的excel文件的数据类型* @param hssfCell* @return String* @author zhang 2015-08-18 00:12*/@SuppressWarnings("static-access")private String getValue(HSSFCell hssfCell) {if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {return String.valueOf(hssfCell.getBooleanCellValue());} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {return String.valueOf(hssfCell.getNumericCellValue());} else {return String.valueOf(hssfCell.getStringCellValue());}}public static void main(String[] args) {System.out.println(new ReadExcel().readExcel("D://student_info.xlsx").size());}
}
excel文件中数据形式:
以上代码经测试有效,先说明几个注意点:
1、文件名为student_info.xlsx,存放在D盘根目录下(这个可以自行更改,也可去测试xls后缀类型的文件)。
2、若直接测试的话,文件格式需与上面图片中的一致,若想按自己的需求则只需修改readXlsx(String path)和readXls(String path)方法中的少量代码即可,很容易看明白。
3、还需添加相应的jar包才行
通过poi实现解析并读取excel文件(包含xls、xlsx后缀)相关推荐
- python3读取excel文件(xls/xlsx)
第一种方法:打开Excel文件,另存为 .csv文件即可,利用读取csv的方式 第二种方法: 第一步: pip install pyexcel-xls 环境:python3.6 工具:pycharm2 ...
- python3 read excel,python3读取excel文件(xls/xlsx)
第一种方法:打开Excel文件,另存为 .csv文件便可,利用读取csv的方式html 第二种方法:python 第一步: pip install pyexcel-xls 环境:python3.6ap ...
- R读取excel文件乱码 read.xlsx() 解决方法
1. 参考[R语言]R读取含中文excel文件,read.xlsx乱码问题 该文章总结得很好,可以直接跳到最后看博主的总结. 2. 如果依旧是乱码那么用read.xlsx2()去读取excel文件, ...
- python处理excel的书_Python处理Excel文件(csv, xls, xlsx)
Excel文件格式主要有csv,xlsx和xlsx,对于不同的格式,我们使用不同的包来进行处理. 使用csv包处理csv文件 读取csv文件 import csv with open('./data. ...
- Java读取Excel文件汇总
相信很多程序猿朋友碰到上传.读取Excel文件的问题,做个小结,权当是笔记收着吧. 之前做金融报表,是把Excel内嵌到页面中的,技术上采用ZK结合POI读取和插入Excel数据,后台方法比较死,比较 ...
- java excel文件读取的内容_java读取Excel文件指定内容
--边学习边记录~ 最近需要用到从外部文件导入测试数据,因而上网查了一些读取excel文件这方面的代码,然后修改后适用于现有场景中(得到excel中指定单元格的内容). 导入的jar:poi-3.16 ...
- android读取excel文件_python里读写excel等数据文件的几种常用方式
python处理数据文件第一步是要读取数据,文件类型主要包括文本文件(csv.txt等).excel文件.数据库文件.api等. 下面整理下python有哪些方式可以读取数据文件. 1. python ...
- 使用libxl库读取excel文件
前言 在程序中读取excel文件比如XLS和XLSX,方法有很多,比如ADO,OLE,ODBC等方式.但是这些方法要么依赖于平台,要么读取速度慢,有的甚至需要电脑本身装有excel程序. 但是有一个付 ...
- python 读取Excel文件(包括后缀为.xls与.xlsx)
1.python 读取Excel文件(.xls文件) 导入的为xlrd 读取该sheet表中的值 import xlrd# 读取表格 def read_excel(book_data,sheet_da ...
最新文章
- thinkphp 框架自动加载原理_ThinkPHP5分析Part 1 基本框架流程
- Mysql数据库的安装教程
- mysql windows 安装_Windows 安装 Mysql(zip格式)
- 头像星球html,HTML5 Canvas 星球大战黑武士头像
- 1143 多少个Fibonacci数
- 实时获取滚动条的高度_适用于星上快速处理的雷达高度计有效波高反演技术
- Entity Framework 4.1 : 贪婪加载和延迟加载
- node.js 回调函数
- 概率软逻辑(PSL,Probabilistic soft logic)通用(可处理中文)版本
- Linux安装yum教程
- centos7服务器如何通过trunk模式接入交换机
- deepin系统引导_Deepin系统安装教程
- 1. 不吹不擂,第一篇就能提升你对Bean Validation数据校验的认知
- Gamemaker小课堂#0 如何为 Windows 游戏编写 DLL 扩展
- jQuery(javascript) 与Vue有什么区别
- 2022全国视力防控展,中国爱眼教育大会
- 学习使用js链接websocket服务断线重连的方法
- 扫描普通二维码跳转到微信小程序指定页面
- 编程:5 位评委对参赛选手进行打分,将所有的打分结果存储在对应类型的数组中,将所有评分结果 去除一个最低分,去除一个最高分,然后获取剩余 3 位评委的平均分数为选手的最终得分。设计程序, 用键盘输入
- 点开,看一段,你就会喜欢上学习pandas,你该这么学!No.3