自动调整行和列
自动调整
Aspose.Cells for Python via .NET提供了一个表示Microsoft Excel文件的Workbook类。Workbook类包含一个worksheets集合,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了广泛的属性和方法,用于管理工作表。本文讨论了使用Worksheet类来自动调整行或列。
自动调整行 - 简单
自动调整行宽度和高度最直接的方法是调用 Worksheet 类的 auto_fit_row 方法。auto_fit_row 方法以要调整行的索引作为参数。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the 3rd row of the worksheet | |
worksheet.auto_fit_row(1) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
如何在单元格范围内自动调整行
一行由多个列组成。Aspose.Cells for Python via .NET允许开发人员通过调用auto_fit_row方法的重载版本来根据行内单元格范围内容自动调整行。它接受以下参数:
- 行索引,即要自动调整的行的索引。
- 第一个列索引,即行的第一个列的索引。
- 最后列索引,指行的最后一列的索引。
auto_fit_row 方法检查行中所有列的内容,然后自动调整行。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the 3rd row of the worksheet | |
worksheet.auto_fit_row(1, 0, 5) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
如何在一系列单元格中自动调整列
一列由许多行组成。通过调用重载版本的auto_fit_column 方法,可以根据列中单元格范围中的内容自动调整列,该方法接受以下参数:
- 列索引,要自动调整的列的索引。
- 第一行索引,列的第一行的索引。
- 最后行索引,列的最后一行的索引。
auto_fit_column 方法检查列中所有行的内容,然后自动调整列。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the Column of the worksheet | |
worksheet.auto_fit_column(4, 4, 6) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
如何为合并单元格自动调整行高
使用 Aspose.Cells for Python via .NET 可以自动调整合并单元格的行高,甚至对使用 AutoFitterOptions API 进行合并的单元格也可以。AutoFitterOptions类提供了auto_fit_merged_cells_type属性,可用于自动调整合并单元格的行高。auto_fit_merged_cells_type接受AutoFitMergedCellsType枚举,其包含以下成员。
- NONE: 忽略合并单元格。
- FIRST_LINE: 仅扩展第一行的高度。
- LAST_LINE: 仅扩展最后一行的高度。
- EACH_LINE: 仅扩展每行的高度。
from aspose.cells import AutoFitMergedCellsType, AutoFitterOptions, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
# Instantiate a new Workbook | |
wb = Workbook() | |
# Get the first (default) worksheet | |
_worksheet = wb.worksheets[0] | |
# Create a range A1:B1 | |
range = _worksheet.cells.create_range(0, 0, 1, 2) | |
# Merge the cells | |
range.merge() | |
# Insert value to the merged cell A1 | |
_worksheet.cells.get(0, 0).value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end" | |
# Create a style object | |
style = _worksheet.cells.get(0, 0).get_style() | |
# Set wrapping text on | |
style.is_text_wrapped = True | |
# Apply the style to the cell | |
_worksheet.cells.get(0, 0).set_style(style) | |
# Create an object for AutoFitterOptions | |
options = AutoFitterOptions() | |
# Set auto-fit for merged cells | |
options.auto_fit_merged_cells_type = AutoFitMergedCellsType.EACH_LINE | |
# Autofit rows in the sheet(including the merged cells) | |
_worksheet.auto_fit_rows(options) | |
# Save the Excel file | |
wb.save(outputDir + "AutofitRowsforMergedCells.xlsx") |
您还可以尝试使用重载版本的auto_fit_rows 和auto_fit_columns 方法,这些方法接受一系列行/列和AutoFitterOptions实例,以根据您的要求自动调整所选行/列。AutoFitterOptions。
上述方法的签名如下: