AutoFit Columns and Rows while loading HTML in Workbook
Possible Usage Scenarios
You can autofit columns and rows while loading your HTML file inside the Workbook object. Please set the HtmlLoadOptions.auto_fit_cols_and_rows property to true for this purpose.
AutoFit Columns and Rows while loading HTML in Workbook
The following sample code first loads the sample HTML into Workbook without any load options and saves it in XLSX format. It then again loads the sample HTML into Workbook but this time, it loads the HTML after setting the HtmlLoadOptions.auto_fit_cols_and_rows property to true and saves it in XLSX format. Please download both the output excel files i.e.Output Excel File Without AutoFitColsAndRows and Output Excel File With AutoFitColsAndRows. The following screenshot shows the effect of HtmlLoadOptions.auto_fit_cols_and_rows property on both output excel files.
Sample Code
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") |