zoukankan
html css js c++ java
导出Excel并设置样式
有时我们需要在datagrid里进行一些操作,比如多层表头,比如隐藏相同的列等等,很多朋友喜欢用OWC或其它方法来导出excel,其实还可以直接由datagrid导出为excel格式。
导出Excel
#region
导出Excel
/**/
///
<summary>
///
方法编号:07
///
方法名称:ExportToExcel
///
内容摘要:将DataGrid导出为Excel文件,结构与DataGrid相同;引用此方法,须传入page对象
///
算法思路:将DataGrid render并以Excel的格式输出给客户端;
///
须取消分页并重新执行绑定后调用此方法
///
</summary>
public
void
ExportToExcel(DataGrid m_grid,System.Web.UI.Page _page)
{
_page.Response.Clear();
//
清除缓冲区流中的所有内容输出
_page.Response.Buffer
=
true
;
_page.Response.Charset
=
"
GB2312
"
;
//
设置输出流的http字符集
//
生成文件名
StringBuilder m_strName
=
new
StringBuilder();
m_strName.Append(DateTime.Now.Year.ToString()).Append(DateTime.Now.Month.ToString())
.Append(DateTime.Now.Day.ToString()).Append(DateTime.Now.Hour.ToString())
.Append(DateTime.Now.Minute.ToString()).Append(DateTime.Now.Second.ToString());
_page.Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=ExportData
"
+
m_strName.ToString()
+
"
.xls
"
);
_page.Response.ContentEncoding
=
System.Text.Encoding.GetEncoding(
"
GB2312
"
);
//
设置输出流为简体中文
//
设置输出文件类型为excel文件
_page.Response.ContentType
=
"
application/ms-excel
"
;
//
关闭保存视图状态
_page.EnableViewState
=
false
;
System.Globalization.CultureInfo m_CI
=
new
System.Globalization.CultureInfo(
"
ZH-CN
"
,
true
);
//
区域设置
System.IO.StringWriter m_stringWriter
=
new
System.IO.StringWriter(m_CI);
System.Web.UI.HtmlTextWriter m_htmlTextWriter
=
new
System.Web.UI.HtmlTextWriter(m_stringWriter);
//
将m_grid中的内容输出到m_htmlTextWriter
m_grid.RenderControl(m_htmlTextWriter);
_page.Response.Write(m_stringWriter.ToString());
//
将当前所有缓冲的输出发送到客户端,并停止该页执行
_page.Response.End();
}
/**/
///
<summary>
///
方法编号:08
///
方法名称:ExportToExcel
///
内容摘要:将DataGrid导出为Excel文件,结构与DataGrid相同;引用此方法,须传入page对象
///
算法思路:将DataGrid render并以Excel的格式输出给客户端;
///
须取消分页并重新执行绑定后调用此方法
///
</summary>
public
void
ExportToExcel(DataGrid m_grid)
{
this
.ExportToExcel(m_grid,
this
.m_page);
}
/**/
///
<summary>
///
方法编号:09
///
方法名称:SetExcelStyle
///
内容摘要:设置DataGrid导出为EXCEL时的样式
///
</summary>
public
DataGrid SetExcelStyle(DataGrid m_grid)
{
//
给DataGrid增加样式
Table m_tb
=
(Table)m_grid.Controls[
0
];
m_grid.Style.Add(
"
font-size
"
,
"
9pt
"
);
m_grid.Style.Add(
"
font-family
"
,
"
Courier New
"
);
m_grid.Style.Add(
"
heigth
"
,
"
25px
"
);
m_grid.HeaderStyle.Height
=
Unit.Pixel(
25
);
m_grid.GridLines
=
GridLines.Both;
m_tb.BorderStyle
=
BorderStyle.Solid;
m_tb.BorderColor
=
Color.FromKnownColor(KnownColor.Black);
m_grid.ItemStyle.BackColor
=
Color.FromKnownColor(KnownColor.White);
m_grid.FooterStyle.BackColor
=
Color.FromKnownColor(KnownColor.Beige);
m_grid.HeaderStyle.BackColor
=
Color.FromKnownColor(KnownColor.Beige);
m_grid.AlternatingItemStyle.BackColor
=
Color.FromKnownColor(KnownColor.White);
return
m_grid;
}
#endregion
导出Excel
如果datagrid里有linkbutton等服务端控件,则需要手动进行转换为文字,如:
//
将DataGrid里的Linkbutton替换为label对象
foreach
(DataGridItem _item
in
m_outGrid.Items)
{
LinkButton _lb
=
(LinkButton)_item.Cells[
0
].Controls[
0
];
_item.Cells[
0
].Text
=
_lb.Text;
}
愿一路奔跑不退缩,到目前一直从事.Net的B/S,C/S企业应用研发
查看全文
相关阅读:
韩信点兵 中国剩余定理
A -- A. Quailty and Playing Cards 模拟 + 思考
HDU 5904 LCIS DP
Ubuntu上的android sdk提示 bash: ......sdk/platform-tools/adb或者emulator: 没有那个文件或目录 解决笔记
重学数据结构系列之——平衡树之SB Tree(Size Blanced Tree)
重学数据结构系列之——二叉排序树
重学数据结构系列之——二叉树基础
重学数据结构系列之——哈希表
如何判断出栈序列合理性
重学数据结构系列之——栈
原文地址:https://www.cnblogs.com/syveen/p/231772.html
最新文章
bzoj1189
bzoj1934 bzoj2768
bzoj1854
poj 1182用向量的思考模式
codeforces 361A
hdu 1711
codeforces 359A
hdu 1027
codeforces 361B
hdu 361B
热门文章
codeforces 362A找规律
codeforces 363A
codeforces 362B
HDU 2473 Junk-Mail Filter 并查集,虚拟删除操作
C. Destroying Array 并查集,逆向思维
D. Time to Raid Cowavans 分块暴力,感觉关键在dp
HDU 5908 Abelian Period 可以直接用multiset
hdu 3579 Hello Kiki
HDU 1573 X问题
POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
Copyright © 2011-2022 走看看