Salvataggio File in un Oggetto di Risposta

Salvataggio del file nell’oggetto di risposta

È anche possibile generare un file dinamicamente e inviarlo direttamente a un browser del client. Per farlo, utilizza una versione sovraccaricata speciale del metodo Save che accetta i seguenti parametri:

L’enumerazione ContentDisposition determina se il file inviato al browser fornisce l’opzione di aprire direttamente nel browser o in un’applicazione associata a .xls/.xlsx o un’altra estensione.

L’enumerazione contiene i seguenti tipi di salvataggio predefiniti:

Tipo Descrizione
Allegato Invia il foglio di calcolo al browser e apre un’applicazione come allegato associato a .xls/.xlsx o altre estensioni
Inline Invia il documento al browser e presenta un’opzione per salvare il foglio di calcolo sul disco o aprirlo all’interno del browser

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

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

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

Nota

A causa dell’oggetto “System.Web.HttpResponse” che non è incluso in .NET5 e .Netstandard, Quindi questa funzione non esiste nella versione Aspose.Cells .NET5 e .Netstandard, è possibile fare riferimento al codice seguente per salvare il file nello stream, quindi manipolare lo stream.

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