获取单元格范围

可能的使用场景

当您只需要操作工作表上的一些数据时,您需要知道整个工作表的数据范围。Aspose.Cells提供了这一功能。Aspose.Cells提供以下属性和方法来帮助您实现您的目标。

使用Aspose.Cells获取单元格范围

此示例演示如何:

  1. 创建一个工作簿。
  2. 在第一个工作表中的单元格中添加数据。
  3. 获取单元格 Range
//Instantiating an Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the newly added worksheet
Worksheet ws = workbook.getWorksheets().get(0);
Cells cells = ws.getCells();
//Setting the value to the cells
Cell cell = cells.get("A1");
cell.putValue("Fruit");
cell = cells.get("B1");
cell.putValue("Count");
cell = cells.get("C1");
cell.putValue("Price");
cell = cells.get("A2");
cell.putValue("Apple");
cell = cells.get("A3");
cell.putValue("Mango");
cell = cells.get("A4");
cell.putValue("Blackberry");
cell = cells.get("A5");
cell.putValue("Cherry");
cell = cells.get("B2");
cell.putValue(5);
cell = cells.get("B3");
cell.putValue(3);
cell = cells.get("B4");
cell.putValue(6);
cell = cells.get("B5");
cell.putValue(4);
cell = cells.get("C2");
cell.putValue(5);
cell = cells.get("C3");
cell.putValue(20);
cell = cells.get("C4");
cell.putValue(30);
cell = cells.get("C5");
cell.putValue(60);
cell = cells.get("E10");
Style temp = workbook.createStyle();
temp.getFont().setColor(Color.getRed());
cell.setStyle(temp);
// Get max display range of worksheet
Range range = cells.getMaxDisplayRange();
//get maximum row index of cell which contains data.
System.out.println(cells.getMaxRow());
//get maximum row index of cell which contains data or style.
System.out.println(cells.getMaxDataRow());
//get maximum column index of cell which contains data.
System.out.println(cells.getMaxColumn());
//get maximum column index of cell which contains data or style.
System.out.println(cells.getMaxDataColumn());