将文件保存到响应对象

将文件保存到响应对象

还可以动态生成文件并直接发送到客户端浏览器。 为此,使用接受以下参数的 Save 方法的特殊重载版本:

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

注意

由于未在.NET5和.Netstandard中包含"System.Web.HttpResponse"对象, 因此,在Aspose.Cells .NET5和.Netstandard版本中不存在此函数,您可以参考以下代码将文件保存到流中,然后对流进行操作。

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