Gestern hatte ich das Bedürfnis, Daten aus einem Excel-File auszulesen und sie in einer .aspx-Seite darzustellen. Das Ganze ist per OleDbConnection sehr easy und leicht zu managen:
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("List.xls") + ";" +
"Extended Properties=Excel 8.0;";
OleDbConnection myConn = new OleDbConnection(connectionString);
// Open connection with the database.
myConn.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 myComm =new OleDbCommand("SELECT * FROM myRange1", myConn);
// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter da = new OleDbDataAdapter();
// Pass the Select command to the adapter.
da.SelectCommand = myComm;
// Create new DataSet to hold information from the worksheet.
DataSet ds = new DataSet();
// Fill the DataSet with the information from the worksheet.
da.Fill(ds, "XLData");
// Bind data to DataGrid control.
resultGridView.DataSource = ds.Tables[0].DefaultView;
resultGridView.DataBind();
// Clean up objects.
myConn.Close();
Vielleicht hilft's ja jemandem. :)