导入 Microsoft Excel 文件
Contents
[
Hide
]
与 Aspose.Cells.GridDesktop、Aspose.Cells.GridWeb 控件一样,可以打开和加载 Microsoft Excel 文件 - 包含数据、格式、图表、图像等 - 但在 Web 应用程序中。本主题说明如何。
导入 Excel 文件
从文件导入
要使用 Aspose.Cells.GridWeb 控件打开 Excel 文件:
- 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体。
- 通过指定文件路径导入 Excel 文件。
- 运行应用程序。
如果您不知道如何将控件添加到 Web 表单,请参阅将 GridWeb 添加到 Web 窗体.
当 Aspose.Cells.GridWeb 控件添加到 Web 窗体时,该控件会自动实例化并以默认大小添加到窗体。您不必创建 Aspose.Cells.GridWeb 控件对象,您所要做的就是拖放控件并开始使用它。
但是,要将 Excel 文件中的内容加载到 Aspose.Cells.GridWeb 控件中,您必须调用 ImportExcelFile 方法来指定 Excel 文件的路径。之后,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 | |
// Gets the web application's path. | |
string path = (this.Master as Site).GetDataDir(); | |
string fileName = path + "\\GridWebBasics\\SampleData.xls"; | |
// Imports from an excel file. | |
GridWeb1.ImportExcelFile(fileName); |
上面的代码片段可以以任何你想要的方式使用。例如,要在加载 Web 表单时自动加载 Excel 文件,请将此代码添加到表单的 Page_Load 事件。如果想在点击按钮时打开文件,在web窗体中添加一个按钮,在按钮的Click事件下写上上面的代码。
单击按钮时加载 Excel 文件
如果您的文件系统是 NTFS,您应该向 ASPNET 或 Everyone 用户帐户授予读取访问权限,否则您将在运行时遇到访问被拒绝的异常。
从流导入
除了从文件中打开 Excel 文件外,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 | |
// Gets the web application's path. | |
string path = (this.Master as Site).GetDataDir(); | |
string fileName = path + "\\GridWebBasics\\SampleData.xls"; | |
// Opening an Excel file as a stream | |
FileStream fs = File.OpenRead(fileName); | |
// Loading the Excel file contents into the control from a stream | |
GridWeb1.ImportExcelFile(fs); | |
// Closing stream | |
fs.Close(); |