[POI-HSSF] 例示Logical Row/Column 和 Physical Row/Column 的区别

HSSF是处理Excel的API. 在这个API的语境中,Row/Column 有 "Logical" 和 "Physical" 的区别。

Logical和Physical分别是什么意思? 我不敢望文生义,去网上搜又搜不到,最后只好自己测试一下。 你看了下面的测试结果,就知道这两个东西的区别了:

totallyEmpty.xls (全空的Excel)

get Physical NumberOfRows:
0

get Logical FirstRowNum:
0

get Logical LastRowNum:
0

—-Let’s walk through the physical rows:
没有东西可遍历

—-Let’s walk through the logical rows:
没有东西可遍历

excelPath: row_2_4_6.xls (1、3、5行为空,2、4、6不空,1-based)

get Physical NumberOfRows:
3

get Logical FirstRowNum:
0

get Logical LastRowNum:
5

—-Let’s walk through the physical rows:
遍历出3个对象

    org.apache.poi.hssf.usermodel.HSSFRow@1b48197

    org.apache.poi.hssf.usermodel.HSSFRow@1a80a69

    org.apache.poi.hssf.usermodel.HSSFRow@14384c2

—-Let’s walk through the logical rows: 
遍历出5个对象

    null

    org.apache.poi.hssf.usermodel.HSSFRow@1b48197

    null

    org.apache.poi.hssf.usermodel.HSSFRow@1a80a69

    null

    org.apache.poi.hssf.usermodel.HSSFRow@14384c2

col_2_4_6.xls (首行的1、3、5列为空,2、4、6不为空,1-based)

get Physical Number Of Cells:
3

get Logical First Number Of Cells:
1

get Logical Last Number Of Cells:
5

———-Let’s walk through the physical cells:
遍历出3个对象

    cell2

    cell4

    cell6

———-Let’s walk through the logical cells:
遍历出5对象

    cell2

    null

    cell4

    null

    cell6

附:所调用的方法

—行相关:

get Physical NumberOfRows:    sheet.getPhysicalNumberOfRows()

get Logical FirstRowNum:     sheet.getFirstRowNum()

get Logical LastRowNum:    sheet.getLastRowNum()

the physical rows:

sheet.rowIterator()

the logical rows: 

for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++){}

–列相关

get Physical Number Of Cells: firstRow.getPhysicalNumberOfCells()

get Logical First Number Of Cells: firstRow.getFirstCellNum()

get Logical Last Number Of Cells: (firstRow.getLastCellNum() – 1) //要减1,很奇怪吧

the physical cells:

firstRow.cellIterator()

the logical cells: 

for (int i = firstRow.getFirstCellNum(); i <= firstRow.getLastCellNum() - 1; i++) {}

  

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.