Spalten und Zeilen automatisch anpassen beim Laden von HTML in Arbeitsmappe
Mögliche Verwendungsszenarien
Sie können Spalten und Zeilen automatisch anpassen, während Sie Ihre HTML-Datei im Workbook-Objekt laden. Bitte setzen Sie die HtmlLoadOptions.auto_fit_cols_and_rows-Eigenschaft zu true für diesen Zweck.
Spalten und Zeilen automatisch anpassen beim Laden von HTML in Arbeitsmappe
Der folgende Beispielcode lädt zunächst den Beispiel-HTML-Code ohne Ladeoptionen in Workbook und speichert ihn im XLSX-Format. Danach wird der Beispiel-HTML-Code erneut in Workbook geladen, diesmal jedoch nachdem die HtmlLoadOptions.auto_fit_cols_and_rows-Eigenschaft auf true gesetzt wurde, und im XLSX-Format gespeichert. Bitte laden Sie beide Ausgabe-Excel-Dateien herunter, d.h. Ausgabe Excel-Datei ohne AutoFitColsAndRows und Ausgabe Excel-Datei mit AutoFitColsAndRows. Der folgende Screenshot zeigt die Wirkung der HtmlLoadOptions.auto_fit_cols_and_rows-Eigenschaft auf beide Ausgabe-Excel-Dateien.
Beispielcode
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") |