Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using this code for export function while importing the same excel sheet i'm getting the error like "File you are trying to open is in a different format than what you have specified by the file extension" like that
C#
public void ExportToExcel(DataTable dt, string filename)
     {
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        //Response.ContentType = "application/ms-excel";
        Response.Write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
        Response.ContentEncoding = Encoding.UTF8;
        Response.Charset = "";
        EnableViewState = false;

        //Set Fonts
        Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
        Response.Write("<BR><BR><BR>");

        //Sets the table border, cell spacing, border color, font of the text, background,
        //foreground, font height
        Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");

        // Check not to increase number of records more than 65k according to excel,03
        if (dt.Rows.Count <= 65536)
        {

            // Get DataTable Column's Header
            foreach (DataColumn column in dt.Columns)
            {
                //Write in new column
                Response.Write("<Td>");

                //Get column headers  and make it as bold in excel columns
                Response.Write("<B>");
                Response.Write(column);
                Response.Write("</B>");
                Response.Write("</Td>");
            }

            Response.Write("</TR>");

            // Get DataTable Column's Row
            foreach (DataRow row in dt.Rows)
            {
                //Write in new row
                Response.Write("<TR>");

                for (int i = 0; i <= dt.Columns.Count - 1; i++)
                {
                    Response.Write("<Td>");
                    Response.Write(row[i].ToString());
                    Response.Write("</Td>");
                }

                Response.Write("</TR>");
            }
        }

        Response.Write("</Table>");
        Response.Write("</font>");
        Response.Flush();
        Response.End();
Posted
v2
Comments
Hemant Singh Rautela 22-Mar-13 2:42am    
I think there is no any error, when file create you can contiune with this error & file will can open with excel or you can use 3rd party dll for conversion.

I also applied as below post(Its working fine):
http://hemantrautela.blogspot.com/2013/01/how-convert-gridview-to-wordexcel.html
Maciej Los 22-Mar-13 2:57am    
You are trying to write text as HTML table, but not to write to Excel file!

1 solution

Do you understand what you are doing???
Here you say you are sending excel:
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
And then? What's that???
Response.Write("<table border="1" ...=""> <tr>");</tr></table>
That's a table in html! That's not at all excel!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900