StreamProviderを使用してHTMLを保存する
Contents
[
Hide
]
画像と形を含むExcelファイルをHTMLファイルに変換すると、次の2つの問題に直面することがよくあります。
- HTMLストリームにエクセルファイルを保存する際、画像やシェイプをどこに保存すべきか
- デフォルトのパスを期待されるパスに置き換える
この記事では、IStreamProvider インターフェイスを実装してHtmlSaveOptions.StreamProvider プロパティを設定する方法について説明します。このインターフェースを実装することで、HTML生成中に作成されたリソースを特定の場所またはメモリストリームに保存することができます。
HtmlSaveOptions.StreamProvider プロパティの使用法を示す主要なコードです。
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); | |
string outputDir = dataDir + @"out\"; | |
// Create workbook | |
Workbook wb = new Workbook(dataDir + "sample.xlsx"); | |
HtmlSaveOptions options = new HtmlSaveOptions(); | |
options.StreamProvider = new ExportStreamProvider(outputDir); | |
// Save into .html using HtmlSaveOptions | |
wb.Save(dataDir + "output_out.html", options); |
上記のコード内で使用されるIStreamProvider インターフェイスを実装するExportStreamProvider クラスのコードです。
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 | |
public class ExportStreamProvider : IStreamProvider | |
{ | |
private string outputDir; | |
public ExportStreamProvider(string dir) | |
{ | |
outputDir = dir; | |
} | |
public void InitStream(StreamProviderOptions options) | |
{ | |
string path = outputDir + Path.GetFileName(options.DefaultPath); | |
options.CustomPath = path; | |
Directory.CreateDirectory(Path.GetDirectoryName(path)); | |
options.Stream = File.Create(path); | |
} | |
public void CloseStream(StreamProviderOptions options) | |
{ | |
if (options != null && options.Stream != null) | |
{ | |
options.Stream.Close(); | |
} | |
} | |
} |