Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am converting the Gridview data into Excel for that code as follows;

i have two button as follows Generate (Button) And Export (Button).

when i click the Generate button the data will be displayed into the Gridview.

Then when i click the Export button the Gridview data will be displayed into the Excel.


Export Button code as follows;


C#
try
     {
         string attachment = "attachment; filename=collectiondetails.xls";
         Response.ClearContent();
         Response.AddHeader("content-disposition", attachment);
         Response.ContentType = "application/vnd.ms-excel";

Response.Write("");
         Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
         Response.Write("<body>");
         Response.Write("<table border="1" bordercolor="black">");

         Response.Write("<tr>");
         Response.Write("<td colspan="5"><center>");
         Response.Write("<font size="4" face="Times New Roman">Batchwise fee collection details</font>");
         Response.Write("</center></td>");
         Response.Write("</tr>");

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
         System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
         GridView1.RenderControl(htmlWrite);
         Response.Write(stringWrite.ToString());

         Response.Flush();
         Response.Write("<table>");
         Response.Write("</body>");
         Response.Write("</html>");
         Response.End();
     }
     catch (Exception Ex)
     {
         Label1.Text = Ex.Message.ToString();
         return;
     }



When i run, click the Generate button the data will displayed into the Gridview,
then when i click the Export Button shows as follows


You have chosen to open
collectiondetails.xls

Open with Microsoft Excel
Save file

when i select the open with Microsoft Excel and Click Ok, the error shows as follows;

unable to read the file.

From my above Export Button code what is the problem in my above code?


how can i do? please help me.


Regards,
Narasiman P.
Posted
Updated 25-Apr-13 2:36am
v2

1 solution

C#
Response.Clear();
        Response.Buffer = true;
       
        Response.AddHeader("content-disposition", 
         "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        GridView1.AllowPaging = false;
        GridView1.DataBind(); 

        //Change the Header Row back to white color
        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

        //Apply style to Individual Cells
        GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");   

        for (int i = 0; i < GridView1.Rows.Count;i++ )
        {
            GridViewRow row = GridView1.Rows[i];

            //Change Color back to white
            row.BackColor = System.Drawing.Color.White;

            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");

            //Apply style to Individual Cells of Alternating Row
            if (i % 2 != 0)
            {
                row.Cells[0].Style.Add("background-color", "#C2D69B");
                row.Cells[1].Style.Add("background-color", "#C2D69B");
                row.Cells[2].Style.Add("background-color", "#C2D69B");
                row.Cells[3].Style.Add("background-color", "#C2D69B");   
            }
        }
        GridView1.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
        Response.Write(style); 
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
 
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