导出 Microsoft Excel 文件
Contents
[
Hide
]
可以使用 Aspose.Cells.GridWeb 控件在 GUI 模式的网站上创建新的或操作现有的 Microsoft Excel 文件。然后可以将文件保存到 Excel 文件中。 Aspose.Cells.GridWeb 有效地充当在线电子表格编辑器。本主题介绍如何将网格内容保存到 Excel 文件。
导出 Excel 文件
导出为文件
将 Aspose.Cells.GridWeb 控件的内容保存为 Excel 文件:
- 将 Aspose.Cells.GridWeb 控件添加到您的 Web 表单。
- 在指定路径将您的工作保存为 Excel 文件。
- 运行应用程序。
如果您不知道如何将 Aspose.Cells.GridWeb 控件添加到您的 Web 表单,那么您应该参考将 GridWeb 添加到 Web 窗体
当 Aspose.Cells.GridWeb 控件添加到窗体时,该控件会自动实例化并以默认大小添加到窗体。您不必创建 Aspose.Cells.GridWeb 控件对象,您所要做的就是拖放控件并开始使用它。
下面的代码示例说明了如何将网格内容保存到 Excel 文件。
This file contains hidden or 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 | |
// Generates a temporary file name. | |
string filename = Session.SessionID + "_out.xls"; | |
string path = (this.Master as Site).GetDataDir() + "\\GridWebBasics\\"; | |
// Saves to the file. | |
this.GridWeb1.SaveToExcelFile(path + filename); | |
// Sents the file to browser. | |
Response.ContentType = "application/vnd.ms-excel"; | |
Response.AddHeader("content-disposition", "attachment; filename=" + filename); | |
Response.WriteFile(path + filename); | |
Response.End(); |
如果您的文件系统是 NTFS,请授予对 ASPNET 或 Everyone 用户帐户的读/写访问权限,否则您将在运行时遇到拒绝访问的异常。
上面的代码片段可以以多种方式使用。一种常见的方法是添加一个按钮,单击该按钮可将网格内容保存到 Excel 文件中。 Aspose.Cells.GridWeb 提供了一种更简单的任务方法。 Aspose.Cells.GridWeb 有一个名为 SaveCommand 的事件。上面的代码片段可以添加到 SaveCommand 事件的事件处理程序中,它允许用户通过单击 Aspose.Cells.GridWeb 的内置救球按钮。
GridWeb 的 SaveCommand 事件
通过单击 GridWeb 的内置保存按钮将网格内容保存到 Excel
如果您在 Visual Studio 中工作,则可以通过双击特性窗格。要了解更多信息,请参阅使用 GridWeb 事件
导出为流
也可以将网格内容保存到流中(例如 MemoryStream)。
This file contains hidden or 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 | |
// Generates a temporary file name. | |
string filename = Session.SessionID + "_out.xls"; | |
string path = (this.Master as Site).GetDataDir() + "\\GridWebBasics\\"; | |
FileStream fs = File.Create(path + filename); | |
// Saving Grid content of the control to a stream | |
GridWeb1.SaveToExcelFile(fs); | |
// Closing stream | |
fs.Close(); | |
// Sents the file to browser. | |
Response.ContentType = "application/vnd.ms-excel"; | |
Response.AddHeader("content-disposition", "attachment; filename=" + filename); | |
Response.WriteFile(path + filename); | |
Response.End(); |