Mentioned
In
|
 |
 |
 |
 |
Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms
of Use |
Trademarks
Article ID: 306572 - Last Review: November 23, 2004 - Revision: 4.4 How to query and display excel data by using ASP.NET, ADO.NET, and Visual C# .NETThis article was previously published under Q306572 On This PageSUMMARY
This step-by-step article demonstrates how to display data from an Excel worksheet through an ASP.NET (.aspx) page by using Visual C# .NET. Create Sample Excel Worksheet- Start Microsoft Excel, and then create a new worksheet.
- Add the following information to the new worksheet to create a simple Excel database:
Collapse this tableExpand this table | A | B |
|---|
| 1 | FirstName | LastName | | 2 | Scott | Bishop | | 3 | Katie | Jordan |
NOTE: Although the data starts with cell A1 in this example, you can add this data to any adjacent cells within the worksheet. - Highlight the rows and columns where the data resides.
- On the Insert menu, point to Name, and then click Define.
- In the Names in workbook text box, type myRange1, and then click OK.
- On the File menu, click Save. In the Save in list, select the Web server root (which is typically C:\InetPub\Wwwroot\). In the File name text box, type ExcelData.xls. Click OK.
- On the File menu, click Exit.
Create ASP.NET Sample Using Visual C# .NET
This code sample demonstrates how to query and display information in an Excel worksheet. The following code uses the worksheet that you created in the previous section.
- Open Microsoft Visual Studio .NET. The Visual Studio .NET Integrated Development Environment (IDE) is displayed.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, under Project Types, click Visual C# Projects. Under Templates, click ASP.NET Web Application.
- In the New Project dialog box, locate the Name and Location text boxes. Notice that the Name text box is not available (it appears grayed out or dimmed). The Location text box contains the following text (or similar):
http://localhost/WebApplication1
Replace the text in the Location text box with http://localhost/ExcelCSTest, and then click OK. A new project is created, which includes a Web Form named WebForm1.aspx. - In the Visual Studio .NET IDE, locate the Solution Explorer window. If you cannot find it, click Solution Explorer on the View menu.
- In Solution Explorer, right-click WebForm1.aspx, and then click View Designer to display the designer for the appearance of the page. The designer allows you to add controls and manipulate the appearance of the page.
- Locate the toolbox. Depending on your IDE Option settings, the toolbox can appear as a window or a button (which often appears on the left side of the IDE). If you cannot find the toolbox, click Toolbox on the View menu.
If the toolbox appears as a button, move the pointer over the button so that the contents of the toolbox are displayed. - When the designer view of a Web Form is active, the toolbox is divided into sections, including the Web Forms, Components, HTML, and other sections. Click the Web Forms section.
- In the Web Forms section of the toolbox, click DataGrid, and then drag it onto the designer for WebForm1.
- Right-click WebForm1.aspx, and then click View Code to display the code-behind page source.
- Add the following statements to the top of the code-behind page, above the namespace section:
using System.Data.OleDb;
using System.Data;
- Highlight the following code, right-click the code, and then click Copy. In WebForm1.aspx.cs, paste the code into the Page_Load event:
// Create connection string variable. Modify the "Data Source"
// parameter as appropriate for your environment.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +
"Extended Properties=Excel 8.0;";
// Create connection object by using the preceding connection string.
OleDbConnection objConn = new OleDbConnection(sConnectionString);
// Open connection with the database.
objConn.Open();
// The code to follow uses a SQL SELECT command to display the data from the worksheet.
// Create new OleDbCommand to return data from worksheet.
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM myRange1", objConn);
// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;
// Create new DataSet to hold information from the worksheet.
DataSet objDataset1 = new DataSet();
// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData");
// Bind data to DataGrid control.
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
DataGrid1.DataBind();
// Clean up objects.
objConn.Close();
- On the File menu, click Save All to save the project files.
- On the Build menu, click Build to build the project. This prepares the code in the code-behind page so that it can be executed.
- In Solution Explorer, right-click WebForm1.aspx, and then click View in Browser to run the code.
Additional Code Explanation
The code sample in this article uses the Microsoft Jet OLE DB Provider to access the Excel worksheet. This code uses the following connection string to connect to the worksheet:
// Create connection string variable. Modify the "Data Source"
// parameter as appropriate for your environment.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +
"Extended Properties=Excel 8.0;";
As the comments indicate, you must modify the path information for the specific Excel worksheet. In addition, you must also set the value of the Extended Properties parameter to properly connect to the file.
Note that the connection string uses the Server.MapPath function. This function takes a path that is relative to Microsoft Internet Information Services (IIS) to a file and returns a hard disk path to that file. For example, in the Create Sample Excel Spreadsheet section, you create ExcelData.xls in the Web root directory, which is typically located at C:\Inetpub\Wwwroot. This also creates a subfolder named ExcelCSTest within the Wwwroot folder and a file named WebForm1.aspx within the ExcelCSTest folder.
In this example, the file path on the hard disk is as follows:
C drive
- Inetpub
- Wwwroot (which contains ExcelData.xls)
- ExcelCSTest (which contains WebForm1.aspx)
The IIS path to the files is as follows:
Web Root (which contains ExcelData.xls)
- ExcelCSTest (which contains WebForm1.aspx)
In this case, the relative path from the WebForm1.aspx page to the ExcelData.xls file is "../ExcelData.xls". The ".." characters inform IIS to go up one folder level. Therefore, the code
Server.MapPath("../ExcelData.xls")
returns the following string:
C:\Inetpub\Wwwroot\ExcelData.xls
You are not required to use Server.MapPath. You can also hard code this information to a specific path, or you can use any method to supply the location of the Excel file on the hard disk. REFERENCESFor additional information about how to access Excel files with ASP.NET, click the article numbers below
to view the articles in the Microsoft Knowledge Base:
307029Â
(http://kbalertz.com/Feedback.aspx?kbNumber=307029/EN-US/
)
HOWTO: Transfer XML Data to Microsoft Excel 2002 by Using Visual C# .NET
306023Â
(http://kbalertz.com/Feedback.aspx?kbNumber=306023/EN-US/
)
HOW TO: Transfer Data to an Excel Workbook by Using Visual C# .NET
For additional information about using ADO.NET, click the article numbers below
to view the articles in the Microsoft Knowledge Base:
306636Â
(http://kbalertz.com/Feedback.aspx?kbNumber=306636/EN-US/
)
HOW TO: Connect to a Database and Run a Command by Using ADO.NET and Visual C# .NET
314145Â
(http://kbalertz.com/Feedback.aspx?kbNumber=314145/EN-US/
)
HOW TO: Populate a DataSet Object from a Database by Using Visual C# .NET
307587Â
(http://kbalertz.com/Feedback.aspx?kbNumber=307587/EN-US/
)
HOW TO: Update a Database from a DataSet Object by Using Visual C# .NET
NOTE: The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, places, or events is intended or should be inferred.
APPLIES TO- Microsoft Excel 2002 Standard Edition
- Microsoft Excel 2000 Standard Edition
- Microsoft ASP.NET 1.1
- Microsoft ASP.NET 1.0
- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft ADO.NET 1.1
- Microsoft ADO.NET (included with the .NET Framework)
| kbdatabase kbhowtomaster kbwebforms KB306572 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
|
madhu
- madhu4up NOSPAM-AT-NOSPAM yahoo.com
|
Reported as Irrelevant
|
| Written:
2/1/2005 1:41 AM |
|
|
|
Laya
- layamohan NOSPAM-AT-NOSPAM rediffmail.com
|
Reported as Irrelevant
|
| Written:
6/29/2005 11:30 PM |
|
|
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please
|
 |
 |
 |
 |
 |
 |
 |
| |