Read excel 2003 file format return dataset
FileToConvert is path of excel file
public DataSet ConvertExcelFiletodataset()
{
string connectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + FileToConvert + “;Extended Properties=Excel 8.0;”;
DataSet ds = new DataSet();
OleDbConnection connection = new OleDbConnection(connectionString);
try
{
connection.Open();
//this next line assumes that the file is in default Excel format with Sheet1 as the first sheet name, adjust accordingly
DataTable dtsheet = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow dr in dtsheet.Rows)
{
OleDbDataAdapter adapter = new OleDbDataAdapter(“SELECT * FROM [” + dr[“TABLE_NAME”] + “]”, connection);
DataTable dt = new DataTable();
adapter.Fill(ds, dr[“TABLE_NAME”].ToString());
//the next 2 lines do exactly the same thing, just shows two ways to get the same data
connection.Close();
}
}
catch (Exception ex)
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
throw (ex);
}
finally
{
connection.Close();
}
return ds;
}
