- 浏览: 784992 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (360)
- Java (101)
- JPA/Hibernate (10)
- Spring (14)
- Flex/BlazeDS (37)
- Database (30)
- Lucene/Solr/Nutch (0)
- Maven/Ant (25)
- CXF/WebService (3)
- RPC/RMI/SOAP/WSDL (1)
- REST (6)
- TDD/BDD/JUnit (1)
- Servlet/JSP (2)
- AI/MachineLearning (3)
- Resource (1)
- 字符编码 (2)
- OOA/OOPS/UML (5)
- DesignPattern (8)
- 算法与数据结构 (11)
- Web&App Server (13)
- 并发&异步&无阻塞 (7)
- Entertainment (4)
- JavaScript/ExtJS (45)
- CodeStyle&Quality (1)
- svn/git/perforce (8)
- JSON (2)
- JavaScriptTesting (4)
- Others (6)
- RegularExpression (2)
- Linux/Windows (12)
- Protocal (2)
- Celebrities (1)
- Interview (1)
- 计算机语言 (1)
- English (2)
- Eclipse (5)
- TimeZone/时区 (1)
- Finance (1)
- 信息安全 (1)
- JMS/MQ (2)
- XSD/XML/DTD (3)
- Android (4)
- 投资 (3)
- Distribution (3)
- Excel (1)
最新评论
-
qdujunjie:
如果把m换成具体的数字,比如4或者5,会让读者更明白
m阶B树中“阶”的含义 -
java-admin:
不错,加油,多写点文章
关于Extjs的mixins和plugin -
xiehuaidong880827:
你好,我用sencha cmd打包完本地工程后,把app.js ...
ExtJS使用Sencha Cmd合并javascript文件为一个文件 -
KIWIFLY:
lwpan 写道inverse = "true&qu ...
Hibernate中什么时候使用inverse=true -
luedipiaofeng:
good
消除IE stop running this script弹出框
参考文档:http://poi.apache.org/spreadsheet/quick-guide.html#Iterator
请注意MissingCellPolicy定义了如何处理缺失的单元格/空单元格:
程序中通过Row.RETURN_NULL_AND_BLANK访问。
这里有一tricky的地方,如果用CellIterator的方式,当碰到空(没有定义,没有编辑过)的单元格时,CellIterator.next()读取的是空单元格右边有内容的单元格,也就是说跳过了空的单元格,如果你的程序是跟位置有关系的话,这就会出错了。比如下面的代码就会跳过空的单元格,
Iterate over rows and cells
Sometimes, you'd like to just iterate over all the rows in a sheet, or all the cells in a row. This is possible with a simple for loop.
Luckily, this is very easy. Row defines a CellIterator inner class to handle iterating over the cells (get one with a call to row.cellIterator()), and Sheet provides a rowIterator() method to give an iterator over all the rows. These implement the java.lang.Iterable interface to allow foreach loops.
这种方式本质就是使用CellIterator,跟上面的例子一样会跳过没有定义过的单元格
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.
所以如果你需要处理所有的单元格,即不管该单元格有没有定义,有没有内容,你的程序都会依赖这些单元格,那么你应该使用普通的for循环配合getRow(), getCell()方法和MissingCellPolicy策略来循环整个Excel文件的单元格。
请注意MissingCellPolicy定义了如何处理缺失的单元格/空单元格:
/** Missing cells are returned as null, Blank cells are returned as normal */ public static final MissingCellPolicy RETURN_NULL_AND_BLANK = new MissingCellPolicy(); /** Missing cells are returned as null, as are blank cells */ public static final MissingCellPolicy RETURN_BLANK_AS_NULL = new MissingCellPolicy(); /** A new, blank cell is created for missing cells. Blank cells are returned as normal */ public static final MissingCellPolicy CREATE_NULL_AS_BLANK = new MissingCellPolicy();
程序中通过Row.RETURN_NULL_AND_BLANK访问。
这里有一tricky的地方,如果用CellIterator的方式,当碰到空(没有定义,没有编辑过)的单元格时,CellIterator.next()读取的是空单元格右边有内容的单元格,也就是说跳过了空的单元格,如果你的程序是跟位置有关系的话,这就会出错了。比如下面的代码就会跳过空的单元格,
while (rowIterator.hasNext()) { row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: case Cell.CELL_TYPE_STRING: //your logic ... break; } } }
Iterate over rows and cells
Sometimes, you'd like to just iterate over all the rows in a sheet, or all the cells in a row. This is possible with a simple for loop.
Luckily, this is very easy. Row defines a CellIterator inner class to handle iterating over the cells (get one with a call to row.cellIterator()), and Sheet provides a rowIterator() method to give an iterator over all the rows. These implement the java.lang.Iterable interface to allow foreach loops.
这种方式本质就是使用CellIterator,跟上面的例子一样会跳过没有定义过的单元格
Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { // Do something here } }
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); 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 } } }
所以如果你需要处理所有的单元格,即不管该单元格有没有定义,有没有内容,你的程序都会依赖这些单元格,那么你应该使用普通的for循环配合getRow(), getCell()方法和MissingCellPolicy策略来循环整个Excel文件的单元格。
发表评论
-
sapjco3 notes
2019-03-21 14:51 1101sapjco https://support.sap.com/ ... -
使用RestTemplate发送post JSON请求
2019-01-12 17:30 4592private final String BASE_URL = ... -
使用RestTemplate发送post JSON请求
2019-01-12 17:30 3398private final String BASE_URL = ... -
Spring线程池ThreadPoolTaskExecutor
2018-08-06 09:51 1787<!-- spring thread pool ex ... -
Spring注解事物@Transactional不工作
2018-08-02 18:50 2550“In proxy mode (which is the de ... -
创建前缀索引报长度超出错误
2018-07-25 15:44 1658表结构定义如下: CREATE TABLE `sku` ( ` ... -
Mysql Varchar字符长度
2018-07-25 15:23 1305`sku_name` VARCHAR(200) NOT NUL ... -
使用 Spring RestTemplate 发送 post 请求
2018-07-23 18:49 11643注意点: 1)使用MultiValueMap设置入参,不要使 ... -
Java动态代理Dynamic Proxy
2018-07-21 16:33 734JAVA学习篇--静态代理VS动态代理 https://blo ... -
分布式实时日志分析解决方案 ELK 部署架构
2018-07-20 09:52 1148原文链接:http://www.importn ... -
为什么HashMap容量一定要为2的幂呢?
2018-07-19 10:07 1663原文链接:https://blog.csdn.net/wang ... -
为什么计算HashCode时通常选择31这个数?
2018-07-19 10:05 1341摘自http://www.importnew.com/2208 ... -
jackson自定义序列化和反序列化
2018-07-10 18:47 2187原文链接:https://blog.csdn.net/liu ... -
Pay special attention when modifying online running system
2017-06-23 10:25 0Never remove any properties, me ... -
Map中的Null key, Null Value
2017-06-14 10:52 1897ConcurrentHashMap的key和value都不能为 ... -
Java语法糖
2017-06-05 20:03 485Java语法糖之foreach http://www.imp ... -
Java集合相关
2017-05-24 17:55 0Java集合框架:ArrayList http://www. ... -
Java数据类型的转换:隐式(自动)转换与强制转换
2017-05-14 10:46 0http://blog.csdn.net/u011240877 ... -
分布式开放消息系统(RocketMQ)的原理与实践
2017-05-07 19:55 683分布式开放消息系统(RocketMQ)的原理与实践 http ... -
面试知识点复习(Interview knowledge review)
2017-05-07 18:39 0JVM,多线程相关知识 http://darrenzhu.it ...
相关推荐
该工具类是poi基于事件驱动去读取海量excel数据,解决内存溢出以及空单元格的问题。 该压缩包里面有两个文件,TestExcel.java主要是原始的处理方法,操作简单,适合小数据量的读取。 ExampleEventUserModelUtil.java...
将excel文件中的内容通过java文件读取出来,包括总行数,列数,单元格内容,合并单元格,行高,列宽,图片等信息。
poi获取exccel有合并单元格的数据,存入List
Java操纵POI解析Excel表,根据表头将内容储存。通过实体类set、get方便其它方法引用
使用Java使用POI读取Excel,并且解决了合并单元格的读取
poi导入/导出Excel表格,合并单元格的读取和设置
ExcelDemo_Excel导出_下载_POI读excel_JXL读写excel_java读写excel_列宽_读取合并或拆分单元格内容
poi读取合并单元格帮助类
压缩包里面有poi相关的jar包,以及我在使用poi时候导入jar包遇到问题总结和一个小demo。
基于新版本的POI编写的读取Excel文件数据的工具类,可根据绝对路径、File对象、InputSteam对象读取解析Excel文件内容,并返回List<List<String>>格式结果,其中包含对单元格公式的处理。
主要介绍了Java POI读取excel中数值精度损失问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java用poi读取excel文件 。利用单元格对象可以获得它的值,提取单元格的值时请注意它的类型:
这个是使用java语言 poi导入excel到数据库中,同时使用了xml对excel的每个单元格数据进行了非空验证。经测试,导入数据库成功。
用poi方法读取excel中单元格内容的源代码
java POI完整示例,POI将word转HTML,数据库倒出数据到Excel等
POI excel单元格顔色设定
本POI实战为本人在业余时间写成,从浅开始,整个文档处于初到中级水平,包含大部分项目开发中会用到的常用功能,由于本人精力与能力有限,如有错误欢迎指正。 目录大概: 1.POI入门 4 1.1 Excel基本知识 4 1.2 POI...
poi读取大量数据会造成gc内存溢出的报错,由于垃圾回收机制无法将大量的对象及时的回收,而这些对象又会保存在内存中,会导致内存不够用的情况,这时候我们就需要使用新的方法,读取为cvs即可.此解决方案可支持千万数据的...
Excel POI读取封装(文件+示范代码) package org.excel.service; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java....
java实现在Excel中插入图片有时碰到下载excel中加入图片或已经制作好的图章。