Salva Html con StreamProvider
Contents
[
Hide
]
Durante la conversione di campi excel che contengono iamges e forme in file html, spesso affrontiamo i seguenti due problemi:
- Dove dovremmo salvare le immagini e le forme quando salviamo il file excel nel flusso html.
- Sostituisci il percorso predefinito con il percorso escluso.
Questo articolo spiega come implementareIStreamProvider interfaccia per l’impostazione delHtmlSaveOptions.StreamProvider proprietà. Implementando questa interfaccia, sarai in grado di salvare le risorse create durante la generazione di HTML nelle tue posizioni o flussi di memoria specifici.
Questo è il codice principale che mostra l’utilizzo diHtmlSaveOptions.StreamProviderproprietà
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-.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); |
Ecco il codice perExportStreamProvider classe che implementaIStreamProviderinterfaccia utilizzata all’interno del codice precedente.
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-.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(); | |
} | |
} | |
} |