获取Excel sheet页中实际有效行数(不是我写的,但是也不是我转载的)
来源:互联网 发布:中世纪2优化9兵种 编辑:程序博客网 时间:2024/06/05 02:56
private List<Integer> realNumberOfRow(HSSFSheet sheet){
int realNums = 0;
int firstNum = sheet.getFirstRowNum();
int lastNum = sheet.getLastRowNum();
List<Integer> number=new ArrayList<Integer>();//获取每个有值的行数下标
for (int i=firstNum;i<=lastNum;i++){
HSSFRow row = sheet.getRow(i);
int end = 0;
if(row!=null ){
end = row.getLastCellNum();
}/*else{//将为空的行删除
sheet.shiftRows(i+1, sheet.getLastRowNum()+1,-1);
continue;
}*/
boolean isNotBlankRow = false;
for(int j=0;j<end;j++){
HSSFCell cell=row.getCell(j);
//若有一个单元格不为空则此行非空
if(cell!=null){
int cellType=cell.getCellType();
String cellValue = null;
if (cellType == HSSFCell.CELL_TYPE_BLANK){//将为空的行删除
//sheet.shiftRows(i+1, sheet.getLastRowNum()+1,-1);
cellValue = "";
continue;
}else if(cellType == HSSFCell.CELL_TYPE_BOOLEAN){
cellValue = String.valueOf(cell.getBooleanCellValue());
}else if(cellType == HSSFCell.CELL_TYPE_ERROR){
cellValue = String.valueOf(cell.getErrorCellValue());
}else if(cellType == HSSFCell.CELL_TYPE_FORMULA){
cellValue = cell.getCellFormula();
}else if(cellType == HSSFCell.CELL_TYPE_NUMERIC){
DecimalFormat df = new DecimalFormat("#");
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellValue = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
// 如果是date类型则 ,获取该cell的date值
//value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();
} else { // 纯数字
cellValue = df.format(cell.getNumericCellValue());
}
//cellValue = String.valueOf(cell.getNumericCellValue());
}else if(cellType == HSSFCell.CELL_TYPE_STRING){
cellValue = cell.getStringCellValue();
}else{
}
if(!StringUtil.isBlank(cellValue)){
isNotBlankRow = true;
break;
}
}
}
if(isNotBlankRow){
number.add(i);
realNums += 1;
}
}
return number;
}
int realNums = 0;
int firstNum = sheet.getFirstRowNum();
int lastNum = sheet.getLastRowNum();
List<Integer> number=new ArrayList<Integer>();//获取每个有值的行数下标
for (int i=firstNum;i<=lastNum;i++){
HSSFRow row = sheet.getRow(i);
int end = 0;
if(row!=null ){
end = row.getLastCellNum();
}/*else{//将为空的行删除
sheet.shiftRows(i+1, sheet.getLastRowNum()+1,-1);
continue;
}*/
boolean isNotBlankRow = false;
for(int j=0;j<end;j++){
HSSFCell cell=row.getCell(j);
//若有一个单元格不为空则此行非空
if(cell!=null){
int cellType=cell.getCellType();
String cellValue = null;
if (cellType == HSSFCell.CELL_TYPE_BLANK){//将为空的行删除
//sheet.shiftRows(i+1, sheet.getLastRowNum()+1,-1);
cellValue = "";
continue;
}else if(cellType == HSSFCell.CELL_TYPE_BOOLEAN){
cellValue = String.valueOf(cell.getBooleanCellValue());
}else if(cellType == HSSFCell.CELL_TYPE_ERROR){
cellValue = String.valueOf(cell.getErrorCellValue());
}else if(cellType == HSSFCell.CELL_TYPE_FORMULA){
cellValue = cell.getCellFormula();
}else if(cellType == HSSFCell.CELL_TYPE_NUMERIC){
DecimalFormat df = new DecimalFormat("#");
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellValue = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
// 如果是date类型则 ,获取该cell的date值
//value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();
} else { // 纯数字
cellValue = df.format(cell.getNumericCellValue());
}
//cellValue = String.valueOf(cell.getNumericCellValue());
}else if(cellType == HSSFCell.CELL_TYPE_STRING){
cellValue = cell.getStringCellValue();
}else{
}
if(!StringUtil.isBlank(cellValue)){
isNotBlankRow = true;
break;
}
}
}
if(isNotBlankRow){
number.add(i);
realNums += 1;
}
}
return number;
}
阅读全文
0 0
- 获取Excel sheet页中实际有效行数(不是我写的,但是也不是我转载的)
- POI 解析excel时sheet的实际行数,数字问题
- 我的英语也不是很好,还不是照样学java编程,还不是照样写代码啊,献给每一个努力的小伙伴们!(雪豹软件工作室)
- 不是我写的原文
- 随机数的产生(觉得很有用,不是我写的~但是想分享给其他人)
- Nginx 502 Bad Gateway错误是FastCGI有问题,造成NGINX 502错误的可能性比较多 不是我想要的 但是写的挺好
- 知识不是原创的,但是文章确实是我一个字一个字敲出来的
- Excel 中 vbA 获得有效行数的方法
- 不是不委屈,但是我有更好的未来
- 但是你们却不是我的菜aa11
- 我考的不是研,是寂寞
- 叫醒我的不是闹钟,是梦想
- 我的性格不是怪,是INTJ
- 我什么也不是
- 老大让我写个表单验证的插件,市面上的表单验证插件不再少数。但是老大让我写肯定是想锻炼我
- IT人为什么拿不到高薪了[不是我写的哈,是转的]
- 【是她自己扑过来的,我也不愿意啊…
- 也不是每个腊鸡搬我帖,删我评论,我都会愤怒的
- 编译,反编译,混淆,【一】
- 我在学校网站一页面上无意间发现了一个漏洞
- 大数据学习2——大数据处理架构Hadoop概述
- 【Python】选择ipython替代python的理由
- 【Scikit-Learn 中文文档】无监督学习: 寻求数据表示
- 获取Excel sheet页中实际有效行数(不是我写的,但是也不是我转载的)
- Java中使用Jedis操作Redis
- 中间件kingshard入门(四):jmeter压测出现的问题too many open files
- vue加载时闪现模板的解决方法(例如加载数据之前出现item.name)
- 自定义View画五角星
- mt6735 CTS Google issue of android.holo
- MyBatis的Mapper接口以及Example的实例函数及详解
- java垃圾回收
- rabbitmq启用日志功能记录消息队列收发情况