如何将多个页面的gridview数据导出到excel文档中?

我已经在我的C#asp.net表单应用程序中使用了gridview,我已经启用了“AllowPaging”,现在只要我想将其转换为Excel,只显示第一页。 转换是使用“HtmlTextWriter”和“StringWriter”完成的。

-提前致谢。

要从gridview中导出excel,你可以在你的项目中使用下面的类。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Web.UI.WebControls; using System.Web.UI; using System.Web; namespace gride_edit { public class GridToExcel { public static void ExportToExcel(string strFileName, GridView gv) { using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // Create a form to contain the grid Table table = new Table(); // add the header row to the table if (gv.HeaderRow != null) { PrepareControlForExport(gv.HeaderRow); table.Rows.Add(gv.HeaderRow); } // add each of the data rows to the table foreach (GridViewRow row in gv.Rows) { PrepareControlForExport(row); row.Style["background-color"] = "#FFFBD6"; table.Rows.Add(row); } // add the footer row to the table if (gv.FooterRow != null) { PrepareControlForExport(gv.FooterRow); table.Rows.Add(gv.FooterRow); } // render the table into the htmlwriter table.RenderControl(htw); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", strFileName)); HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); //render the htmlwriter into the response HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } } } private static void PrepareControlForExport(Control control) { for (int i = 0; i < control.Controls.Count; i++) { Control current = control.Controls[i]; if (current is LinkButton) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); } else if (current is ImageButton) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); } else if (current is HyperLink) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); } else if (current is DropDownList) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); } else if (current is HiddenField) { control.Controls.Remove(current); } else if (current is CheckBox) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); } if (current.HasControls()) { PrepareControlForExport(current); } } } } } 

并可以调用这个函数来导出。

 GridToExcel.ExportToExcel("Report" + DateTime.Now.ToString("yyyyMMddhhmmss").ToString() + ".xls", GridView1); 

而为了从多个分页的gridview中导出一个excel,只需在调用convert函数“ExportToExcel()”之前使用这些行。

 GridView1.AllowPaging = false; bindGridView(); 

我从网上获得了“GridToExcel”类。 它为我工作,希望它也能为你工作。 请告诉我。

一般来说,您可以使用以下function

  public void Export(HttpResponseBase Response, object data) { var gridItem = new System.Web.UI.WebControls.GridView(); gridItem.DataSource = data; gridItem.DataBind(); StringWriter writer = new StringWriter(); HtmlTextWriter html = new HtmlTextWriter(writer); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=result.xls"); Response.ContentType = "application/excel"; gridItem.RenderControl(html); Response.Write(writer.ToString()); Response.End(); } 

你可以调用这个函数

 var details = /* your query result goes here */ ; Export(Response , details ); 

希望它可以帮助你