在加载 HTML 到工作簿时自动调整列和行
可能的使用场景
在加载 HTML 文件到 Workbook 对象内时,您可以设置 HtmlLoadOptions.AutoFitColsAndRows 属性为 true ,以自动调整列和行。
加载HTML至工作簿时自适应调整列和行
以下示例代码首先将示例 HTML 无需任何加载选项加载到 Workbook 中,并保存为 XLSX 格式。然后再次将样本 HTML 加载到 Workbook 中,但这次在设置 HtmlLoadOptions.AutoFitColsAndRows 属性为 true 后加载 HTML,并保存为 XLSX 格式。请下载输出的两个 Excel 文件,即 不使用自动调整列和行的输出 Excel 文件 和 使用自动调整列和行的输出 Excel 文件。以下截图展示了 HtmlLoadOptions.AutoFitColsAndRows 属性对两个输出 Excel 文件的影响。
示例代码
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(AutoFitColumnsRowsLoadingHTML.class) + "LoadingSavingConvertingAndManaging/"; | |
//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 byte array input stream | |
ByteArrayInputStream bais = new ByteArrayInputStream(sampleHtml.getBytes()); | |
//Load byte array stream into workbook. | |
Workbook wb = new Workbook(bais); | |
//Save the workbook in xlsx format. | |
wb.save(dataDir + "outputWithout_AutoFitColsAndRows.xlsx"); | |
//Specify the HtmlLoadOptions and set AutoFitColsAndRows = true. | |
HtmlLoadOptions opts = new HtmlLoadOptions(); | |
opts.setAutoFitColsAndRows(true); | |
//Load byte array stream into workbook with the above HtmlLoadOptions. | |
bais.reset(); | |
wb = new Workbook(bais, opts); | |
//Save the workbook in xlsx format. | |
wb.save(dataDir + "outputWith_AutoFitColsAndRows.xlsx"); |