Spara fil till responsobjekt

Spara fil till responsobjekt

Det är också möjligt att generera en fil dynamiskt och skicka den direkt till en klientwebbläsare. För att göra det, använd en speciell överlagrad version av Save metoden som accepterar följande parametrar:

Uppräkningen ContentDisposition avgör om filen som skickas till webbläsaren ger möjlighet att öppnas av sig själv direkt i webbläsaren eller i en applikation associerad med .xls/.xlsx eller en annan förlängning.

Uppräkningen innehåller följande fördefinierade sparalternativ:

Typ Beskrivning
Bilaga Skickar kalkylarket till webbläsaren och öppnas i en applikation som en bilaga associerad med .xls/.xlsx eller andra filändelser
Inline Skickar dokumentet till webbläsaren och presenterar ett alternativ att spara kalkylarket på disken eller öppna det inne i webbläsaren

XLS-filer

// 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-filer

// 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-filer

// 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();
}

Obs

På grund av objektet “System.Web.HttpResponse” som inte inkluderas i .NET5 och .Netstandard, Funktionen finns därför inte i Aspose.Cells .NET5 och .Netstandard-versionen, du kan hänvisa till följande kod för att spara filen i strömmen och utföra operationer med strömmen.

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();
}