在工作簿中加载 HTML 时自动调整列和行
Contents
[
Hide
]
可能的使用场景
您可以在将 HTML 文件加载到 Workbook 对象中时自动调整列和行。请设置**HtmlLoadOptions.AutoFitColsAndRows**财产给**真的**以此目的。
在工作簿中加载 HTML 时自动调整列和行
以下示例代码首先将示例 HTML 加载到 Workbook 中,不带任何加载选项,并以 XLSX 格式保存。然后它再次将示例 HTML 加载到工作簿中,但这一次,它在设置之后加载 HTML**HtmlLoadOptions.AutoFitColsAndRows**财产给**真的**并以XLSX格式保存。请同时下载输出的 excel 文件,即输出没有 AutoFitColsAndRows 的 Excel 文件和使用 AutoFitColsAndRows 输出 Excel 文件.下面的截图显示了效果**HtmlLoadOptions.AutoFitColsAndRows**两个输出 excel 文件的属性。
示例代码
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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
//Sample HTML. | |
string 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. | |
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sampleHtml)); | |
//Load memory stream into workbook. | |
Workbook wb = new Workbook(ms); | |
//Save the workbook in xlsx format. | |
wb.Save(dataDir + "outputWithout_AutoFitColsAndRows.xlsx"); | |
//Specify the HTMLLoadOptions and set AutoFitColsAndRows = true. | |
HtmlLoadOptions opts = new HtmlLoadOptions(); | |
opts.AutoFitColsAndRows = true; | |
//Load memory stream into workbook with the above HTMLLoadOptions. | |
wb = new Workbook(ms, opts); | |
//Save the workbook in xlsx format. | |
wb.Save(dataDir + "outputWith_AutoFitColsAndRows.xlsx"); |