会员
周边
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
我立寒风自癫狂
问己之不悉,解己之不惑.
博客园
首页
新随笔
联系
订阅
管理
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
posted @
2009-08-25 11:33
Zoya Qiu
阅读(
223
) 评论(
0
)
编辑
收藏
举报
刷新页面
返回顶部
公告