使用StreamProvider保存HTML
Contents
[
Hide
]
当将包含图像和形状的Excel文件转换为HTML文件时, 我们经常会遇到以下两个问题:
- 将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(); | |
} | |
} | |
} |