zoukankan
html css js c++ java
asp.net导出数据到Excel
Code
1
Sql数据列表导出到EXCEL
#region
Sql数据列表导出到EXCEL
2
/**/
///
<summary>
3
///
<param name="queryStr">
sql语句
</param>
4
///
<param name="connectionString">
l数据库连接
</param>
5
///
<param name="ExcelFileName">
Excel文件名
</param>
6
///
</summary>
7
public
static
bool
ToExcel(
string
queryStr,
string
ExcelFileName)
8
{
9
//
TODO: implement
10
DataSet ds
=
new
DataSet();
11
ds
=
SqlHelper.ExecuteDataset(SqlHelper.conn, CommandType.Text, queryStr);
12
//
queryStr语句生成DataSet
13
14
DataTable dt
=
new
DataTable();
15
dt
=
ds.Tables[
0
];
16
17
HttpContext context
=
HttpContext.Current;
18
context.Response.Clear();
19
context.Response.Buffer
=
true
;
20
context.Response.Charset
=
"
GB2312
"
;
21
22
System.Globalization.CultureInfo myCItrad
=
new
System.Globalization.CultureInfo(
"
ZH-CN
"
,
true
);
23
//
设置区域性信息
24
StringWriter sw
=
new
StringWriter(myCItrad);
25
//
定义字符串写入流对象
26
27
string
header
=
""
;
28
29
foreach
(DataColumn column
in
dt.Columns)
30
{
31
header
+=
"
"
+
column.Caption.ToString()
+
"
\t
"
;
32
}
33
34
if
(header
!=
""
)
35
header
=
header.Remove(
0
,
1
)
+
"
"
;
36
sw.WriteLine(header);
37
38
foreach
(DataRow dr
in
dt.Rows)
39
{
40
string
record
=
""
;
41
for
(
int
i
=
0
; i
<
dt.Columns.Count; i
++
)
42
{
43
record
+=
"
"
+
dr[i].ToString().Replace(
"
"
,
"
"
).ToString()
+
"
\t
"
;
44
}
45
46
if
(record
!=
""
)
47
record
=
record.Remove(
0
,
1
)
+
"
"
;
48
sw.WriteLine(record);
49
}
50
sw.Close();
51
context.Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
HttpUtility.UrlEncode(ExcelFileName, System.Text.Encoding.UTF8)
+
"
.xls
"
);
52
context.Response.ContentType
=
"
application/ms-excel
"
;
53
context.Response.ContentEncoding
=
System.Text.Encoding.GetEncoding(
"
GB2312
"
);
54
context.Response.Write(sw);
55
context.Response.End();
56
return
true
;
57
}
58
#endregion
查看全文
相关阅读:
正则表达式获取远程网页
Devexpress 常见问题
CSS 带显示隐藏左部页面按钮
CSS 技巧积累
SQL 常用操作
重置 自增字段 起始值 和 步长
Devexpress TreeList
Devexpress GridControl
JS常用
ajax跨域请求
原文地址:https://www.cnblogs.com/MyFavorite/p/1140560.html
最新文章
【转】C#实现窗体中所有控件跟随窗体尺寸的自由变换
mssql索引视图无法对视图创建 索引,因为该视图未绑定到架构
asp.net 获取IP
《中国天气预报》城市编号一览表
查询引用 表,视图,的所有存储过程
简繁体在线切换JS插件
(Microsoft) Visual Studio LightSwitch
[惊!] IE 10.0,你没看错! IE10 Platform Preview 1出来啰~
[习题]如何触发 GridView 身体里面的「子控件」的事件 (ASP.NET案例精编 / 清华大学出版社 Ch.10/11两章的补充)
在纯AS项目中嵌入swf资源
热门文章
先熟悉nasm 【3】
DOS下COM文件和EXE文件的比较
linux采用gcc编译c, 如何知道库所对应的名称?
关于tink的碰撞检测类【2】
IT版奋斗
vimrc文件是让vim执行的
debian下装jre
debian下安装NVIDIA驱动
FlashDevelop开发手记(一)
Devexpress 控件
Copyright © 2011-2022 走看看