导入 Microsoft Excel 文件
导入Excel文件
从文件导入
使用Aspose.Cells.GridWeb控件打开Excel文件:
- 将Aspose.Cells.GridWeb控件添加到Web表单中。
- 通过指定文件路径导入 Excel 文件。
- 运行应用程序。
当Aspose.Cells.GridWeb控件添加到Web表单时,该控件将自动实例化并添加到具有默认大小的表单。您无需创建Aspose.Cells.GridWeb控件对象,您只需拖放该控件并开始使用它。
然而,要将Excel文件中的内容加载到Aspose.Cells.GridWeb控件中,您必须调用ImportExcelFile方法来指定Excel文件的路径。之后,Aspose.Cells.GridWeb控件将自动在指定路径中找到文件并显示其内容。下面提供了加载Excel文件内容的代码片段。
// 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文件
从流导入
除了从文件打开Excel文件,Aspose.Cells.GridWeb控件还可以从流中加载Excel文件。使用文件作为流是禁止任何文件访问或共享违规问题的更好方法,因为这种方法通过关闭流来确保关闭对文件的所有连接。
// 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(); |