依赖包

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml 支持2007以上版本-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version>
</dependency>

版本问题

  1. 必须使用poi-ooxml依赖包来支持2007和2010版本的Excel。
    否则会报错:org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
  2. 必须使用3.9版本以上poi-ooxml依赖包
    否则会报错:java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException

ExcelUtils.java

示例代码参考了Click here并做了修改

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.lang3.StringUtils;
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 ExcelUtils {/*** suffix of excel 2003*/public static final String OFFICE_EXCEL_V2003_SUFFIX = "xls";/*** suffix of excel 2007*/public static final String OFFICE_EXCEL_V2007_SUFFIX = "xlsx";/*** suffix of excel 2010*/public static final String OFFICE_EXCEL_V2010_SUFFIX = "xlsx";public static final String EMPTY = "";public static final String DOT = ".";public static final String LIB_PATH = "lib";public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + "/student_info" + DOT + OFFICE_EXCEL_V2003_SUFFIX;public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + "/student_info" + DOT + OFFICE_EXCEL_V2007_SUFFIX;public static final String NOT_EXCEL_FILE = " is Not a Excel file!";public static final String PROCESSING = "Processing...";public static void main(String[] args) throws IOException {try {List<Student> list = readExcel("C:\\Users\\Administrator\\Desktop\\excel.xlsx");System.out.println(list);} catch (Exception e) {e.printStackTrace();}}/*** Check which version of The Excel file is. Throw exception if Excel file path is illegal.* * @param path  the Excel file * @return a list that contains Students from Excel.* @throws IOException*/public static List<Student> readExcel(String path) throws IOException, IllegalArgumentException {if (StringUtils.isBlank(path)) {throw new IllegalArgumentException(path + " excel file path is either null or empty");} else {String suffiex = getSuffiex(path);if(StringUtils.isBlank(suffiex)){throw new IllegalArgumentException(path + " suffiex is either null or empty");}if (OFFICE_EXCEL_V2003_SUFFIX.equals(suffiex)) {return readXls(path);} else if (OFFICE_EXCEL_V2007_SUFFIX.equals(suffiex)) {return readXlsx(path);} else if (OFFICE_EXCEL_V2010_SUFFIX.equals(suffiex)) {return readXlsx(path);} else {throw new IllegalArgumentException(path + NOT_EXCEL_FILE);}}}/*** Read the Excel 2017 or 2010* @param path the path of the excel file* @return* @throws IOException*/public static List<Student> readXlsx(String path) throws IOException {System.out.println(PROCESSING + path);InputStream is = new FileInputStream(path);XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);Student student = null;List<Student> list = new ArrayList<Student>();// 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) {student = new Student();XSSFCell no = xssfRow.getCell(0);XSSFCell name = xssfRow.getCell(1);XSSFCell age = xssfRow.getCell(2);XSSFCell score = xssfRow.getCell(3);student.setNo(getValue(no));student.setName(getValue(name));student.setAge(getValue(age));student.setScore(Float.valueOf(getValue(score)));list.add(student);}}}return list;}/*** Read the Excel 2003* @param path the path of the Excel* @return* @throws IOException*/public static List<Student> readXls(String path) throws IOException {System.out.println(PROCESSING + path);InputStream is = new FileInputStream(path);HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);Student student = null;List<Student> list = new ArrayList<Student>();// 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) {student = new Student();HSSFCell no = hssfRow.getCell(0);HSSFCell name = hssfRow.getCell(1);HSSFCell age = hssfRow.getCell(2);HSSFCell score = hssfRow.getCell(3);student.setNo(getValue(no));student.setName(getValue(name));student.setAge(getValue(age));student.setScore(Float.valueOf(getValue(score)));list.add(student);}}}return list;}@SuppressWarnings("static-access")private static 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());}}@SuppressWarnings("static-access")private static 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 String getSuffiex(String path) {if(StringUtils.isBlank(path)){return EMPTY;}int index = path.lastIndexOf(DOT);if (index == -1) {return EMPTY;}return path.substring(index + 1, path.length());}}class Student {/*** id   */private Integer id;/*** 学号*/private String no;/*** 姓名*/private String name;/*** 学院*/private String age;/*** 成绩*/private float score;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}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;}}

读取Excel工具类ExcelUtils相关推荐

  1. Java基于POI读取Excel工具类

    为什么为封装此工具类? 由于公司供应链部门业务需要,对Excel处理这块有较为严苛的要求.为了提高开发效率,从实际项目出发封装了通用自定义读取Excel工具类. 功能概述 支持读取全部excel数据 ...

  2. poi读取excel工具类

    1.添加依赖 <dependency><groupId>org.apache.poi</groupId><artifactId>poi</arti ...

  3. Java中 POI读取Excel工具类

    直接上代码 pom文件需要导入这些依赖 org.apache.poi poi-ooxml 3.9 org.apache.poi poi-ooxml-schemas 3.9 org.apache.poi ...

  4. Excel工具类--下载

    1.使用Excel工具类ExcelUtils,导包 2.前端代码,一个普通a标签 3.后端代码 ToolUtil.getExcelTitle 是额外提供的工具类,来处理不同浏览器下的乱码问题 Exce ...

  5. 操作Excel工具类:ExcelUtils.java

    项目中,时常会需要导入导出Excel的需求,因此我专门花时间设计了工具类. 所需依赖 <!-- 处理xls或xlsx格式的Excel表格导入导出的依赖 --><dependency& ...

  6. Java解析Excel工具类(兼容xls和xlsx)

    依赖jar <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml< ...

  7. java 兼容excel_Java解析Excel工具类(兼容xls和xlsx)

    依赖jar org.apache.poi poi-ooxml 4.0.1 ExcelUtils.java package javax.utils; import java.io.File; impor ...

  8. Java导入Excel工具类使用教程

    前言: 本工具类提供了Excel导入功能,通过反射机制将Excel中数据映射到实体类中,从而获取Excel数据,工具类依赖org.apache.poi包.支持RESTful API,支持Spring ...

  9. 导出excel工具类

    1.excel工具类代码如下 package com.spring.excel.utils; import org.apache.poi.hssf.usermodel.*; import org.ap ...

最新文章

  1. 数据结构源码笔记(C语言):基数排序
  2. Windows内核启动开关/3GB和win10下boot.ini文件问题
  3. JAVA进阶教学之(一维数组)
  4. mysql 动态创建事件_mysql 通过事件定时为数据库创建动态表名
  5. Gibbs Sampling\吉布斯采样(二)
  6. Linux评分脚本,linux必看脚本大全
  7. 腾讯地图api使用——地图选点自动定位到当前位置
  8. 应届生前端上班很吃力怎么办?
  9. mac 不显示 外接屏幕_苹果电脑外接显示器显示不出来 - 卡饭网
  10. 每个程序员都应该了解的内存知识(2)-CPU caches
  11. arcgis怎么压缩tif文件_使用gdal压缩tif文件
  12. 什么是前端渲染和后端渲染和SPA页面
  13. 【arduino】arduino家族,arduino相关各种开发环境汇总,Mixly米思齐最新python开发环境...
  14. 一流管理者,都不会选“先做再说”
  15. MySQL like模糊匹配是否走索引
  16. R中 %in% 运算符取反
  17. iPhoneX安全区域与H5引发的问题(Safe Area)
  18. 【ZooKeeper】大数据之分布式协调神器:ZooKeeper选举
  19. 基于JAVA宠物领养系统计算机毕业设计源码+系统+lw文档+部署
  20. 【CET4四级英语】【单词】英语四级高频词汇(3)

热门文章

  1. Discriminative Feature Learning for Unsupervised Video Summarization(论文翻译)
  2. 按如下函数原型编程从键盘输入一个m行n列的二维数组,然后计算数组中元素的最大值及其所在的行列下标值。其中,m和n的值由用户键盘输入。已知m和n的值都不超过10。
  3. 设计一个排序和查找系统。能够实现对给定的一组学生的借书证信息(如:卡号、姓名、系别、班号等)进行排序和查找。
  4. C语言怎样判断乘法越界,如何判断C语言算术运算的越界问题
  5. cad注释比例和打印比例不一样_cad注释比例(cad注释比例与打印比例)
  6. 大数据专业毕业后前景如何?能做什么职位?
  7. 量化金融论文:Astock
  8. 日期之 显示法定格式的日期
  9. 视频分辨率、帧率和码率三者之间关系详解
  10. leetcode day 2 【1905. 统计子岛屿】 BFS/DFS