在加载 HTML 到工作簿时自动调整列和行
可能的使用场景
在加载 HTML 文件到 Workbook 对象内部时,您可以同时调整列和行的大小。请将 HtmlLoadOptions.auto_fit_cols_and_rows 属性设置为 true 以实现此目的。
加载HTML至工作簿时自适应调整列和行
以下示例代码首先无需加载选项将样本 HTML 加载到 Workbook 中并以 XLSX 格式保存。然后再次加载样本 HTML 到 Workbook 中,但这次在设置 HtmlLoadOptions.auto_fit_cols_and_rows 属性为 true 后加载,并以 XLSX 格式保存。请下载两个输出的 Excel 文件,即 未自动调整列和行的输出 Excel 文件 和 已自动调整列和行的输出 Excel 文件。以下截图展示了 HtmlLoadOptions.auto_fit_cols_and_rows 属性对两个输出 Excel 文件的影响。
示例代码
from aspose.cells import HtmlLoadOptions, Workbook | |
from io import BytesIO | |
# 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(".") | |
# Sample HTML. | |
sampleHtml = "<html><body><table><tr><td>This is sample text.</td><td>Some text.</td></tr><tr><td>This is another sample text.</td><td>Some text.</td></tr></table></body></html>" | |
# Load html string into memory stream. | |
ms = BytesIO(sampleHtml.encode("utf-8")) | |
# Load memory stream into workbook. | |
wb = Workbook(ms) | |
# Save the workbook in xlsx format. | |
wb.save(dataDir + "outputWithout_AutoFitColsAndRows.xlsx") | |
# Specify the HTMLLoadOptions and set AutoFitColsAndRows = true. | |
opts = HtmlLoadOptions() | |
opts.auto_fit_cols_and_rows = True | |
# Load memory stream into workbook with the above HTMLLoadOptions. | |
wb = Workbook(ms, opts) | |
# Save the workbook in xlsx format. | |
wb.save(dataDir + "outputWith_AutoFitColsAndRows.xlsx") |