使用 Aspose 读取和验证行 JAva

Reading and Validating Rows with Aspose JAva

我正在尝试使用 Aspose Cells 读取和验证行 Java,但我以前从未使用过 ir,而且文档不是很清楚,它甚至没有告诉你如何读取行.这是我到目前为止所做的:

public static void main(String[] args) {

    String dataDir = "C:\Users\apoac22836\Desktop\";
    Workbook wb = new Workbook(dataDir + "Example.xlsm");
    Worksheet worksheet = wb.getWorksheets().get("BOM");

    int column = 1;
    for (int i = 0; i < 40; i++) {

        Cell lastCell = worksheet.getCells().endCellInColumn((short) column);

        for (int row = 0; row <= lastCell.getRow(); row++) {
            Cell cell = worksheet.getCells().get(row, column);
            System.out.println(cell.getStringValue());
        }
        column += 1;

        System.out.println("------------------------------------------------");
    }
}

我按列显示单元格的值。

嗯,如果你需要有效地枚举范围、行、单元格中的所有数据(非空值),你可以尝试使用 Range.iterator()、RowCollection.iterator() 和Cells.iterator() 方法获取特定范围、已初始化行和非空或已初始化单元格的迭代器,请参阅以下示例代码供您参考: 例如 示例代码:

//Range Iterator.
Workbook book = new Workbook("sample.xlsx");
Worksheet sheet = book.getWorksheets().get(0);
Range range = sheet.getCells().getMaxDisplayRange();//You may also create your desired range (in the worksheet) using, e.g sheet.getCells().createRange("A1", "J11");
Iterator rangeIterator = range.iterator();
while(rangeIterator.hasNext())
{
Cell cell = (Cell)rangeIterator.next();
System.out.println(cell.getName() + " is not empty");
}

//Cells Iterator.
Workbook workbook = new Workbook("Book1.xls"); 
Worksheet sheet = workbook.getWorksheets().get(0);
Cells cells = sheet.getCells();
//Get the iterator from Cells collection
Iterator cellIterator = cells.iterator();
//Traverse cells in the collection
while (cellIterator.hasNext()) {
      Cell cell = (Cell) cellIterator.next();
      System.out.println(cell.getName() + " " + cell.getValue());
}

//Rows collection Iterator.
String filePath = "c:\source.xlsx";

Workbook workbook = new Workbook(filePath);

Worksheet worksheet = workbook.getWorksheets().get(0);

RowCollection rows =  worksheet.getCells().getRows();

Object obj = rows.iterator().next();

Iterator<Row> rowIterator = worksheet.getCells().getRows().iterator();

while(rowIterator.hasNext())

{

      Row r = rowIterator.next();

      Iterator<Cell> cellIterator = r.iterator();

      while(cellIterator.hasNext())

      {

            Cell cell= cellIterator.next();
            System.out.println(cell.getStringValue());

      }

}

希望,这有点帮助。

我在 Aspose 担任支持 developer/Evangelist。