使用StreamProvider保存HTML
Contents
[
Hide
]
在将包含图像和形状的Excel文件转换为HTML文件时,我们经常会面临以下两个问题:
- 将Excel文件保存为HTML流时,应该保存图像和形状的位置。
- 用期望的路径替换默认路径。
本文说明了如何为设置特定位置或内存流保存在HTML生成期间创建的资源提供IStreamProvider接口的实现。通过实现此接口,您将能够将创建的资源保存到指定的位置或内存流。
示例代码
这是展示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-Java | |
String dataDir = Utils.getDataDir(HtmlSaveOptions.class); | |
Workbook wb = new Workbook(dataDir + "sample.xlsx"); | |
HtmlSaveOptions options = new HtmlSaveOptions(); | |
options.setStreamProvider(new ExportStreamProvider(dataDir)); | |
wb.save(dataDir + "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-Java | |
public class ExportStreamProvider implements IStreamProvider { | |
private String outputDir; | |
public ExportStreamProvider(String dir) { | |
outputDir = dir; | |
System.out.println(outputDir); | |
} | |
@Override | |
public void closeStream(StreamProviderOptions options) throws Exception { | |
if (options != null && options.getStream() != null) { | |
options.getStream().close(); | |
} | |
} | |
@Override | |
public void initStream(StreamProviderOptions options) throws Exception { | |
System.out.println(options.getDefaultPath()); | |
File file = new File(outputDir); | |
if (!file.exists() && !file.isDirectory()) { | |
file.mkdirs(); | |
} | |
String defaultPath = options.getDefaultPath(); | |
String path = outputDir + defaultPath.substring(defaultPath.lastIndexOf("/") + 1); | |
options.setCustomPath(path); | |
options.setStream(new FileOutputStream(path)); | |
} | |
} | |