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
查看全文
相关阅读:
Python Post四种请求方式
Python 字符串转Base64编解码
JS 数组遍历
FineUI MVC 前端获取表格Json通过Post传递后台
C# Json转DataTable
MSSQL 关联更新
Python selenium Message: session not created: This version of ChromeDriver only supports Chrome version 76
FineUI MVC 同级新增页签
Tomcat Tomcat的中文乱码设置
zabbix-4.0-监控服务器的ping告警设置
原文地址:https://www.cnblogs.com/Zoya/p/1553501.html
最新文章
基础算法--快排,归并排序
leetcoede 8 字符串转换整数
中断,异常,系统调用
计算机启动流程
AcWing 最短哈密顿路径(状态压缩dp)
P2921 在农场万圣节(非递归的类似于记忆化搜索的巧妙方法||记忆化搜索||tarjan)
P1064 金明的预算方案(01背包||有依赖的背包)
滑动窗口问题
centos 7 No package python-dev available
centos build-essential 报错
热门文章
阿里云服务器ssh连接经常断开
linux系统添加新用户并赋予相应权限
阿里云服务器上安装java配置jdk
centos7修改系统语言为简体中文
阿里云服务器(ECS)CentOS修改yum源为阿里源
75. 颜色分类
Commando War UVA
P1111 修复公路
Visual Studio 无法在运行时报错
.net WebApi服务
Copyright © 2011-2022 走看看