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

关于解决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打印出出的内容 :

 

转载于:https://www.cnblogs.com/linbo3168/p/6510387.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值