?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
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.
???????? ?????????dialog box, locate the???, ???????text boxes. ?? ????????text box is not available (it appears grayed out or dimmed). The?????text box contains the following text (or similar):
http://localhost/WebApplication1
Replace the text in the?????text box withhttp://localhost/ExcelCSTest?? ????-????? ????, ?? ???? ???OK. ??? ??? ????????? ????? ??, ?? ???? ??? ??????? WebForm1.aspx ??? ????? ???
// 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();
?? ?? ????? ???? ?? ??? ?????? ???? ??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.
For 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://support.microsoft.com/kb/307029/EN-US/
)
HOWTO: Transfer XML Data to Microsoft Excel 2002 by Using Visual C# .NET
306023
(http://support.microsoft.com/kb/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://support.microsoft.com/kb/306636/EN-US/
)
HOW TO: Connect to a Database and Run a Command by Using ADO.NET and Visual C# .NET
314145
(http://support.microsoft.com/kb/314145/EN-US/
)
HOW TO: Populate a DataSet Object from a Database by Using Visual C# .NET
307587
(http://support.microsoft.com/kb/307587/EN-US/
)
HOW TO: Update a Database from a DataSet Object by Using Visual C# .NET
???:: 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.