我的c:驱动器(本地计算机)中有一个名为abc.xls的excel文件,现在在第一张工作表本身的excel文件中有一个表格,如下所示,该表格可以在工作表的任何范围内,我开发了下面的java程序,它将首先按行扫描整个工作表,然后按列扫描整个工作表,并将找到其中存在TradeRef的单元格
TradeRef TMS Deal Date
12 45 DRT 23/97/2014
23 36 QWE 21/07/2015
现在我下面的程序中的问题是它捕获了TradeRef所在的单元格,然后在列上进行迭代,然后以类似的方式捕获下一行并在列上进行迭代
但是我要应用的逻辑是,当它捕获TradeRef单元并遍历各列并到达表的最后一列(即上表中的Date)时,它应该进一步扫描同一行中的下20列如果在接下来的20列中没有任何值的单元格,那么它应该移至下一行;如果在20列之内,它认为任何单元格都可以有值,那么在这种情况下,它应该读取该单元格的值
所以就像
TradeRef TMS Deal Date
12 45 DRT 23/97/2014
23 36 QWE 21/07/2015
所以请建议如何实现上面的逻辑,即扫描下面一行中的后20列是我以前的实现,即
public class AAA {
public static void main(String[] args) throws IOException {
FileInputStream file = null ;
try {
file = new FileInputStream(new File("C:\\abc.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet firstSheet = workbook.getSheetAt(0);
Iterator iterator = firstSheet.iterator();
Cell c = findFirstRow(firstSheet);
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
file.close();
}
}
public static Cell findFirstRow(HSSFSheet firstSheet) {
for (Row row : firstSheet) {
for (Cell cell : row) {
cell.setCellType(cell.CELL_TYPE_STRING);
if ("TradeRef".equals(cell.getStringCellValue())) {
int row1 = cell.getRowIndex() + 1;
int col = cell.getColumnIndex();
if (firstSheet.getRow(row1) == null)
throw new RuntimeException("Row " + row1 + 1 + " is empty!");
Cell startOfFirstDataRow = firstSheet.getRow(row1).getCell(col);
if (startOfFirstDataRow == null) {
CellReference ref = new CellReference(row1, col);
throw new RuntimeException("Data not found at " + ref.formatAsString());
}
return startOfFirstDataRow;
}
}
}
throw new RuntimeException("TradingRef header cell not found!");
}
}
所以请告知我如何实现上述扫描下20列的逻辑