在加载 HTML 到工作簿时自动调整列和行
可能的使用场景
在加载 HTML 文件到 Workbook 对象内部时,您可以同时调整列和行的大小。请将 HtmlLoadOptions.AutoFitColsAndRows 属性设置为 true 以实现此目的。
加载HTML至工作簿时自适应调整列和行
以下示例代码首先无需加载选项将样本 HTML 加载到 Workbook 中并以 XLSX 格式保存。然后再次加载样本 HTML 到 Workbook 中,但这次在设置 HtmlLoadOptions.AutoFitColsAndRows 属性为 true 后加载,并以 XLSX 格式保存。请下载两个输出的 Excel 文件,即 未自动调整列和行的输出 Excel 文件 和 已自动调整列和行的输出 Excel 文件。以下截图展示了 HtmlLoadOptions.AutoFitColsAndRows 属性对两个输出 Excel 文件的影响。
示例代码
// 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"); |