Guardar HTML con StreamProvider
Cuando convertimos archivos de Excel que contienen imágenes y formas a archivos HTML, a menudo nos enfrentamos a los siguientes dos problemas:
- ¿Dónde debemos guardar las imágenes y formas al guardar el archivo de Excel en un stream HTML?
- Reemplazar la ruta predeterminada con la ruta esperada.
Este artículo explica cómo implementar la interfaz IStreamProvider para establecer la propiedad HtmlSaveOptions.StreamProvider. Al implementar esta interfaz, podrá guardar los recursos creados durante la generación de HTML en ubicaciones específicas o secuencias de memoria.
Código de Muestra
Este es el código principal que muestra el uso de la propiedad HtmlSaveOptions.StreamProvider
// 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); |
Aquí está el código para la clase ExportStreamProvider que implementa la interfaz IStreamProvider utilizada en el código anterior.
// 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)); | |
} | |
} | |