gpt4 book ai didi

java - 使用 Aspose JAva 读取和验证行

转载 作者:行者123 更新时间:2023-12-02 12:16:40 25 4
gpt4 key购买 nike

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

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 担任支持开发人员/传播者。

关于java - 使用 Aspose JAva 读取和验证行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46120215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com