将文件保存到响应对象
Contents
[
Hide
]
Aspose.Cells可以操作文件。本文解释了可以将文件保存到响应对象的各种方法。
将文件保存到响应对象
还可以动态生成文件并直接发送到客户端浏览器。 为此,使用接受以下参数的 Save 方法的特殊重载版本:
- ASP.NET HttpResponse 对象。
- 文件名。
- ContentDisposition,输出文件的 content-disposition 类型。
- SaveOptions,文件格式类型
ContentDisposition 枚举确定发送到浏览器的文件是否提供直接在浏览器中打开或在与 .xls/.xlsx 或其他扩展名相关联的应用程序中打开的选项。
该枚举包含以下预定义的保存类型:
类型 | 描述 |
---|---|
附件 | 将电子表格发送到浏览器,并作为与.xls/.xlsx或其他扩展名相关联的附件在应用程序中打开 |
内置 | 将文档发送到浏览器,并提供选项将电子表格保存到磁盘或在浏览器中打开 |
XLS文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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版本中不存在此函数,您可以参考以下代码将文件保存到流中,然后对流进行操作。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |