StreamProvider で Html を保存する
Contents
[
Hide
]
画像や図形を含む Excel ファイルを html ファイルに変換する場合、次の 2 つの問題に直面することがよくあります。
- Excelファイルを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); |
ここにコードがありますExportStreamProvider実装するクラスIStreamProvider上記のコード内で使用されるインターフェイス。
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(); | |
} | |
} | |
} |