J
JohnE
I have a gridview that is exported to excel. The code below works. But, the
only drawback is the gridview has 6 pages (sometimes more, sometimes less).
When the export occurs, it takes the grid and the pages and puts it into
excel. I need to have a regular looking spreadsheet (all 112 records)
without the gridview paging look. I have tried putting GridView1.AllowPaging
= false in different spots of the code but it is not working. Here is the
exporting code that I am using.
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
PrepareGridViewForExport(GridView1);
ExportGridToExcel(GridView1, "test");
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the specified
ASP.NET server control at run time.
return;
}
public void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}.xls", fileName));
Response.Charset = "";
Response.ContentType =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
private void PrepareGridViewForExport(Control gv)
{
LinkButton lb = new LinkButton();
Literal lit = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls.GetType() == typeof(LinkButton))
{
lit.Text = (gv.Controls as LinkButton).Text;
gv.Controls.Remove(gv.Controls);
gv.Controls.AddAt(i, lit);
}
else if (gv.Controls.GetType() == typeof(DropDownList))
{
lit.Text = (gv.Controls as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls);
gv.Controls.AddAt(i, lit);
}
else if (gv.Controls.GetType() == typeof(CheckBox))
{
lit.Text = (gv.Controls as CheckBox).Checked ? "True" :
"False";
gv.Controls.Remove(gv.Controls);
gv.Controls.AddAt(i, lit);
}
if (gv.Controls.HasControls())
{
PrepareGridViewForExport(gv.Controls);
}
}
}
Can someone see the reason the paging is still there in the spreadsheet and
what is needed to rectifiy it?
Thanks.
John
only drawback is the gridview has 6 pages (sometimes more, sometimes less).
When the export occurs, it takes the grid and the pages and puts it into
excel. I need to have a regular looking spreadsheet (all 112 records)
without the gridview paging look. I have tried putting GridView1.AllowPaging
= false in different spots of the code but it is not working. Here is the
exporting code that I am using.
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
PrepareGridViewForExport(GridView1);
ExportGridToExcel(GridView1, "test");
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the specified
ASP.NET server control at run time.
return;
}
public void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}.xls", fileName));
Response.Charset = "";
Response.ContentType =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
private void PrepareGridViewForExport(Control gv)
{
LinkButton lb = new LinkButton();
Literal lit = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls.GetType() == typeof(LinkButton))
{
lit.Text = (gv.Controls as LinkButton).Text;
gv.Controls.Remove(gv.Controls);
gv.Controls.AddAt(i, lit);
}
else if (gv.Controls.GetType() == typeof(DropDownList))
{
lit.Text = (gv.Controls as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls);
gv.Controls.AddAt(i, lit);
}
else if (gv.Controls.GetType() == typeof(CheckBox))
{
lit.Text = (gv.Controls as CheckBox).Checked ? "True" :
"False";
gv.Controls.Remove(gv.Controls);
gv.Controls.AddAt(i, lit);
}
if (gv.Controls.HasControls())
{
PrepareGridViewForExport(gv.Controls);
}
}
}
Can someone see the reason the paging is still there in the spreadsheet and
what is needed to rectifiy it?
Thanks.
John