zoukankan
html css js c++ java
共享GridView DataGrid DataTable导出到Excel代码
下面是一个导出Excel文件的代码。
code
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Data;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
namespace
Test
{
/**/
///
<summary>
///
重载样例
///
</summary>
public
class
GridViewExport : ExcelExport
{
public
GridViewExport(DataTable dt)
:
base
(dt,
true
)
{
}
public
override
void
GridViewBoundEvent(
object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType
==
DataControlRowType.DataRow)
{
//
e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
//
e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
/**/
///
<summary>
///
重载样例
///
</summary>
public
class
DataGridExport : ExcelExport
{
public
DataGridExport(DataTable dt)
:
base
(dt,
false
)
{
//
不使用动态列,就在这个地方设置表格和列
//
BoundColumn bc = new BoundColumn();
//
bc.DataField = "company";
//
bc.HeaderText = "你好";
//
oDG.Columns.Add(bc);
}
/**/
///
<summary>
///
设置列的样式
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
public
override
void
DataGridBoundEvent(
object
sender, DataGridItemEventArgs e)
{
if
(e.Item.ItemType
==
ListItemType.Item
||
e.Item.ItemType
==
ListItemType.AlternatingItem)
{
//
e.Item.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
//
e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
public
abstract
class
ExcelExport
{
protected
ExcelExport(DataTable dt,
bool
IsGV)
{
if
(IsGV)
{
_IsGV
=
true
;
gv
=
new
GridView();
gv.RowDataBound
+=
new
GridViewRowEventHandler(gv_RowDataBound);
gv.DataSource
=
dt;
}
else
{
oDG
=
new
DataGrid();
oDG.ItemDataBound
+=
new
DataGridItemEventHandler(oDG_ItemDataBound);
oDG.DataSource
=
dt;
}
}
void
gv_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
GridViewBoundEvent(sender, e);
}
protected
DataGrid oDG
=
null
;
protected
GridView gv
=
null
;
private
bool
_IsGV
=
false
;
/**/
///
<summary>
///
如果是GridView就是True
///
</summary>
public
bool
IsGridView
{
get
{
return
_IsGV;
}
}
/**/
///
<summary>
///
导出Excel数据
///
</summary>
public
void
Export()
{
if
(_IsGV)
{
if
(gv.Columns.Count
==
0
)
gv.AutoGenerateColumns
=
true
;
else
gv.AutoGenerateColumns
=
false
;
gv.AllowPaging
=
false
;
gv.DataBind();
ExportData(gv);
}
else
{
if
(oDG.Columns.Count
==
0
)
oDG.AutoGenerateColumns
=
true
;
else
{
oDG.AutoGenerateColumns
=
false
;
}
oDG.DataBind();
ExportData(oDG);
}
}
/**/
///
<summary>
///
导出DataGrid为Excel
///
</summary>
///
<param name="dg"></param>
public
static
void
Export(DataGrid dg)
{
if
(dg
==
null
)
throw
new
Exception(
"
传入的DataGrid为空。
"
);
ExportData(dg);
}
private
static
void
ExportData(
object
ob)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=Excel.xls
"
);
HttpContext.Current.Response.Charset
=
"
GB2312
"
;
HttpContext.Current.Response.ContentEncoding
=
System.Text.Encoding.GetEncoding(
"
GB2312
"
);
HttpContext.Current.Response.ContentType
=
"
application/ms-excel
"
;
//
image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader(
"
content-disposition
"
,
"
attachment; filename=MyExcelFile.xls
"
);
HttpContext.Current.Response.ContentType
=
"
application/excel
"
;
System.IO.StringWriter sw
=
new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw
=
new
System.Web.UI.HtmlTextWriter(sw);
if
(ob
as
GridView
!=
null
)
((GridView)ob).RenderControl(htw);
if
(ob
as
DataGrid
!=
null
)
((DataGrid)ob).RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
/**/
///
<summary>
///
导出GridView为Excel
///
</summary>
///
<param name="gv"></param>
public
static
void
Export(GridView gv)
{
if
(gv
==
null
)
throw
new
Exception(
"
传入的GridView为空。
"
);
ExportData(gv);
}
void
oDG_ItemDataBound(
object
sender, DataGridItemEventArgs e)
{
DataGridBoundEvent(sender, e);
}
/**/
///
<summary>
///
设置DataGrid列的样式
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
public
virtual
void
DataGridBoundEvent(
object
sender, DataGridItemEventArgs e)
{
}
/**/
///
<summary>
///
设置GridView列的样式
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
public
virtual
void
GridViewBoundEvent(
object
sender, GridViewRowEventArgs e)
{
}
}
}
查看全文
相关阅读:
php对接网易云信视频直播
python基础--1.数据操作
pytest自动化7:assert断言
pytest自动化6:pytest.mark.parametrize装饰器--测试用例参数化
pytest自动化5:pytest-html插件--生成html报告
pytest自动化4:fixture之yield实现teardown
pytest自动化3:fixture之conftest.py实现setup
pytest自动化2:测试用例setup和teardown
pytest自动化1:兼容unittest代码实例
排序
原文地址:https://www.cnblogs.com/LifelongLearning/p/957245.html
最新文章
Spring WebFlux 学习笔记
Spring WebFlux 学习笔记
如何高效阅读技术类书籍?
SpringBoot实现国际化i18n功能
作为程序员,你可开发哪些有趣项目?
程序员老外通过编程赚钱的10个途径
成功的软件工程师共有的10个习惯和技能
面试:说说Java反射中获取Class对象三种方式的区别?
一篇文章全面了解Java反射机制
Java动态代理之一CGLIB详解
热门文章
Java14发布,16大新特性,代码更加简洁明快
Java14发布,16大新特性,代码更加简洁明快
php抽奖功能
php企业微信获取员工userid以及打卡信息
thinkphp phpexcel导出返回乱码
php使用ffmpeg获取上传的视频的时长,码率等信息
php微信小程序生成二维码,出现乱码,{"errcode":44002,"errmsg":"empty post data"},'{"errcode":41001,"errmsg":"access_token missing hint: [OoC.2a0822e255]"}',以及其他的坑
微信小程序页面3秒后自动跳转
thinkphp+redis实现秒杀,缓存等功能
php网易云信im即时通讯和聊天室
Copyright © 2011-2022 走看看