StreamProvider ile HTML yü Kaydet
Resimler ve şekiller içeren excel dosyalarını html dosyalarına dönüştürdüğümüzde, genellikle aşağıdaki iki sorunla karşılaşırız:
- Excel dosyasını html olarak kaydederken görüntü ve şekilleri nereye kaydedeceğiz.
- Varsayılan yolu beklenen yol ile değiştirin.
Bu makale, IStreamProvider arayüzünü HtmlSaveOptions.StreamProvider özelliğini ayarlamak için nasıl uygulayacağınızı açıklar. Bu arayüzü uygulayarak, HTML oluşturma sırasında oluşturulan kaynakları belirli konumlara veya bellek akışlarına kaydedebilirsiniz.
Bu, HtmlSaveOptions.StreamProvider özelliğini kullanmanın ana kodudur.
// 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); |
Aşağıda, yukarıdaki kod içerisinde kullanılan IStreamProvider arayüzünü uygulayan ExportStreamProvider sınıfının kodu bulunmaktadır.
// 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(); | |
} | |
} | |
} |