1.将整 GridView的数据导出到Excel中关增加一个效果线做美化

最新的GridViewExport操作类

using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;namespace DotNet.Utilities
{/// <summary>/// Summary description for GridViewExport/// </summary>public class GridViewExport{public GridViewExport(){//// TODO: Add constructor logic here//}public static void Export(string fileName, GridView gv){HttpContext.Current.Response.Clear();HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));HttpContext.Current.Response.ContentType = "application/ms-excel";//HttpContext.Current.Response.Charset = "utf-8";using (StringWriter sw = new StringWriter()){using (HtmlTextWriter htw = new HtmlTextWriter(sw)){//  Create a form to contain the gridTable table = new Table();table.GridLines = GridLines.Both;  //单元格之间添加实线//  add the header row to the tableif (gv.HeaderRow != null){PrepareControlForExport(gv.HeaderRow);table.Rows.Add(gv.HeaderRow);}//  add each of the data rows to the tableforeach (GridViewRow row in gv.Rows){PrepareControlForExport(row);table.Rows.Add(row);}//  add the footer row to the tableif (gv.FooterRow != null){PrepareControlForExport(gv.FooterRow);table.Rows.Add(gv.FooterRow);}//  render the table into the htmlwritertable.RenderControl(htw);//  render the htmlwriter into the responseHttpContext.Current.Response.Write(sw.ToString());HttpContext.Current.Response.End();}}}/// <summary>/// Replace any of the contained controls with literals/// </summary>/// <param name="control"></param>private static void PrepareControlForExport(Control control){for (int i = 0; i < control.Controls.Count; i++){Control current = control.Controls;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 CheckBox){control.Controls.Remove(current);control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));}if (current.HasControls()){PrepareControlForExport(current);}}}/// <summary>/// 导出Grid的数据(全部)到Excel/// 字段全部为BoundField类型时可用/// 要是字段为TemplateField模板型时就取不到数据/// </summary>/// <param name="grid">grid的ID</param>/// <param name="dt">数据源</param>/// <param name="excelFileName">要导出Excel的文件名</param>public static void OutputExcel(GridView grid, DataTable dt, string excelFileName){Page page = (Page)HttpContext.Current.Handler;page.Response.Clear();string fileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(excelFileName));page.Response.AddHeader("Content-Disposition", "attachment:filename=" + fileName + ".xls");page.Response.ContentType = "application/vnd.ms-excel";page.Response.Charset = "utf-8";StringBuilder s = new StringBuilder();s.Append("<HTML><HEAD><TITLE>" + fileName + "</TITLE><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>");int count = grid.Columns.Count;s.Append("<table border=1>");s.AppendLine("<tr>");for (int i = 0; i < count; i++){if (grid.Columns.GetType() == typeof(BoundField))s.Append("<td>" + grid.Columns.HeaderText + "</td>");//s.Append("<td>" + grid.Columns.HeaderText + "</td>");}s.Append("</tr>");foreach (DataRow dr in dt.Rows){s.AppendLine("<tr>");for (int n = 0; n < count; n++){if (grid.Columns[n].Visible && grid.Columns[n].GetType() == typeof(BoundField))s.Append("<td>" + dr[((BoundField)grid.Columns[n]).DataField].ToString() + "</td>");}s.AppendLine("</tr>");}s.Append("</table>");s.Append("</body></html>");page.Response.BinaryWrite(System.Text.Encoding.GetEncoding("utf-8").GetBytes(s.ToString()));page.Response.End();}}
}

C#GridViewExport帮助类,美化导出相关推荐

  1. 根据自定义类属性导出Excel

    根据自定义类属性导出Excel 之前的工作中遇到了导出Excel的功能需求,为了方便之后的工作使用,整理成一个Util Jar包下载 https://download.csdn.net/downloa ...

  2. 使用idea将指定的类文件导出为jar包

    使用idea将指定的类文件导出为jar包 1.打开项目结构属性框 2.在项目结构属性中,选择构建Artifacts,然后点击+号,选择jar里面的empty,如图 3.配置jar包的相关属性(最关键) ...

  3. java导出类_java导出excel工具类

    java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...

  4. 工具类--Excel 导出poi

    实现功能 --批量导出excel 文件,配置一个sheet多少条数据,根据查询数据量的多少确定生成几个sheet页. pom 文件导入ExcelUtils工具包,依赖于poi包. <!-- ht ...

  5. DLL导出类和导出函数

    from:https://blog.csdn.net/goodluckmt/article/details/52691297 1.动态库DLL中的类或者函数有时候要被其他的库调用,因此需要被其他库调用 ...

  6. php 导出excel类,php 导出excel类

    标签: /** * excel导出类 * * 使用方法 $excel=new Excel(); * //设置编码: *$excel->setEncode("utf-8",&q ...

  7. 「Java工具类」pdf导出工具类java导出pdf文件工具类

    介绍语 本号主要是Java常用关键技术点,通用工具类的分享:以及springboot+springcloud+Mybatisplus+druid+mysql+redis+swagger+maven+d ...

  8. java工具类之导出Excel

    在我们的项目中经常会用到导出excel,这里就列举一下大家最常用到的导出EXCEL技术,在说一下自己设计的工具类.对POI不感兴趣的人,可以直接跳到最下面的工具类; 什么是Apache POI? Ap ...

  9. 利用POI工具类实现导出Excel的功能

    poi工具类的概述: Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 结构: HSSF - 提供读 ...

最新文章

  1. php session域名共享,实现多域名下共用一个SESSION
  2. ORACLE HANDBOOK系列之六:ODP.NET与复杂的PL/SQL数据类型(Using ODP.NET To Deal With Complex PLSQL Data Types)...
  3. 原来MySQL面试还会问这些...
  4. 使用Redis作为分布式锁的错误用法
  5. 100 道 Linux 笔试题,能拿90分以上的都去了BAT
  6. 网页后门危害大 网站安全狗帮助查杀
  7. 用mui索引实现动态数据仿通讯录的功能
  8. 多测师软件测试肖sir_金融问题(1)
  9. Ubuntu 备份系统为ISO镜像 解决ISO限制4GB大小 Clone当前系统到其他电脑
  10. 力扣(83.643)补8.29
  11. 【死链】JDK1.7中HashMap在多线程环境的并发问题源码分析
  12. muduo学习笔记:net部分之实现TCP网络编程库-Buffer
  13. 2021-11-13周报
  14. Postgres-XL数据库GTM——GTM and Global Transaction Management
  15. Codeforces Round #695 (Div. 2)ABCD题解详析
  16. 学完大数据开发一般可以胜任哪些工作?
  17. python3.5权限问题
  18. 移动通信网络规划:室内覆盖系统概述
  19. 简历 解析 技术总结
  20. 通过IP地址查看计算机名

热门文章

  1. Mongoose之 SchemaTypes 数据类型
  2. GrowingIO创始人兼CEO张溪梦:互联网下半场,数据如何驱动企业突破增长重围?
  3. NTT通信公司在大阪开通运营容灾数据中心
  4. “反应快”的程序猿更优秀吗?
  5. 机器学习是如何改善企业生产力的?(内附机器智能版图)
  6. 4-asa-url-filter
  7. android通过json生成视图
  8. silverlightLogo动画
  9. 文档预览 OfficeWebViewer:在浏览器中查看Office文档
  10. MySQL抽稀_python安装mysql的依赖包mysql-python操作