These two methods should get you started. They will fill a dataset with the
excel data.
Hope that helps.
-Darrin
private DataSet GetExcelData(System.IO.FileInfo importFile)
{
OleDbConnection _xlConn = new OleDbConnection();
try
{
DataSet _xlDataSet = new DataSet();
_xlConn = GetOleConnectionForExcel(importFile.FullName);
OleDbDataAdapter _xlAdapt = new OleDbDataAdapter();
_xlConn.Open();
DataTable _schemaTable =
_xlConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach(DataRow _dr in _schemaTable.Rows)
{
_xlAdapt.SelectCommand = new OleDbCommand("Select * from [" +
_dr["TABLE_NAME"].ToString() + "]", _xlConn);
_xlAdapt.Fill(_xlDataSet, _dr["TABLE_NAME"].ToString());
}
return _xlDataSet;
}
catch(SqlException _sqlException)
{
throw new Exception("Error getting Excel data.", _sqlException);
}
catch(Exception _exception)
{
throw new Exception("Error getting Excel data.", _exception);
}
finally
{
if(_xlConn.State != ConnectionState.Closed)
_xlConn.Close();
}
}
private OleDbConnection GetOleConnectionForExcel(string filePath)
{
string _cnString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filePath +
";" + "Extended Properties=Excel 8.0;IMEX=1";
return new OleDbConnection(_cnString);
}