zoukankan
html css js c++ java
VS2005 ASP.NET C# DataSet导入Excel
新建一个按钮Button1,即可实现DataSet导入Excel功能:
Code
1
protected
void
Button1_Click(
object
sender, EventArgs e)
2
{
3
string
sql_excel
=
"
select * from Web_Processing
"
;
4
string
mystring
=
"
Provider=sqloledb;Data Source=hz;Initial Catalog=Web;User Id=sa;Password=123
"
;
5
OleDbConnection cnn
=
new
OleDbConnection(mystring);
6
OleDbDataAdapter myDa
=
new
OleDbDataAdapter(sql_excel, cnn);
7
DataSet ds
=
new
DataSet();
8
myDa.Fill(ds);
9
cnn.Close();
10
11
String filename
=
""
;
12
ExportDataSetToExcel(ds, filename);
13
}
14
15
public
void
ExportDataSetToExcel(DataSet ds,
string
filename)
16
{
17
HttpResponse response
=
HttpContext.Current.Response;
18
19
//
first let's clean up the response.object
20
response.Clear();
21
response.Charset
=
""
;
22
23
//
set the response mime type for excel
24
response.ContentType
=
"
application/vnd.ms-excel
"
;
25
response.AddHeader(
"
Content-Disposition
"
,
"
attachment;filename=\
""
+ filename +
"
\
""
);
26
27
//
create a string writer
28
using
(System.IO.StringWriter sw
=
new
StringWriter())
29
{
30
using
(HtmlTextWriter htw
=
new
HtmlTextWriter(sw))
31
{
32
//
instantiate a datagrid
33
DataGrid dg
=
new
DataGrid();
34
dg.DataSource
=
ds.Tables[
0
];
35
dg.DataBind();
36
dg.RenderControl(htw);
37
response.Write(sw.ToString());
38
response.End();
39
}
40
}
41
}
42
查看全文
相关阅读:
使用C语言来扩展PHP,写PHP扩展dll
用C语言写PHP扩展 linux
mysql 建索引删除索引命令
xhprof安装&&使用
Burst Windows 2003 VPS安装中文语言包图文教程
yii框架的缓存
rsync的配置与应用
mysql分组排序取前N条记录的最简洁的单条sql !
linux下php以fastcgi模式运行
JS收集<2>:图片上传_限制格式、类型、尺寸
原文地址:https://www.cnblogs.com/Zoya/p/1553501.html
最新文章
windows中控制台中在同一行打印信息
windows批处理命令大全
Ubuntu设置环境变量三法
Visual Studio 2010 移走 ipch 和 sdf
MongoDB学习总结
工具汇总
fmt
fmt标签
wget指令
SP游标
热门文章
在IE6和Firefox里面修改DIV高度
几个.Net开源的CMS系统
学习.NET 之 IReaper
.Text blog的一点点安装心得
网站排名
Reapter嵌套
Ajax学习
AjaxControlToolkit 中的脚本错误: Sys未定义的解决方法
IE与Firefox下对CSS解析的区别
Zend_http_Client学习
Copyright © 2011-2022 走看看