ワークブックにHTMLをロードする際の列と行を自動調整する
可能な使用シナリオ
HTMLファイルをWorkbookオブジェクト内で読み込む際に列と行を自動調整できます。このためには、HtmlLoadOptions.AutoFitColsAndRowsプロパティをtrueに設定してください。
ワークブックにHTMLをロードする際の列と行を自動調整する
次のサンプルコードは、最初にロードオプションなしでサンプルHTMLをWorkbookに読み込み、XLSX形式で保存します。その後、HtmlLoadOptions.AutoFitColsAndRowsプロパティをtrueに設定してサンプルHTMLをWorkbookに再度読み込み、XLSX形式で保存します。出力エクセルファイル、つまりAuftput Excel File Without AutoFitColsAndRowsとAutoFitColsAndRowsを使用した出力Excelファイルをダウンロードしてください。次のスクリーンショットは、HtmlLoadOptions.AutoFitColsAndRowsプロパティの効果を示したものです。

サンプルコード
| // 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 = "./"; | |
| //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"); |