获取工作表中的最大范围

获取最大范围

在 Aspose.Cells 中,如果已经初始化了 rowcolumn 对象,即使空行或空列中没有数据,这些行和列将被计算到最大区域中。

Workbook workbook = new Workbook("Book1.xlsx");
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.Worksheets;
Worksheet sheet = worksheets[0];
//Gets the max data range.
int maxRow = sheet.Cells.MaxRow;
int maxColumn = sheet.Cells.MaxColumn;
//The range is A1:B3.
Range range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1);
sheet.Cells["A10"].PutValue(null);
maxRow = sheet.Cells.MaxRow;
maxColumn = sheet.Cells.MaxColumn;
//The range is udpated to A1:B10.
range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1);

获取最大数据范围

在大多数情况下,我们只需要获取包含所有数据的所有范围,即使范围外的空单元格被格式化。 关于形状、表格和数据透视表的设置将被忽略。

// Instantiate a new Workbook.
Workbook workbook = new Workbook("Book1.xlsx");
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.Worksheets;
Worksheet sheet = worksheets[0];
//Gets the max data range.
int maxRow = sheet.Cells.MaxDataRow;
int maxColumn = sheet.Cells.MaxDataColumn;
//The range is A1:B3.
Range range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1);
sheet.Cells["A10"].PutValue(null);
maxRow = sheet.Cells.MaxDataRow;
maxColumn = sheet.Cells.MaxDataColumn;
//The range is still A1:B3.
range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1);

获取最大显示范围

当我们将工作表中的所有数据导出到 HTML、PDF 或图像时,需要获取一个包含所有可见对象的区域,包括数据、样式、图形、表格和数据透视表。 以下代码展示了如何将最大显示范围呈现为 HTML:

// Instantiate a new Workbook.
Workbook workbook = new Workbook("Book1.xlsx");
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.Worksheets;
//Gets the max display range.
Range range = worksheets[0].Cells.MaxDisplayRange;
//Save the range to html
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.ExportActiveWorksheetOnly = true;
saveOptions.ExportArea = CellArea.CreateCellArea(range.FirstRow, range.FirstColumn, range.FirstRow + range.RowCount - 1, range.FirstColumn + range.ColumnCount - 1);
//Save the range.
workbook.Save("html.html", saveOptions);

这是 源 Excel 文件