حفظ الملف إلى كائن الاستجابة

حفظ الملف إلى كائن الاستجابة

من الممكن أيضًا إنشاء ملف ديناميكيًا وإرساله مباشرة إلى متصفح العميل. للقيام بذلك ، استخدم إصدارًا خاصًا محملاً فوق طاقته من**[حفظ] (https://reference.aspose.com/cells/net/aspose.cells.workbook/save/methods/5)**الطريقة التي تقبل المعلمات التالية:

ال**[ContentDisposition] (https://reference.aspose.com/cells/net/aspose.cells/contentdisposition)**يحدد التعداد ما إذا كان الملف الذي يتم إرساله إلى المتصفح يوفر خيار الفتح بنفسه مباشرة في المستعرض أو في تطبيق مرتبط بـ .xls / .xlsx أو امتداد آخر.

يحتوي التعداد على أنواع الحفظ المحددة مسبقًا التالية:

يكتب وصف
مرفق يرسل جدول البيانات إلى المتصفح ويفتح في تطبيق ما كمرفق مرتبط بـ .xls / .xlsx أو ملحقات أخرى
في النسق يرسل المستند إلى المتصفح ويقدم خيارًا لحفظ جدول البيانات على القرص أو فتحه داخل المتصفح

XLS ملفات

// 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);
HttpResponse Response = null;
// Load your source workbook
Workbook workbook = new Workbook();
if (Response != null)
{
// Save in Excel2003 XLS format
workbook.Save(Response, dataDir + "output.xls", ContentDisposition.Inline, new XlsSaveOptions());
Response.End();
}

XLSX ملفات

// 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);
HttpResponse Response = null;
// Load your source workbook
Workbook workbook = new Workbook();
if (Response != null)
{
// Save in Xlsx format
workbook.Save(Response, dataDir + "output.xlsx", ContentDisposition.Attachment, new OoxmlSaveOptions());
Response.End();
}

PDF ملفات

// 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);
HttpResponse Response = null;
// Creating a Workbook object
Workbook workbook = new Workbook();
if (Response != null)
{
// Save in Pdf format
workbook.Save(Response, dataDir + "output.pdf", ContentDisposition.Attachment, new PdfSaveOptions());
Response.End();
}

ملحوظة

نظرًا للكائن “System.Web.HttpResponse” لم يتم تضمينه في .NET5 و .Netstandard ، لذلك لا توجد هذه الوظيفة في Aspose.Cells .NET5 والإصدار القياسي ، يمكنك الرجوع إلى الكود التالي لحفظ الملف في الدفق ، ثم إجراء العملية على الدفق.

public async Task<IActionResult> DownloadExcel()
{
// 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 filePath = dataDir + "Book1.xlsx";
// Load your source workbook
Workbook workbook = new Workbook(filePath);
// Save the workbook to a memory stream
var stream = new MemoryStream();
workbook.Save(stream, SaveFormat.Xlsx);
// Reset the position of the stream to 0
stream.Position = 0;
// Set the content type and file name
var contentType = "application/octet-stream";
var fileName = "output.xlsx";
// Set the response headers
Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{fileName}\"");
Response.ContentType = contentType;
// Write the file contents to the response body stream
await stream.CopyToAsync(Response.Body);
// Close the file stream
stream.Dispose();
// Return the response
return new EmptyResult();
}