Hello sir,
I am walmik, i have a gridview control i just want to export to excel i try your code but code execute successfully. but not going to convert to excel plz tell me solution........
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
GV_Final_Billing.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
GV_Final_Billing.HeaderRow.Cells[0].Style.Add("background-color", "green");
GV_Final_Billing.HeaderRow.Cells[1].Style.Add("background-color", "green");
GV_Final_Billing.HeaderRow.Cells[2].Style.Add("background-color", "green");
GV_Final_Billing.HeaderRow.Cells[3].Style.Add("background-color", "green");
for (int i = 0; i < GV_Final_Billing.Rows.Count; i++)
{
GridViewRow row = GV_Final_Billing.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");
}
}
GV_Final_Billing.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();
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
GV_Final_Billing.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
GV_Final_Billing.HeaderRow.Cells[0].Style.Add("background-color", "green");
GV_Final_Billing.HeaderRow.Cells[1].Style.Add("background-color", "green");
GV_Final_Billing.HeaderRow.Cells[2].Style.Add("background-color", "green");
GV_Final_Billing.HeaderRow.Cells[3].Style.Add("background-color", "green");
for (int i = 0; i < GV_Final_Billing.Rows.Count; i++)
{
GridViewRow row = GV_Final_Billing.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");
}
}
GV_Final_Billing.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();