0

Iam Exporting data from datatable to excel ,i referred the below link and implemented the same

Export DataTable to Excel File

The problem iam facing is when opening Excel it is showing the file you are trying to open is in different format.What can i do to resolve this error

The Code i used is

 dt = city.GetAllCity();//your datatable
    string attachment = "attachment; filename=city.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.ms-excel";
    string tab = "";
    foreach (DataColumn dc in dt.Columns)
    {
        Response.Write(tab + dc.ColumnName);
        tab = "\t";
    }
    Response.Write("\n");
    int i;
    foreach (DataRow dr in dt.Rows)
    {
        tab = "";
        for (i = 0; i < dt.Columns.Count; i++)
        {
            Response.Write(tab + dr[i].ToString());
            tab = "\t";
        }
        Response.Write("\n");
    }
    Response.End();
Community
  • 1
  • 1
havin
  • 203
  • 4
  • 17
  • You need to check which version of excel is installed on your system. and you need to use that extension in your code. – Aftab Ahmed Sep 22 '16 at 05:55
  • You are writing a tab-delimited file but saying it's a "native" xls file: Excel used to put up with that but for the last few versions that gets you the warning about the file format. See: https://support.microsoft.com/en-us/kb/948615 – Tim Williams Sep 22 '16 at 05:58

0 Answers0