Сохранить HTML с помощью StreamProvider
Contents
[
Hide
]
При преобразовании полей Excel, содержащих изображения и фигуры, в файлы html мы часто сталкиваемся со следующими двумя проблемами:
- Где мы должны сохранять изображения и фигуры при сохранении файла excel в поток html.
- Замените путь по умолчанию на исключенный путь.
В этой статье объясняется, как реализоватьIStreamProvider интерфейс для настройкиХтмлсавеоптионс. StreamProvider имущество. Внедрив этот интерфейс, вы сможете сохранять созданные ресурсы во время генерации HTML в ваших конкретных местах или потоках памяти.
Образец кода
Это основной код, показывающий использованиеХтмлсавеоптионс. StreamProviderимущество
This file contains hidden or 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 hidden or 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)); | |
} | |
} | |