我正在尝试使用 JDK 1.4 和 Apache POI 3.2 读取 XLS(2007 格式)文件。我的文件中已经有几行是 TEXT 格式的(右键单击-> 格式化单元格-> 文本)。
我在文件中手动添加了几行,没有任何文本格式,假设新添加的行的格式为 RAW。
POI 无法读取后来添加的行。只有当新添加的行也格式化为 TEXT 时,它才能读取它们。
代码如下所示:
HSSFSheet sheet = workBook.getSheetAt(sheetno);
int rowStart = sheet.getFirstRowNum();
//int rowEnd = sheet.getLastRowNum();
int rowEnd = sheet.getPhysicalNumberOfRows();
for (int rowNum = rowStart; rowNum <= rowEnd; rowNum++)
{
HSSFRow row = (HSSFRow) sheet.getRow(rowNum);
Vector cellStoreVector=new Vector();
int current = 0, next = 1;
for (int colNum = row.getFirstCellNum(); colNum < row.getLastCellNum(); colNum++)
{
HSSFCell cell = (HSSFCell) row.getCell(colNum);
currentCell = cell.getColumnIndex();
current = cell.getColumnIndex();
if (current < next)
{
cellStoreVector.addElement(cell);
}
else
{
int loop = current - next;
for (int k = 0; k < loop + 1; k++)
{
cellStoreVector.addElement("");
next = next + 1;
}
}
next = next + 1;
}
}
到目前为止我尝试过的事情:
1) 使用 RowIterator 对象,然后使用 while 循环进行迭代。
Iterator rowIter = sheet.rowIterator();
while(rowIter.hasNext()) - Results in the same no. of rows being iterated.
2) 利用
sheet.getPhysicalNumberOfRows(); and
sheet.getLastRowNum();- Results in the same no. of rows being iterated.
以前有没有人遇到过与 Excel 文件类似的问题?任何帮助将不胜感激。
谢谢,阿里汉特