关于解决java读取excel文件遇空行抛空指针的问题 !

package exceRead;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcleReadNullRow {
public static void getType(XSSFCell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getRichStringCellValue().getString() + "|");
// System.out.print("|");
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(String.valueOf(cell.getDateCellValue()) + "|");
} else {
System.out.print(cell.getNumericCellValue() + "|");
}
// System.out.print("|");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "|");
// System.out.print("|");
break;
default:
} } public static void main(String[] args) throws FileNotFoundException, IOException { ExcleUtil excle = new ExcleUtil();
String value = "";
String filePath = "C:\\Users\\ty\\Documents\\工作簿1.xlsx";
File file = new File(filePath);
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));
XSSFSheet sheet = wb.getSheetAt(0);
int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum();
int rowcount1 = sheet.getPhysicalNumberOfRows();
System.out.println(rowcount1);
System.out.println("该excle的总行数为:" + (rowcount + 1) + "行 !"); /*
* int rows = sheet.getLastRowNum(); for(int i = 0; i < rows+1; i++){ //
* List<String> rowData = new ArrayList<String>(); Row row =
* sheet.getRow(i); if(row != null){ int cols = row.getLastCellNum();
* for(int j = 0; j < cols; j++){ XSSFCell cell = (XSSFCell)
* row.getCell(j); ExcleReadNullRow.getType(cell); } //
* System.out.println();
*
* }
*/ for (int i = 0; i < rowcount + 1; i++) {
Row row = sheet.getRow(i);
if (row == null) {
System.out.println("this row is null ----------------------");
continue; } else {
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
XSSFCell cell = (XSSFCell) row.getCell(j);
System.out.print("| ");
if (cell != null) {
ExcleReadNullRow.getType(cell);
} else { continue; }
}
System.out.println(); } } }
}

excle文件中的内容 :

java打印出出的内容 :

关于解决java读取excel文件遇空行抛空指针的问题 !的更多相关文章

  1. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

  2. java读取excel文件的代码

    如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...

  3. JAVA 读取excel文件成List<Entity>

    package com.fsinfo.common.utils; import com.fsinfo.modules.enterprise.entity.EnterpriseRecordEntity; ...

  4. java 读取Excel文件并数据持久化方法Demo

    import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util ...

  5. java读取excel文件数据导入mysql数据库

    这是我来公司的第二周的一个小学习任务,下面是实现过程: 1.建立maven工程(方便管理jar包) 在pom.xml导入 jxl,mysql-connector 依赖 可以在maven仓库搜索 2.建 ...

  6. JAVA读取EXCEL文件异常Unable to recognize OLE stream

    异常: jxl.read.biff.BiffException: Unable to recognize OLE stream at jxl.read.biff.CompoundFile.<in ...

  7. Java 读取Excel 文件内容

    在一个项目中,有一个需求,是把excel文件的内容转换为xml格式展示.在学习如何操作的过程中,首先是如何获取excel文件,其中操作的代码如下: 1.首先是导入需要的 jar, 下载地址:https ...

  8. java读取excel文件内容

    1.导入依赖JAR包 <!-- jxl 操作excel --> <dependency> <groupId>org.jxls</groupId> < ...

  9. Java 读取excel 文件 Unable to recognize OLE stream 错误

    原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls).

随机推荐

  1. Springboot启动报Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered.

    解决方法: 属于bean重复,根据错误提示剔除多于的Bean引用!

  2. Linux入门进阶第五天——用户管理(帐号管理 )上

    一.帐号与群组 关于使用者帐号: 用户的ID与帐号信息所在位置是 /etc/passwd,而管理密码的数据则是在 /etc/shadow 每个登陆的使用者至少都会取得两个 ID , 一个是使用者 ID ...

  3. Java使用POI导出excel(下)——实例与小技巧

    [更新]:thinkgem的导出工具类: /** * Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesit ...

  4. Freemarker入门(一)——入门与基本概述

    1.概述 1.文档 完美的中文文档:http://freemarker.foofun.cn/ 2.是什么 FreeMarker是一个用Java语言编写的模板引擎.它基于模板来生成文本输出.Freema ...

  5. 20155325 2016-2017-2 《Java程序设计》第十周学习总结

    教材学习内容总结 Java视频笔记 强制转换 运算符 获取特定位数的值 循环 switch(不能判断布尔型) int x = 3, y = 3, z = 3; int n = 0; switch (x ...

  6. python 多线程笔记(1)-- 概念

    本文对不使用线程和使用线程做了一个对比. 假设有两件事情:听歌.看电影 一.不用线程 import time songs = ['爱情买卖','朋友','回家过年','好日子'] movies = [ ...

  7. SpringBoot-08:SpringBoot采用json的方式实现前后台通用的配置文件

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 一.需求: 本篇博客是最近笔者做的一个项目,已经上线但是还在不断开发,有些页面上的配置,测试服务器和正式服务器 ...

  8. [css 实践篇]CSS中的尺寸单位

    绝对单位 px: Pixel 像素 pt: Points 磅 pc: Picas 派卡 in: Inches 英寸 mm: Millimeter 毫米 cm: Centimeter 厘米 q: Qua ...

  9. Vue学习计划基础笔记(六) - 组件基础

    组件基础 目标: 掌握组件的构建方式 掌握如何复用组件.父子组件如何传值.如何向父组件发送消息 掌握如何通过插槽分发内容 了解解析dom模板时的注意事项 了解动态组件 组件 组件理解起来大概上就和ph ...

  10. 【python 3.6】python读取json数据存入MySQL(一)

    整体思路: 1,读取json文件 2,将数据格式化为dict,取出key,创建数据库表头 3,取出dict的value,组装成sql语句,循环执行 4,执行SQL语句 #python 3.6 # -* ...