GridView导出Excel
来源:互联网 发布:谷歌搜索引擎优化 编辑:程序博客网 时间:2024/04/29 11:44
protected void export_Click(object sender, EventArgs e) { if (this.MachineList.Rows.Count == 0) { Response.Write("<script>alert('没有查找到数据,无法导出!')</script>"); } else { this.MachineList.AllowPaging = false; // 将有分页的GridView中的数据全部导出到Excel Bind(); Export("application/ms-excel", "设备信息.xls"); // 换成 export("application/ms-word", "设备信息.doc"); 那么导出的就是Word格式的了. this.MachineList.AllowPaging = true; Bind(); } } 二、导出主函数 public void Export(string FileType, string FileName) { string style = @"<style>.text{mso-number-format:@}</script>";//导入到excel时,保存表里数字列中前面存在的 0 . PrepareGridViewForExport(MachineList);//将模版列显示出来 Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = Encoding.UTF7; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; this.MachineList.AllowPaging = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); this.MachineList.RenderControl(htw); Response.Write(style); Response.Write(sw.ToString()); //Response.Write(dt.ToString()); Response.End(); } /// <summary> /// 重写VerifyRenderingInServerForm(Control control)方法,以确保在程序运行时,指定的GridView控件总是位于"<form runat="server"></form>"标记内. /// 该方法用来确认在运行时为指定的ASP.NET服务器控件呈现<form runat="server">标记. /// 必须位于<form runat="server">中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息;另外,发送回或依赖于注册的脚本块的 /// 控件应该在Control.Render()方法的重写中调用此方法. /// 如果回发或使用客户端脚本的服务器控件没有包含在<form runat="server"> 标记中,它们将无法正常工作,这时,可以通过重写VerifyRenderingInServerForm( /// Control control)方法使用程序正常运行. /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for //the specified ASP.NET server control at run time. } 三、如果GridView存在模板列,其中包含子控件,例如CheckBox等,导出EXCEL后就会出现该区域的不规律。所以要对模板列单独处理(转载) public void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong { LinkButton lb = new LinkButton(); Literal l = new Literal(); string name = String.Empty; for (int i = 0; i < gv.Controls.Count; i++) { if (gv.Controls[i].GetType() == typeof(LinkButton)) { l.Text = (gv.Controls[i] as LinkButton).Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(DropDownList)) { l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(CheckBox)) { l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False"; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(ImageButton)) { l.Text = "图片"; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } if (gv.Controls[i].HasControls()) { PrepareGridViewForExport(gv.Controls[i]); } } } 注:对于模板列的处理还有一特殊情况,我在GridView使用时,用了<a></a>在客户端打开对话框的方法。在导出时,这一列不是模板列控件,但导出后Excel中会显示有下划线的链接,所以想去掉它。 想了很多方法,也没成功。后来我只好用了不得以的方法,多加一列文本列,用来显示链接的文本,导出前采用列隐藏的方法解决。 如果有更好的处理方法,也请各位指教!
- GridView导出Excel研究
- GridView导出Excel研究
- GridView导出Excel研究
- GridView导出Excel
- GridView格式化导出Excel
- GridView导出Excel研究
- GridView导出到Excel
- GridView导出Excel研究
- GridView导出Excel研究
- GridView导出Excel研究
- GridView导出Excel研究
- gridview导出Excel
- GridView导出Excel
- gridview 导出excel
- GridView导出到Excel
- GridView导出到Excel
- GridView导出Excel研究
- GridView导出到Excel
- 自定义通用查询组件
- 重复记录问题
- JSP生成随机证码,并且局部刷新验证码
- UVa Problem 10033 Interpreter (解释器)
- NHibernate各种查询
- GridView导出Excel
- struts1.X的国际化问题
- UVa Problem 10196 Check the Check (将军)
- TeraSort实验--测试Map和Reduce Task数量对Hadoop性能的影响
- 走进 SQL/XML
- eclipse中可以搜索当前文件的插件下载
- 浅析win7库数据管理不足,为何还用分区管理
- Web开发中的文件上传问题研究
- UVa Problem 10142 Australian Voting (澳大利亚投票)