Datei im Antwortobjekt speichern

Datei im Antwortobjekt speichern

Es ist auch möglich, eine Datei dynamisch zu generieren und direkt an einen Client-Browser zu senden. Verwenden Sie dazu eine spezielle überladene Version von**Speichern**Methode, die die folgenden Parameter akzeptiert:

Das**ContentDisposition**Die Enumeration bestimmt, ob die an den Browser gesendete Datei die Option bietet, sich selbst direkt im Browser oder in einer Anwendung zu öffnen, die mit .xls/.xlsx oder einer anderen Erweiterung verknüpft ist.

Die Enumeration enthält die folgenden vordefinierten Speichertypen:

Typ Beschreibung
Anhang Sendet die Tabelle an den Browser und wird in einer Anwendung als Anhang geöffnet, der mit .xls/.xlsx oder anderen Erweiterungen verknüpft ist
In der Reihe Sendet das Dokument an den Browser und bietet eine Option zum Speichern der Tabelle auf der Festplatte oder zum Öffnen im Browser

XLS Dateien

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

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

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

Notiz

Da das Objekt „System.Web.HttpResponse“ nicht in .NET5 und .Netstandard enthalten ist, Diese Funktion existiert also nicht in Aspose.Cells .NET5- und .Netstandard-Version, Sie können auf den folgenden Code verweisen, um die Datei im Stream zu speichern, und dann den Vorgang im Stream ausführen.

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