现在的位置: 首页 > 综合 > 正文

GridView中的数据导出到Excel方法(包含处理模板列)

2014年02月07日 ⁄ 综合 ⁄ 共 6417字 ⁄ 字号 评论关闭
  1. GridView中的数据导出到Excel方法(包含处理模板列)收藏
  2. 新一篇: 关于js日期控件 | 旧一篇: 55种网页常用小技巧 
  3.     今天再整理一篇有用的文章,对于GridView中数据的导出的处理问题。有时我们在GridView表格中显示的不仅仅是从数据库中读出的值,还需要加上一些人为处理的模板列(比如删除按钮、下拉选择框、hypelinker等)。在导出后,我们往往不需要这些控件显示或者不希望以控件的形式来显示,而是以数据的形式来展示。这就需要在导出时作一些处理,参考了一些互联网上的信息,整理如下:
  4. 一、导出button 
  5.    protected void export_Click(object sender, EventArgs e)
  6.     {
  7.         if (this.MachineList.Rows.Count == 0)
  8.         {
  9.             Response.Write("<script>alert(&apos;没有查找到数据,无法导出!&apos;)</script>");
  10.         }
  11.         else
  12.         {
  13.             this.MachineList.AllowPaging = false// 将有分页的GridView中的数据全部导出到Excel
  14.             Bind();
  15.             Export("application/ms-excel""设备信息.xls");
  16.             // 换成 export("application/ms-word", "设备信息.doc"); 那么导出的就是Word格式的了.
  17.             this.MachineList.AllowPaging = true;
  18.             Bind();
  19.         }        
  20.     }
  21. 二、导出主函数
  22.  public void Export(string FileType, string FileName)
  23.     {
  24.         string style = @"<style>.text{mso-number-format:@}</script>";//导入到excel时,保存表里数字列中前面存在的 0 .
  25.         PrepareGridViewForExport(MachineList);//将模版列显示出来
  26.         Response.Clear();
  27.         Response.Charset = "GB2312";
  28.         Response.ContentEncoding = Encoding.UTF7;
  29.         Response.AppendHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
  30.         Response.ContentType = FileType;
  31.         this.EnableViewState = false;
  32.         this.MachineList.AllowPaging = false;
  33.         System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN"true);
  34.         StringWriter sw = new StringWriter();
  35.         HtmlTextWriter htw = new HtmlTextWriter(sw);
  36.         this.MachineList.RenderControl(htw);
  37.         Response.Write(style);
  38.         Response.Write(sw.ToString());
  39.         //Response.Write(dt.ToString());
  40.         Response.End();
  41.     }
  42.     /// <summary>
  43.     /// 重写VerifyRenderingInServerForm(Control control)方法,以确保在程序运行时,指定的GridView控件总是位于"<form runat="server"></form>"标记内.
  44.     /// 该方法用来确认在运行时为指定的ASP.NET服务器控件呈现<form runat="server">标记.
  45.     ///     必须位于<form runat="server">中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息;另外,发送回或依赖于注册的脚本块的
  46.     /// 控件应该在Control.Render()方法的重写中调用此方法.
  47.     ///     如果回发或使用客户端脚本的服务器控件没有包含在<form runat="server"> 标记中,它们将无法正常工作,这时,可以通过重写VerifyRenderingInServerForm(
  48.     /// Control control)方法使用程序正常运行.
  49.     /// </summary>
  50.     /// <param name="control"></param>
  51.     public override void VerifyRenderingInServerForm(Control control)
  52.     {
  53.         // Confirms that an HtmlForm control is rendered for
  54.         //the specified ASP.NET server control at run time.
  55. }
  56. 三、如果GridView存在模板列,其中包含子控件,例如CheckBox等,导出EXCEL后就会出现该区域的不规律。所以要对模板列单独处理(转载)
  57. public  void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong
  58.     {
  59.         LinkButton lb = new LinkButton();
  60.         Literal l = new Literal();
  61.         string name = String.Empty;
  62.         for (int i = 0; i < gv.Controls.Count; i++)
  63.         {
  64.             
  65.             if (gv.Controls[i].GetType() == typeof(LinkButton))
  66.             {
  67.                 l.Text = (gv.Controls[i] as LinkButton).Text;
  68.                 gv.Controls.Remove(gv.Controls[i]);
  69.                 gv.Controls.AddAt(i, l);
  70.             }
  71.             else if (gv.Controls[i].GetType() == typeof(DropDownList))
  72.             {
  73.                 l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
  74.                 gv.Controls.Remove(gv.Controls[i]);
  75.                 gv.Controls.AddAt(i, l);
  76.             }
  77.             else if (gv.Controls[i].GetType() == typeof(CheckBox))
  78.             {
  79.                 l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
  80.                 gv.Controls.Remove(gv.Controls[i]);
  81.                 gv.Controls.AddAt(i, l);
  82.             }
  83.             else if (gv.Controls[i].GetType() == typeof(ImageButton))
  84.             {
  85.                 l.Text = "图片";
  86.                 gv.Controls.Remove(gv.Controls[i]);
  87.                 gv.Controls.AddAt(i, l);
  88.             }
  89.             if (gv.Controls[i].HasControls())
  90.             {
  91.                 PrepareGridViewForExport(gv.Controls[i]);
  92.             }
  93.         }
  94. }
  95.     注:对于模板列的处理还有一特殊情况,我在GridView使用时,用了<a></a>在客户端打开对话框的方法。在导出时,这一列不是模板列控件,但导出后Excel中会显示有下划线的链接,所以想去掉它。
  96. 想了很多方法,也没成功。后来我只好用了不得以的方法,多加一列文本列,用来显示链接的文本,导出前采用列隐藏的方法解决。
  97. 如果有更好的处理方法,也请各位指教!

抱歉!评论已关闭.