我正在尝试使用 Apache\\'s POI for Java 在 Excel 电子表格中查找最后一行的索引。
我认为使用
另一个问题是我不能只查找第一个空行,因为在有效数据行之间可能有空行。
那么有没有办法找到最后一行的索引呢?我想我可以要求数据之间没有空行,但我希望有更好的解决方案。
编辑:对于使用迭代器的记录没有帮助。它只是遍历了 1140/1162 假定的行。
我使用 poi-3.6-20091214 和一个
2 3 4 | Workbook book = new HSSFWorkbook(myxls); Sheet sheet = book.getSheetAt(0); System.out.println(sheet.getLastRowNum()); |
输出:
您可以使用以下方法获取原始行数。
2 | int rowsNum = worksheet.getPhysicalNumberOfRows(); |
我以前也遇到过同样的问题。这可能是由于 Excel 单元格已被编辑然后在 Excel 中清空所致。一旦它们被触摸,它们就会显示为使用过的单元格。
我使用这个技巧来删除(不仅仅是清空)那些单元格,并获得正确的返回行值:
这不是 POI 库的问题。
确定的唯一方法是测试行。这是我用于相同问题的解决方案:
2 3 4 5 6 7 8 9 10 11 12 13 14 | if( sheet.getPhysicalNumberOfRows() > 0 ) { // getLastRowNum() actually returns an index, not a row number lastRowIndex = sheet.getLastRowNum(); // now, start at end of spreadsheet and work our way backwards until we find a row having data for( ; lastRowIndex >= 0; lastRowIndex-- ){ Row row = sheet.getRow( lastRowIndex ); if( row != null ){ break; } } } |
注意:这不会检查看似为空但实际上不是空的行,例如其中包含空字符串的单元格。为此,您需要一个更完整的解决方案,例如:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | { this.evaluator = workbook.getCreationHelper().createFormulaEvaluator(); this.formatter = new DataFormatter( true ); int lastRowIndex = -1; if( sheet.getPhysicalNumberOfRows() > 0 ) { // getLastRowNum() actually returns an index, not a row number lastRowIndex = sheet.getLastRowNum(); // now, start at end of spreadsheet and work our way backwards until we find a row having data for( ; lastRowIndex >= 0; lastRowIndex-- ) { Row row = sheet.getRow( lastRowIndex ); if( !isRowEmpty( row ) ) { break; } } } return lastRowIndex; } /** * Determine whether a row is effectively completely empty - i.e. all cells either contain an empty string or nothing. */ private boolean isRowEmpty( Row row ) { if( row == null ){ return true; } int cellCount = row.getLastCellNum() + 1; for( int i = 0; i < cellCount; i++ ){ String cellValue = getCellValue( row, i ); if( cellValue != null && cellValue.length() > 0 ){ return false; } } return true; } /** * Get the effective value of a cell, formatted according to the formatting of the cell. * If the cell contains a formula, it is evaluated first, then the result is formatted. * * @param row the row * @param columnIndex the cell's column index * @return the cell's value */ private String getCellValue( Row row, int columnIndex ) { String cellValue; Cell cell = row.getCell( columnIndex ); if( cell == null ){ // no data in this cell cellValue = null; } else{ if( cell.getCellType() != Cell.CELL_TYPE_FORMULA ){ // cell has a value, so format it into a string cellValue = this.formatter.formatCellValue( cell ); } else { // cell has a formula, so evaluate it cellValue = this.formatter.formatCellValue( cell, this.evaluator ); } } return cellValue; } |
我知道如何使用 VBA 解决您的问题,但我不确定如何从 Apache POI 界面获取等效信息。在 VBA 中,要获取工作表"Sheet1"中使用的单元格范围,请使用:
这将返回一个
同样,我不确定这是否可以通过 POI API 访问,但如果不能,也许它提供了一种执行任意 VBA 片段的方法?
使用迭代器不会返回空行和未使用的行
2 3 4 5 6 7 8 | while (itr.hasNext()) { Row row = itr.next(); //your code here } |
对我来说,在任何情况下都没有任何效果,因为它适用于 HSSFWorkbook,但不适用于 XSSFWorkbook。
最后在解决方法的帮助下,我能够解决这个问题。
通过在工作表末尾合并两列或两行(在您的内容完成后)。
然后写下面的代码。
这里 0 只是我合并的一种情况,但如果您已经合并了单元格或行,则相应地增加您的值。
希望这会有所帮助。
您可以通过以下代码做到这一点:
2 | lastRowNum = model.getRowCount(); |
但是,我试图在 Apache POI 3.7 中执行此操作,但在 API 中找不到
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.