C#GridViewExport帮助类,美化导出
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 grid
Table table = new Table();
table.GridLines = GridLines.Both; //单元格之间添加实线
// 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);
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);
// render the htmlwriter into the response
HttpContext.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帮助类,美化导出的更多相关文章
- [Excel] C#GridViewExport帮助类,美化导出 (转载)
主要功能如下1.将整GridView的数据导出到Excel中关增加一个效果线做美化最新的GridViewExport操作类看下面代码吧 /// <summary> /// 类说明:Grid ...
- TAQSkinScrollBar 类美化滚动条再讨论
再说:TAQSkinScrollBar 类美化滚动条,http://www.138soft.com/?p=156 里面有人提到不可以滚动 滚动的改善方法: unit AQSkinScrollBar; ...
- java工具类POI导出word
1.新建一个word,里面填写内容,如: 2.导出wordjava类 /** * POI导出word测试 * @throws Exception */ @RequestMapping(value=&q ...
- tp5.1 phpspreadsheet- 工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西,)
phpspreadsheet-工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西)1. composer require phpoffice/phpspreadsheet2. 看最下面的两 ...
- C#调用c++类的导出函数
C# 需要调用C++东西,但是有不想做成COM,就只好先导出类中的函数处理. 不能直接调用,需单独导出函数 参考:http://blog.csdn.net/cartzhang/article/deta ...
- 基于phpExcel写的excel类(导出为Excel)
<?php /* * 类的功能 * 传入二位数组导出excel * 传入excel 导出二位数组 * @author mrwu */ require('PHPExcel.php'); requi ...
- 工具类--Excel 导出poi
实现功能 --批量导出excel 文件,配置一个sheet多少条数据,根据查询数据量的多少确定生成几个sheet页. pom 文件导入ExcelUtils工具包,依赖于poi包. <!-- ht ...
- 使用hutool工具类进行导出
引入依赖为: <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</ ...
- 偷懒小工具 - Excel导出公共类
说明 最近接了一个任务,就是做一个列表的Excel导出功能.并且有很多页面都会使用这个功能. 导出的Excel大体格式如图 很简单的列表,标题加背景色,然后不同类型,显示方式不一样.对齐方式不一样.不 ...
随机推荐
- CodeForces 686A-Free Ice Cream
题目: 儿童排队领冰激凌,给你两个数n,x分别代表接下来有n行与初始的冰激淋数:接下来n行,每行有一个字符('+'or‘-’),还有一个整数d,+d表示新增的冰激 凌数(由搬运工搬运到此),-d表示儿 ...
- C++ 之 const 随笔记
const关键字,相信对C语言有所了解的同学都应该知道他的作用:1.修饰常量,2.修饰指针,3.修饰函数 1.修饰常量 const修饰后的变量被定义为常量 2.修饰指针 当用const修饰指针的时候, ...
- In close() at SocketHttpClientConnection in Android
In close() at SocketHttpClientConnection Error In Android. when i tried to acess network data on Mai ...
- BZOJ 1833 count 数字计数
sb数位dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
- ios 各种技术
1.NSlog 发布后不打印 #ifdef DEBUG// 如果有DEBUG这个宏就编译下面一句代码 #define DDLog(...) NSLog(__VA_ARGS__) #else // 如 ...
- 黑马程序员:Java编程_多线程
=========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 进程:是一个正在执行中的程序.每一个进程执行都有一个执行顺序.该顺序是一个执行路 ...
- python数据结构与算法——栈
# 栈# 其实python里面的list就可以当栈使用啦,用collections.deque也可以# 1. 入栈 list.append(item)# 2. 出栈 item = list.pop() ...
- Java-->Json解析网页数据
--> 官方解析jar包: 链接:http://pan.baidu.com/s/1pKDnXKv 密码:694d --> 离线Json格式检测工具: 链接:http://pan.baidu ...
- nginx+tomcat集群配置(4)--rewrite规则和多应用根目录设定思路
前言: nginx中有一块很重要的概念, 就是rewrite规则. 它会对URL进行修改, 然后进行内部的重定向. rewrite授予了nginx更多的自由, 使得后级服务的接入更加地方便. 本文将简 ...
- Jena TDB 102
1 Introduction TDB is a RDF storage of Jena. official guarantees and limitations TDB support full ra ...