Guardar HTML con StreamProvider
Al convertir archivos de Excel que contienen imágenes y formas en 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 en streams de memoria.
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-.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); |
Aquí está el código para la clase ExportStreamProvider, que implementa la interfaz IStreamProvider utilizada dentro del código anterior.
// 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(); | |
} | |
} | |
} |