java用poi读取Excel表格中的数据
Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版。
Apache POI 代码例子地址:http://poi.apache.org/spreadsheet/quick-guide.html
本例子可以读取Microsoft Office Excel 2003/2007/2010,具体代码及注释如下:
读取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook
读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
读取两种格式使用 import org.apache.poi.ss.usermodel.* 包的内容,例如:Workbook
引入包如下:
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 org.apache.poi.ss.usermodel.DateUtil;
【其中的DateUtil不是必须的】
/**
* 读取Excel测试,兼容 Excel 2003/2007/2010
*/
public String readExcel()
{
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
//同时支持Excel 2003、2007
File excelFile = new File("/home/zht/test.xls"); //创建文件对象
FileInputStream is = new FileInputStream(excelFile); //文件流
Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量
//遍历每个Sheet
for (int s = 0; s < sheetCount; s++) {
Sheet sheet = workbook.getSheetAt(s);
int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
//遍历每一行
for (int r = 0; r < rowCount; r++) {
Row row = sheet.getRow(r);
int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
//遍历每一列
for (int c = 0; c < cellCount; c++) {
Cell cell = row.getCell(c);
int cellType = cell.getCellType();
String cellValue = null;
switch(cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
if(DateUtil.isCellDateFormatted(cell)) {
cellValue = fmt.format(cell.getDateCellValue()); //日期型
}
else {
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
}
break;
case Cell.CELL_TYPE_BOOLEAN: //布尔型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK: //空白
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: //错误
cellValue = "错误";
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = "错误";
break;
default:
cellValue = "错误";
}
System.out.print(cellValue + " ");
}
System.out.println();
}
} }
catch (Exception e) {
e.printStackTrace();
} return Action.SUCCESS;
}
如果执行的代码的过程中报出如下错误:
poi导入excel表格数据时Cannot get a text value from a numeric cell
异常描述:在导入excel的时候在获取excel单元格数据的时候会出现Cannot get a text value from a numeric cell的异常抛出。
异常原因:poi读取excel单元格的数据,cell有不同的数据类型(CELL_TYPE_NUMERIC,CELL_TYPE_STRING,CELL_TYPE_FORMULA),如果cell中的数据是数值的话,如果你没有给他设置cell的类型的话。默认会认为是CELL_TYPE_NUMERICl类型,如果从一个NUMBER类型的Cell使用.cell.getStringCellValue()读取出一个字符串就会出错。
解决的方法:在读取数据之前,设置cell的类型为CELL_TYPE_STRING;
cell.setCellType(Cell.CELL_TYPE_STRING);
所以,上面的代码可以简单写成:
/**
* 读取Excel测试,兼容 Excel 2003/2007/2010
*/
public String readExcel()
{
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
//同时支持Excel 2003、2007
File excelFile = new File("/home/zht/test.xls"); //创建文件对象
FileInputStream is = new FileInputStream(excelFile); //文件流
Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量
//遍历每个Sheet
for (int s = 0; s < sheetCount; s++) {
Sheet sheet = workbook.getSheetAt(s);
int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
//遍历每一行
for (int r = 0; r < rowCount; r++) {
Row row = sheet.getRow(r);
int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
//遍历每一个单元格
for (int c = 0; c < cellCount; c++) {
Cell cell = row.getCell(c);
int cellType = cell.getCellType();
String cellValue = null; //在读取单元格内容前,设置所有单元格中内容都是字符串类型
cell.setCellType(Cell.CELL_TYPE_STRING); //按照字符串类型读取单元格内数据
cellValue = cell.getStringCellValue(); /*在这里可以对每个单元格中的值进行二次操作转化*/ System.out.print(cellValue + " ");
}
System.out.println();
}
} }
catch (Exception e) {
e.printStackTrace();
} return Action.SUCCESS;
}
项目中原来就有maven的相关poi依赖,具体如下:
maven依赖配置:
<!--poi-->
<dependency>
<groupId>fakepath</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14-20160307</version> <groupId>fakepath</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.14-20160307</version> <groupId>fakepath</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14-20160307</version> <groupId>fakepath</groupId>
<artifactId>poi-examples</artifactId>
<version>3.14-20160307</version> <groupId>fakepath</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.14-20160307</version> <groupId>fakepath</groupId>
<artifactId>poi</artifactId>
<version>3.14-20160307</version> <groupId>fakepath</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version> </dependency>
java用poi读取Excel表格中的数据的更多相关文章
- 利用java反射机制实现读取excel表格中的数据
如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.Lis ...
- python读取excel表格中的数据
使用python语言实现Excel 表格中的数据读取,需要用到xlrd.py模块,实现程序如下: import xlrd #导入xlrd模块 class ExcelData(): def __init ...
- java读取Excel表格中的数据
1.需求 用java代码读取hello.xls表格中的数据 2.hello.xls表格 3.java代码 package com.test; import java.io.File; import j ...
- 读取Excel表格中数据原型
写下这篇博客来记录自己的工作,这部分功能是读取Excel表格中的数据,并通过c#中的datagridview控件将读取的数据显示出来.为了方便用户,我设计了一个read按钮,用户点击这个按钮,会弹出打 ...
- java通过poi读取excel中的日期类型数据或自定义类型日期
Java 读取Excel表格日期类型数据的时候,读出来的是这样的 12-十月-2019,而Excel中输入的是 2019/10/12 或 2019-10-12 poi处理excel时,当excel没 ...
- JAVA使用POI读取EXCEL文件的简单model
一.JAVA使用POI读取EXCEL文件的简单model 1.所需要的jar commons-codec-1.10.jarcommons-logging-1.2.jarjunit-4.12.jarlo ...
- Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决
Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决 引言: 在Java中 ...
- 读取Excel表格日期类型数据的时候
用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel 2.Excel数据处理: Excel存储日期.时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化 1 ...
- Java实现POI读取Excel文件,兼容后缀名xls和xlsx
1.引入所需的jar包: maven管理项目的话直接添加以下坐标即可: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -- ...
随机推荐
- Mysql优化系列(1)--Innodb重要参数优化
1.简单介绍InnoDB给MySQL提供了具有提交,回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎.InnoDB锁定在行级并且也在SELECT语句提供一个Oracle风格一致的非锁定读.这些特色 ...
- Safecracker-HDU1015
题意 给你大写字母的字符串,A=1,...Z=26,以及target 问你是否有v - w^2 + x^3 - y^4 + z^5 = target 有输出字典序大的那个字符串 分析 dfs code ...
- 关于Runtime error
别人说的,但我感觉是因为你的操作是不符合语言规定的,让编译器无法识别,运行不出
- Daily Scrumming* 2015.12.19(Day 11)
一.团队scrum meeting照片 二.成员工作总结 姓名 任务ID 迁入记录 江昊 任务1090 https://github.com/buaaclubs-team/temp-front/com ...
- logstash 解析日志文件
input { file { path => "/usr/local/test/log.log" } } filter { grok { match => { &quo ...
- 小学四则运算APP 第二阶段冲刺-第五天
团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第二次冲刺阶段时间:11.29~12.09 本次发布的是判断题代码,已经实现部分功能,,但是美中不足的是判断错误 panduanset.j ...
- Java7和8在虚拟机上的差异:Perm Generation vs. Metaspace
- [转帖]Tensor是神马?为什么还会Flow?
Tensor是神马?为什么还会Flow? 互联网爱好者 百家号17-05-2310:03 大数据文摘作品,转载要求见文末 编译 | 邵胖胖,江凡,笪洁琼,Aileen 也许你已经下载了TensorFl ...
- FICO基础知识(二)
FI中的maser data: COA (Chart Of Account) 科目表 Account 科目 Vendor master dada 供应商主数据 Customer master da ...
- 基于C#.NET的高端智能化网络爬虫(一)(反爬虫哥必看)
前两天朋友发给我了一篇文章,是携程网反爬虫组的技术经理写的,大概讲的是如何用他的超高智商通过(挑衅.怜悯.嘲讽.猥琐)的方式来完美碾压爬虫开发者.今天我就先带大家开发一个最简单低端的爬虫,突破携程网超 ...