程序人生
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 20 文章- 0 评论- 184
从Excel中导数据到DataGridView
最近项目中需要从Excel中导数据到DataGridView中,所以写了一个通用类,代码如下:
1
public
class
ExcelToDatableHelper
2
{
3
private
static
ExcelToDatableHelper instance
=
null
;
4
private
static
Object locker
=
new
Object();
5
6
public
static
ExcelToDatableHelper Instance
7
{
8
get
9
{
10
if
(instance
==
null
)
11
{
12
lock
(locker)
13
{
14
if
(instance
==
null
)
15
{
16
instance
=
new
ExcelToDatableHelper();
17
}
18
}
19
}
20
return
instance;
21
}
22
}
23
24
/**/
///
<summary>
25
///
Reads the excel.
26
///
</summary>
27
///
<param name="strFileName">
Name of the file.
</param>
28
///
<param name="sheetName">
Name of the sheet.
</param>
29
///
<param name="colName">
Name of the col.
</param>
30
///
<returns></returns>
31
public
DataSet ReadExcel(
string
strFileName,
string
sheetName, List
<
string
>
colName)
32
{
33
string
strConnection
=
"
Provider=Microsoft.Jet.OLEDB.4.0;Data Source =
"
+
strFileName
+
"
;Extended Properties = Excel 8.0
"
;
34
OleDbConnection oleConnection
=
new
OleDbConnection(strConnection);
35
StringBuilder strColName
=
new
StringBuilder(
""
);
36
string
myStrColName
=
""
;
37
if
(colName.Count
>
0
)
38
{
39
foreach
(
string
str
in
colName)
40
{
41
strColName.Append(str);
42
strColName.Append(
"
,
"
);
43
}
44
myStrColName
=
strColName.ToString().Substring(
0
, strColName.Length
-
1
);
//
去掉","
45
}
46
try
47
{
48
oleConnection.Open();
49
DataSet dsRead
=
new
DataSet();
50
OleDbDataAdapter oleAdper
=
new
OleDbDataAdapter(
"
SELECT
"
+
myStrColName
+
"
FROM [
"
+
sheetName
+
"
$]
"
, oleConnection);
51
oleAdper.Fill(dsRead,
"
result
"
);
52
return
dsRead;
53
}
54
catch
(Exception ex)
55
{
56
MessageBox.Show(ex.ToString());
57
return
null
;
58
}
59
finally
60
{
61
oleConnection.Close();
62
}
63
}
64
65
/**/
///
<summary>
66
///
Opens the file.
67
///
</summary>
68
///
<returns></returns>
69
public
string
OpenFile()
70
{
71
OpenFileDialog openFileDialog
=
new
OpenFileDialog();
72
openFileDialog.InitialDirectory
=
Application.StartupPath;
73
//
openFileDialog.Filter="文本文件|*.*|Excel文件|*.xls|C#文件|*.cs|所有文件|*.*";
74
openFileDialog.Filter
=
"
Excel文件|*.xls
"
;
75
openFileDialog.RestoreDirectory
=
true
;
76
openFileDialog.Title
=
"
打开文件
"
;
77
openFileDialog.FilterIndex
=
1
;
78
if
(openFileDialog.ShowDialog()
==
DialogResult.OK)
79
{
80
return
(openFileDialog.FileName);
81
}
82
else
return
String.Empty;
83
}
84
}
Demo下载:
/Files/wxj1020/ExcelToDatatable.rar
Tag标签:
datagridview
posted @ 2008-04-28 10:12
鹰击长空
阅读(222)
评论(2)
编辑
收藏
所属分类:
WinForm
发表评论
回复
引用
查看
#1楼
2008-04-28 12:33 |
李战
回复
引用
查看
#2楼
2008-05-17 13:20 |
HedgeHog
学习了
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
另存
打印
所属分类的其他文章:
·
从Excel中导数据到DataGridView
·
变相实现DataGridView中既有文本框又有按钮的列
最新IT新闻:
·
微软CFO:将投入更多资金发展搜索业务
·
微软发布Fiji 取名Media Center TV Pack 2008
·
Firefox发布升级版本3.0.1 修复数个安全问题
·
谷歌首页也变了:在线翻译取代热榜
·
旧金山工程师发动网络政变 系统至今高危运行
博客园新闻频道
博客园首页
社区
公告
<
2008年5月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
与我联系
发短消息
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我参加的小组
Ext 2.0
宁波.net俱乐部
WinForms
我参与的团队
Design & Pattern团队(0/808)
宁波.NET俱乐部(4/56)
Debug 探索团队(0/44)
开源CLI核心探索团队(0/80)
WCF技术研究团队(0/116)
经验汇(0/22)
我的标签
设计模式(8)
datagridview(2)
列合并(1)
既有文本框又有按钮(1)
sql(1)
数据库(1)
代码管理器(1)
随笔分类
WinDbg(5)
(rss)
WinForm(2)
(rss)
代码管理器(1)
(rss)
设计模式(9)
(rss)
数据库设计(1)
(rss)
随笔档案
2008年6月 (1)
2008年5月 (3)
2008年4月 (4)
2008年3月 (10)
文章分类
数据库(1)
(rss)
收藏夹
DataTable(1)
(rss)
Remoting(1)
(rss)
程序员资源(8)
(rss)
对面向对象的一些理解(1)
(rss)
模板设计(1)
(rss)
个人收藏
搜索
积分与排名
积分 - 29222
排名 - 1318
最新评论
1. re: 打造属于自己的代码管理器之需求分析篇
谢谢大家支持,不过最近公司项目紧,没时间做。 (鹰击长空)
2. re: 打造属于自己的代码管理器之需求分析篇
支持,早就想自己做一个了,这种工具比较少见啊
加油! (daoyuly)
3. re: 《Head.First设计模式》的学习笔记(7)--命令模式
很不错 (秋色)
4. re: 打造属于自己的代码管理器之需求分析篇
各位为什么不加设一台subversion服务器,然后将你的代码提交上去呢,服务器就是你的代码库,而tortoriseSVN就是你访问代码的代码获取工具 [不过subversion没有提供查询功能,是不... (v0860)
5. re: 打造属于自己的代码管理器之需求分析篇
加你在宁波.net俱乐部了 (董昊(昊子))
阅读排行榜
1. SQL语句优化技术分析(3386)
2. 《Head.First设计模式》的学习笔记(2)--策略模式(2886)
3. 《Head.First设计模式》的学习笔记(4)--装饰者模式(2329)
4. 《Head.First设计模式》的学习笔记(5)--工厂方法模式(2182)
5. 《Head.First设计模式》的学习笔记(6)--单件模式(2006)
评论排行榜
1. SQL语句优化技术分析(28)
2. 打造属于自己的代码管理器之需求分析篇(24)
3. 《Head.First设计模式》的学习笔记(6)--单件模式(19)
4. 《Head.First设计模式》的学习笔记(2)--策略模式(19)
5. 《Head.First设计模式》的学习笔记(4)--装饰者模式(18)