Use ASP.NET to access the Excel document

zhaozj2021-02-16  54

Use ASP.NET to access the Excel document post: Andy.m Date: 2003-07-12 Popularity: 40 Use ASP.NET to access Excel Document Excel is a software in Microsoft's Office Suite, which is mainly used to process electronics form. Excel is welcomed by the majority of office staff with the advantages of friendly interface, and the rapid processing of data. So many documents are saved in Excel. For program designers, in programming, we tend to access the Excel file to get the data. However, because the Excel file is not a standard database, it is more difficult to use the program language to access him. ASP.NET is a product that Microsoft's highly recommended product, as an important part of the .NET Framework framework, which is mainly used for web design. The new design concept, powerful features make ASP.NET is being welcomed by more and more programming staff. It is also the powerful function of ASP.NET to make access to Excel documents a relatively simple matter. Below, an example is specifically illustrated how the ASP.NET accesses the Excel document. I. Program Design and Operation Environment (1) .windows 2000 Professional (2) .. Net Framework SDK Beta 2 (3). Microsoft Access Data Component 2.6 (MADC2.6) II. Specific Design Idea (1). Get Accessing the Excel file name (2). Read the contents of the Excel file (3). Displayed with the DataGrid format to specify some key steps. Third. Key Steps (1) for programming. For convenience, we assume the Excel file in the root directory of the C drive, name Test.xls. (2). In order to read the Excel file, we must understand a namespace - System.Data.OleDb. Three objects in System.Data.OleDB are OLEDBConnection, OLEDBCommand, OLEDBDataAdapter. We are accessing the Excel file by them. I> OLEDBConnection objects are mainly to provide a connection method. II> OLEDBCommand object provides specific operation methods for the target. The III> OLEDBDataAdapter object is the return data set after different operations of the object. In order to read the Test.xls of the C disk, you can do just the following statement: DIM myoledbconnection as oledbconnection = new oledbconnection ("provider = microsoft.jet.OleDb.4.0;" & _ "Data Source = C: /Test.xls;" & _ "Extended Properties =" "Excel 8.0;" "") Dim myoledbcommand as oledbcommand = new oledbcommand ("Select * from [sheet1 $], myoledbconnection)" If you want to read the content of Sheet2, turn the Sheet1 $ changed SHEET2 $ DIM MyData as oledbdataadapter = new oledbdataadapter (myoledbcommand) (3) After reading the contents of the Excel file, it will be displayed in DataGrid. For display, use another namespace --system.data. He has an object Dataset, he can make data to DataGrid to display data in DataGrid.

At this time, the data set read is not expressed in the object Dataset, which requires a conversion, so that the OLEDBDataAdapter object provides a method -fill, which can be converted. The specific procedures are as follows: DIM MyDataSet As New DataSet () MyData.Fill (MyDataSet) 'Completes the conversion of the OLEDBDataAdapter object to DataSet DataSourID1.DataSource = myDataSet.tables (0) .defaultViewDataGrid1.DatabaseTabind () "Complete data governance, display data IV. Specific source code: <% @ page language = "vb"%> <% @ import namespace = "system.data"%> <% @ Import namespace = "system.data.oledb"%>