自动调整合并单元格的行高
Contents
[
Hide
]
Microsoft Excel提供了一个功能,可以根据内容自动调整单元格的高度。该功能称为自动调整行高。Microsoft Excel不会本机设置合并单元格的自动调整操作。有时,这项功能对于真正需要在合并单元格上实现自动调整行高的用户来说是至关重要的。
如何使用AutoFitMergedCellsType自动调整行高
Aspose.Cells for Python via .NET通过AutoFitterOptions.AutoFitMergedCellsType API支持这个功能。使用这个API,可以在一个包括合并单元格的工作表中自动调整行高。以下是所有可能类型的自动调整合并单元格:
- NONE
- 第一行
- 最后一行
- 每一行
自动调整合并单元格的行高
请参考下面的代码,它创建一个工作簿对象并添加多个工作表。在每个工作表中使用不同的方法进行自动调整操作。屏幕截图显示了在执行示例代码后的结果。

C# 示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import AutoFitterOptions, Workbook | |
from os import os, path | |
# 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(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# 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 = True | |
# Autofit rows in the sheet(including the merged cells) | |
_worksheet.auto_fit_rows(options) | |
dataDir = dataDir + "AutoFitMergedCells.out.xlsx" | |
# Save the Excel file | |
wb.save(dataDir) |