获取工作表中的最大范围
在从工作表读取数据时,我们需要知道最大的区域。
在从工作表复制所有数据时,我们需要知道最大的区域。
在将指定区域导出到html和pdf时,我们需要知道最大的区域。
Aspose.Cells for Python via .NET包含查找工作表中最大范围的不同方法。
如何获取最大范围
在Aspose.Cells for Python via .NET中,如果初始化了row和column对象,这些行和列将被计算到最大的区域,即使空行或空列中没有数据。
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 文件。