c# - How to export gridview selected column to excel?
I want to export gridview selected column to excel
I'm following this link to achieve this but in the below code
Where to call that ViewState["ds"]?
for loop not working it comes out because count is '0'
Code :
protected void btnExcel1_Click(object sender, ImageClickEventArgs e)
{
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);
EmpMasterGrid.AllowPaging = false;
EmpMasterGrid.DataBind();
EmpMasterGrid.HeaderRow.Style.Add("background-color", "#FFFFFF");
EmpMasterGrid.HeaderRow.Cells[0].Style.Add("background-color",
"green");
EmpMasterGrid.HeaderRow.Cells[1].Style.Add("background-color",
"green");
EmpMasterGrid.HeaderRow.Cells[2].Style.Add("background-color",
"green");
EmpMasterGrid.HeaderRow.Cells[3].Style.Add("background-color",
"green");
EmpMasterGrid.HeaderRow.Cells[4].Style.Add("background-color",
"green");
EmpMasterGrid.HeaderRow.Cells[5].Style.Add("background-color",
"green");
EmpMasterGrid.HeaderRow.Cells[6].Style.Add("background-color",
"green");
ArrayList arr = (ArrayList)ViewState["ds"];
EmpMasterGrid.HeaderRow.Cells[0].Visible = Convert.ToBoolean(arr[0]);
EmpMasterGrid.HeaderRow.Cells[1].Visible = Convert.ToBoolean(arr[1]);
EmpMasterGrid.HeaderRow.Cells[2].Visible = Convert.ToBoolean(arr[2]);
EmpMasterGrid.HeaderRow.Cells[0].Visible = Convert.ToBoolean(arr[3]);
EmpMasterGrid.HeaderRow.Cells[1].Visible = Convert.ToBoolean(arr[4]);
EmpMasterGrid.HeaderRow.Cells[2].Visible = Convert.ToBoolean(arr[5]);
EmpMasterGrid.HeaderRow.Cells[2].Visible = Convert.ToBoolean(arr[6]);
EmpMasterGrid.HeaderRow.Cells[0].FindControl("chkCol0").Visible =
false;
EmpMasterGrid.HeaderRow.Cells[1].FindControl("chkCol1").Visible =
false;
EmpMasterGrid.HeaderRow.Cells[2].FindControl("chkCol2").Visible =
false;
EmpMasterGrid.HeaderRow.Cells[3].FindControl("chkCol3").Visible =
false;
EmpMasterGrid.HeaderRow.Cells[4].FindControl("chkCol4").Visible =
false;
EmpMasterGrid.HeaderRow.Cells[5].FindControl("chkCol5").Visible =
false;
EmpMasterGrid.HeaderRow.Cells[6].FindControl("chkCol6").Visible =
false;
//Here condition fails bcoz count is 0
for (int i = 0; i < EmpMasterGrid.Rows.Count; i++)
{
GridViewRow row = EmpMasterGrid.Rows[i];
row.Cells[0].Visible = Convert.ToBoolean(arr[0]);
row.Cells[1].Visible = Convert.ToBoolean(arr[1]);
row.Cells[2].Visible = Convert.ToBoolean(arr[2]);
row.Cells[3].Visible = Convert.ToBoolean(arr[3]);
row.Cells[4].Visible = Convert.ToBoolean(arr[4]);
row.Cells[5].Visible = Convert.ToBoolean(arr[5]);
row.Cells[6].Visible = Convert.ToBoolean(arr[6]);
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "textmode");
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");
row.Cells[4].Style.Add("background-color", "#C2D69B");
row.Cells[5].Style.Add("background-color", "#C2D69B");
row.Cells[6].Style.Add("background-color", "#C2D69B");
}
}
EmpMasterGrid.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; }
</style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.End();
}
Any ideas? Thanks in advance
No comments:
Post a Comment