获取工作表中的最大范围

如何获取最大范围

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

from aspose.cells import Workbook
workbook = Workbook("Book1.xlsx")
# Get all the worksheets in the book.
worksheets = workbook.worksheets
sheet = worksheets[0]
# Gets the max data range.
maxRow = sheet.cells.max_row
maxColumn = sheet.cells.max_column
# The range is A1:B3.
range = sheet.cells.create_range(0, 0, maxRow + 1, maxColumn + 1)
sheet.cells.get("A10").put_value(None)
maxRow = sheet.cells.max_row
maxColumn = sheet.cells.max_column
# The range is udpated to A1:B10.
range = sheet.cells.create_range(0, 0, maxRow + 1, maxColumn + 1)

如何获取最大数据范围

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

from aspose.cells import Workbook
# Instantiate a new Workbook.
workbook = Workbook("Book1.xlsx")
# Get all the worksheets in the book.
worksheets = workbook.worksheets
sheet = worksheets[0]
# Gets the max data range.
maxRow = sheet.cells.max_data_row
maxColumn = sheet.cells.max_data_column
# The range is A1:B3.
range = sheet.cells.create_range(0, 0, maxRow + 1, maxColumn + 1)
sheet.cells.get("A10").put_value(None)
maxRow = sheet.cells.max_data_row
maxColumn = sheet.cells.max_data_column
# The range is still A1:B3.
range = sheet.cells.create_range(0, 0, maxRow + 1, maxColumn + 1)

如何获取最大显示范围

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

from aspose.cells import CellArea, HtmlSaveOptions, Workbook
# Instantiate a new Workbook.
workbook = Workbook("Book1.xlsx")
# Get all the worksheets in the book.
worksheets = workbook.worksheets
# Gets the max display range.
range = worksheets[0].cells.max_display_range
# Save the range to html
saveOptions = HtmlSaveOptions()
saveOptions.export_active_worksheet_only = True
saveOptions.export_area = CellArea.create_cell_area(range.first_row, range.first_column, range.first_row + range.row_count - 1, range.first_column + range.column_count - 1)
# Save the range.
workbook.save("html.html", saveOptions)

这是 源 Excel 文件