Issue in Export And Import Data To Excel File In ASP.NET
<p>I am import the data from a data table to excel file using following code : </p>

<pre><code>public void ExportDataTable(DataTable dt)
        {
            string attachment = "attachment; filename=EmployeeList.xls";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            string sTab = "";
            foreach (DataColumn dc in dt.Columns)
            {
                HttpContext.Current.Response.Write(sTab + dc.ColumnName);
                sTab = "\t";
            }
            HttpContext.Current.Response.Write("\n");

            int i;
            foreach (DataRow dr in dt.Rows)
            {
                sTab = "";
                for (i = 0; i &lt; dt.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write(sTab + dr[i].ToString());
                    sTab = "\t";
                }
                HttpContext.Current.Response.Write("\n");
            }
            HttpContext.Current.Response.End();
        }
</code></pre>

<p>It is working fine. </p>

<p>Now when i am importing the this excel file to data table then following error accurs:</p>

<p>External table is not in the expected format. </p>

<p>Here is my code to export the excel file to data table :</p>

<pre><code>  public static DataTable excelToDataTable(string file)
        {
            DataTable dt = new DataTable();
            OleDbConnection oledbcn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= Server.MapPath("EmployeeList.xls").ToString(); Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'");       
            string SheetName = "EmployeeList";
            oledbcn.Open();
            OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM [" + SheetName + "$]", oledbcn);
            OleDbDataAdapter oledbda = new OleDbDataAdapter();
            oledbda.SelectCommand = cmd;
            oledbda.Fill(dt);
            oledbcn.Close();

            return dt;
        }
</code></pre>

<p>Please resolve my issue guys in such way that i can export and import MSOffice 2003 and 2007 excel files.</p>

<p>Thanks,
Rajbir</p>
