string filePath = "~/somedirectory/somespreadsheet.xls";
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + MapPath(filePath) + ";"
+ "Extended Properties=" + (char)34
+ "Excel 8.0;IMEX=1;" + (char)34;
DataSet dsRecords = new DataSet();
// "Sheet" is name of sheet being opened, must add $ after the name
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
//da.TableMappings.Add("Table", "ExcelText");
da.Fill(dsRecords);
scheduleGridView.DataSource = dsRecords.Tables[0].DefaultView;
scheduleGridView.DataBind();
As a note, you can also access the spreadsheet directly using the file path:
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=c:/somedirectory/somespreadsheet.xls;"
+ "Extended Properties=Excel 8.0;";
No comments:
Post a Comment