حفظ Html باستخدام StreamProvider
عند تحويل ملفات Excel التي تحتوي على صور وأشكال إلى ملفات HTML، نواجه في كثير من الأحيان المشاكل الاتية: 1.أين يجب أن نحفظ الصور والأشكال عند حفظ ملف Excel إلى تدفق HTML.
- استبدال المسار الافتراضي بالمسار المتوقع.
يشرح هذا المقال كيفية تنفيذ واجهة IStreamProvider لضبط خاصية HtmlSaveOptions.StreamProvider. من خلال تنفيذ هذه الواجهة، ستتمكن من حفظ الموارد التي تم إنشاؤها أثناء توليد صفحات HTML إلى مواقعك المحددة أو تدفقات الذاكرة الداخلية.
هذا هو الرمز الرئيسي الذي يظهر استخدام 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); |
فيما يلي الرمز لفئة ExportStreamProvider التي تنفذ IStreamProvider المستخدمة داخل الرمز المذكور أعلاه.
// 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(); | |
} | |
} | |
} |