`
x10232
  • 浏览: 55428 次
  • 来自: 北京
社区版块
存档分类
最新评论

POI读取Excel时对cell中数据的判断

    博客分类:
  • Java
阅读更多
Iterate over cells, with control of missing / blank cells

In some cases, when iterating, you need full control over how missing or blank rows and cells are treated, and you need to ensure you visit every cell and not just those defined in the file. (The CellIterator will only return the cells defined in the file, which is largely those with values or stylings, but it depends on Excel).

In cases such as these, you should fetch the first and last column information for a row, then call getCell(int, MissingCellPolicy) to fetch the cell. Use a MissingCellPolicy to control how blank or null cells are handled.

    
// Decide which rows to process
    int rowStart = Math.min(15, sheet.getFirstRowNum());
    int rowEnd = Math.max(1400, sheet.getLastRowNum());

    for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
       Row r = sheet.getRow(rowNum);
       if (r == null) {
          // This whole row is empty
          // Handle it as needed
          continue;
       }

       int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);

       for (int cn = 0; cn < lastColumn; cn++) {
          Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
          if (c == null) {
             // The spreadsheet is empty in this cell
          } else {
             // Do something useful with the cell's contents
          }
       }
    }




Getting the cell contents

To get the contents of a cell, you first need to know what kind of cell it is (asking a string cell for its numeric contents will get you a NumberFormatException for example). So, you will want to switch on the cell's type, and then call the appropriate getter for that cell.

In the code below, we loop over every cell in one sheet, print out the cell's reference (eg A3), and then the cell's contents.
    // import org.apache.poi.ss.usermodel.*;

    Sheet sheet1 = wb.getSheetAt(0);
    for (Row row : sheet1) {
        for (Cell cell : row) {
            CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
            System.out.print(cellRef.formatAsString());
            System.out.print(" - ");

            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.println(cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.println(cell.getDateCellValue());
                    } else {
                        System.out.println(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.println(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    System.out.println(cell.getCellFormula());
                    break;
                default:
                    System.out.println();
            }
        }
    }
				
分享到:
评论

相关推荐

    Java POI读取excel中数值精度损失问题解决

    主要介绍了Java POI读取excel中数值精度损失问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    poi excel加密

    poi加密,可以只读或者输入密码,希望能帮助大家

    java使用poi在excel单元格添加超链接,设置字体颜色(csdn)————程序.pdf

    java使用poi在excel单元格添加超链接,设置字体颜色(csdn)————程序

    POI操作Excel常用方法总结.docx

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。 二、 HSSF概况 HSSF 是Horrible SpreadSheet Format的缩写,通过HSSF,你可以用纯Java代码来读取...

    Excel POI读取封装(文件+示范代码)

    Excel POI读取封装(文件+示范代码) package org.excel.service; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java....

    poi读取excel

    Excel操作类。 * 本类提供了简单的获取指定cell给指定cell添加内容的方法。

    java上传并读取excel工具类

    java上传并读取excel工具类 涉及到MultiFile转FIle 以及多个sheet页

    poi包对excel操作的教程

    Apache的Jakata项目的POI子项目,目前比较成熟的是HSSF接口,处理MSExcel对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。

    poi最新版本及收集的帮助资料

    我们可以在cell中设置各种类型的值。 尤其要注意的是如果你想正确的显示非欧美的字符时,尤其象中日韩这样的语言,必须设置编码为16位的即是HSSFCell.ENCODING_UTF_16,才能保证字符的高8位不被截断而引起编码失真...

    POI海量数据大数据文件生成SXSSFWorkbook使用简介.pdf

    POI海量数据⼤数据⽂件⽣成 海量数据⼤数据⽂件⽣成SXSSFWorkbook使⽤简介 使⽤简介 在之前我们知道处理xls的excel⽤的workbook是HSSFWorkbook,处理xlsx的excel⽤的是XSSFWorkbook。 上⾯两个类导出excel的时候数据...

    java excel 导入手机号码(包括对手机的验证)

    * 此代码是完成从excel导入电话号码,将正确的电话号码保存到set集合中,因为set集合对于重复的值会覆盖,所以达到了去重复的值的用例,并累计了不正确的电话号码的个数,对电话号码进行了验证有效性。所需要的 dom4...

    从Excel里面读出数据,并将数据存入数据库

    // 创建对Excel工作簿文件的引用 FileInputStream finput = new FileInputStream(excelPath); POIFSFileSystem fs = new POIFSFileSystem(finput); HSSFWorkbook wb = new HSSFWorkbook(fs);//...

    Apache的POI开发实例

    Apache的POI组件是Java操作Microsoft Office办公套件的强大API,其中对Word,Excel和PowperPoint都有支持,当然使用较多的还是Excel,因为Word和PowerPoint用程序动态操作的应用较少。那么本文就结合POI来介绍一下...

    excel-streaming-reader:使用Apache POI的流式Excel阅读器的易于使用的实现

    不幸的是,POI库中唯一可读取流工作簿的内容要求您的代码使用类似SAX的解析器。 该API缺少所有友好的类,如Row和Cell 。 该库用作该流API的包装,同时保留了标准POI API的语法。 继续阅读以了解它是否适​​合您。...

    poi初学者指南,java操作excel

    Apache 的Jakata 项目的POI 子项目,目前比较成熟的是HSSF 接口,处理MSExcel 对 象。它不象我们仅仅是用csv 生成的没有格式的可以由Excel 转换的东西,而是真正的Excel 对象,你可以控制一些属性如sheet,cell 等等...

    excel-streaming-reader

    不幸的是,POI库中唯一可读取流工作簿的内容要求您的代码使用类似SAX的解析器。 该API缺少所有友好的类,如Row和Cell 。 该库用作该流API的包装,同时保留了标准POI API的语法。 继续阅读以了解它是否适​​合您。...

    Java_Jxl开发.doc

    在开源世界中,有两套比较有影响的API可供使用,一个是POI,一个是jExcelAPI。 介绍 jxl操作excel包括对象Workbook,Sheet ,Cell。 一个excel就对应一个Workbook对象, 一个Workbook可以有多个Sheet对象 一个...

Global site tag (gtag.js) - Google Analytics