StreamProviderを使用してHTMLを保存する
Contents
[
Hide
]
画像やシェイプを含むエクセルファイルを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-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); |
以下のコードは、上記のコード内で使用される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-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)); | |
} | |
} | |