java 获取excel最后一行_查找Excel电子表格中的最后一行
确切知道的唯一方法是测试行 . 这是我用于同一问题的解决方案:
int lastRowIndex = -1;
if( sheet.getPhysicalNumberOfRows() > 0 )
{
// getLastRowNum() actually returns an index, not a row number
lastRowIndex = sheet.getLastRowNum();
// now, start at end of spreadsheet and work our way backwards until we find a row having data
for( ; lastRowIndex >= 0; lastRowIndex-- ){
Row row = sheet.getRow( lastRowIndex );
if( row != null ){
break;
}
}
}
注意:这不会检查看似空但不是的行,例如其中包含空字符串的单元格 . 为此,您需要一个更完整的解决方案,例如:
private int determineRowCount()
{
this.evaluator = workbook.getCreationHelper().createFormulaEvaluator();
this.formatter = new DataFormatter( true );
int lastRowIndex = -1;
if( sheet.getPhysicalNumberOfRows() > 0 )
{
// getLastRowNum() actually returns an index, not a row number
lastRowIndex = sheet.getLastRowNum();
// now, start at end of spreadsheet and work our way backwards until we find a row having data
for( ; lastRowIndex >= 0; lastRowIndex-- )
{
Row row = sheet.getRow( lastRowIndex );
if( !isRowEmpty( row ) )
{
break;
}
}
}
return lastRowIndex;
}
/**
* Determine whether a row is effectively completely empty - i.e. all cells either contain an empty string or nothing.
*/
private boolean isRowEmpty( Row row )
{
if( row == null ){
return true;
}
int cellCount = row.getLastCellNum() + 1;
for( int i = 0; i < cellCount; i++ ){
String cellValue = getCellValue( row, i );
if( cellValue != null && cellValue.length() > 0 ){
return false;
}
}
return true;
}
/**
* Get the effective value of a cell, formatted according to the formatting of the cell.
* If the cell contains a formula, it is evaluated first, then the result is formatted.
*
* @param row the row
* @param columnIndex the cell's column index
* @return the cell's value
*/
private String getCellValue( Row row, int columnIndex )
{
String cellValue;
Cell cell = row.getCell( columnIndex );
if( cell == null ){
// no data in this cell
cellValue = null;
}
else{
if( cell.getCellType() != Cell.CELL_TYPE_FORMULA ){
// cell has a value, so format it into a string
cellValue = this.formatter.formatCellValue( cell );
}
else {
// cell has a formula, so evaluate it
cellValue = this.formatter.formatCellValue( cell, this.evaluator );
}
}
return cellValue;
}
java 获取excel最后一行_查找Excel电子表格中的最后一行相关推荐
- java 获取端口号错误_尝试访问SPYDER中的pyspark时,获取错误“异常:Java网关进程在向驱动程序发送其端口号之前已退出”...
我想通过Spyder运行Pypark 这是完整的错误:Traceback (most recent call last): File "", line 1, in sc = Spa ...
- java获取web.xml 参数_解析web.xml中在Servlet中获取context-param和init-param内的参数
web.xml里面可以定义两种参数:1.application范围内的参数,存放在servletcontext中,在web.xml中配置如下: context/param avalible durin ...
- java获取excle表格对象_Java读取excel表格
Java读取excel表格 一般都是用poi技术去读取excel表格的,但是这个技术又是什么呢 什么是Apache POI? Apache POI是一种流行的API,它允许程序员使用Java程序创建, ...
- 怎么把好几行弄成一行_【excle 如何多行变一行】excel中怎么把多行同一个人的数据变成一行?...
excel中怎样实现多行数据变成一行数据 最简单的方法就是讲姓名2制(即成员)后在"本人"所,家庭成员1所在列做"选择性粘贴",然后选择"行列互换&q ...
- excel 溢出 修复_修复Excel条件格式重复规则
excel 溢出 修复 Conditional formatting is a great way to highlight specific data, but did you know that ...
- python相比于excel的优势_使用Excel和python来做回归分析
使用Excel和python来做回归分析 作者:PHPYuan 时间:2018-08-01 03:40:50 聊完方差分析,就不得不说回归分析. 回归分析是一种应用广泛的统计分析方法,在金融,医学等领 ...
- excel 拖动滚动条_使用Excel滚动条修剪圣诞树
excel 拖动滚动条 An Excel scroll bar can be used for practical (and sometimes boring) things, like testin ...
- excel自动筛选_在Excel中按选择自动筛选
excel自动筛选 In Excel 2003, you can add a couple of buttons to the toolbar to make it easy to filter a ...
- excel文件修复_修复Excel文件
excel文件修复 If you're having problems with an Excel file, using the built in repair feature might fix ...
最新文章
- Weex控制Android返回键解决方案
- 昂贵的聘礼(poj 1062)
- P3441-[POI2006]MET-Subway【图论,贪心】
- 中科大 计算机网络15 DNS域名解析系统
- 网络存储SAN网络存储术语解释
- word 文档 一次性设置多张图片大小
- 微服务架构-设计总结
- 眼图在通信系统中有什么意义_KT124煤矿调度通信系统和传统调度系统相比有什么优势...
- 中科大EPC课程爬取
- LINUX中信号量的使用
- 一篇了解TrustZone
- SQL 事务(Tran | Transation)与 Try...Catch 的使用
- Xilinx 7系列FPGA架构之时钟资源(一)
- Day2快捷键+简单Dos命令
- 2019年6月6日第十五周体育馆团体预约系统UML软件工程项目日志
- 计算机发展至今多少年,电子计算机的发展史年份
- css简单实现一类卡片(自用笔记)
- lintcode 778. 太平洋和大西洋的水流 dfs
- C# 深入单例(回笼)
- app爬虫反编译(一) 之反编译工具