With the code i have below i am able to export only one page of gridview to excel..
pls check and modify it once.so that i can import all the pages of gridview to excel at a time
with checked status of checkbox.
Thanks in advance
protected void btnExporttoExcel_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Presentees"), new DataColumn("StudentName"), new DataColumn("ColgRegisterNum"), new DataColumn("CollegeName"), new DataColumn("Date"), });
foreach (GridViewRow row in gvStdntDetails.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool cell0 = (row.FindControl("CheckBox1") as CheckBox).Checked;
string cell1;
if (cell0)
cell1 = "Present";
else
cell1 = "Absent";
string cell3 = (row.FindControl("gvlblRegNum") as Label).Text;
string cell4 = (row.FindControl("gvlblSTdntName") as Label).Text;
string cell5 = (row.FindControl("gvlblColgName") as Label).Text;
string cell6 = (row.FindControl("gvAttDate") as Label).Text;
dt.Rows.Add(cell1, cell3, cell4, cell5, cell6);
}
}
GridView GridView1 = new GridView();
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.DataSource = dt;
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
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");
}
}
GridView1.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}