ワークブックにHTMLをロードする際の列と行を自動調整する
可能な使用シナリオ
Workbook オブジェクト内でHTMLファイルをロードする際、列と行を自動調整できます。この目的のためにHtmlLoadOptions.AutoFitColsAndRowsプロパティを true に設定してください。
ワークブックにHTMLをロードする際の列と行を自動調整する
次のサンプルコードでは、まずロードオプションなしでサンプルHTMLをワークブックにロードし、XLSX形式で保存します。そして、HtmlLoadOptions.AutoFitColsAndRowsプロパティを true に設定してサンプルHTMLを再びワークブックにロードし、XLSX形式で保存します。HtmlLoadOptions.AutoFitColsAndRows プロパティの効果は、以下のスクリーンショットに示されています。自動調整なしの出力Excelファイル および 自動調整ありの出力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"); |