程序人生
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 19 文章- 0 评论- 154
从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
鹰击长空
阅读(160)
评论(2)
编辑
收藏
所属分类:
WinForm
发表评论
回复
引用
查看
#1楼
2008-04-28 12:33 |
李战
回复
引用
查看
#2楼
2008-05-17 13:20 |
HedgeHog
学习了
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
向地震灾区捐赠爱心
所属分类的其他文章:
·
从Excel中导数据到DataGridView
·
变相实现DataGridView中既有文本框又有按钮的列
博客园首页
社区
<
2008年4月
>
日
一
二
三
四
五
六
30
31
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
1
2
3
4
5
6
7
8
9
10
与我互动
给我发短消息
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我参与的团队
Design & Pattern团队(0/760)
Debug 探索团队(0/26)
开源CLI核心探索团队(0/65)
WCF技术研究团队(0/69)
我的标签
设计模式(8)
datagridview(2)
列合并(1)
既有文本框又有按钮(1)
sql(1)
数据库(1)
随笔分类
WinDbg(5)
(rss)
WinForm(2)
(rss)
设计模式(9)
(rss)
数据库设计(1)
(rss)
随笔档案
2008年5月 (3)
2008年4月 (4)
2008年3月 (10)
文章分类
数据库(1)
(rss)
收藏夹
DataTable(1)
(rss)
Remoting(1)
(rss)
程序员资源(8)
(rss)
对面向对象的一些理解(1)
(rss)
模板设计(1)
(rss)
个人收藏
搜索
积分与排名
积分 - 22914
排名 - 1528
最新评论
1. re: 《Head.First设计模式》的学习笔记(6)--单件模式
public static Singleton Instance 11 { 12 get 13 { 14 if ... (HedgeHog)
2. re: 《Head.First设计模式》的学习笔记(9)--外观模式
很好的例子,学习一下。 (yelloweast)
3. re: 从Excel中导数据到DataGridView
学习了 (HedgeHog)
4. re: 《Head.First设计模式》的学习笔记(9)--外观模式
@ 炭炭 如果为了解耦,那么这些子系统的接口不能暴露。但是如果有特殊需求(比如提高性能),那么子系统的接口也可以暴露,此时外观模式只起到了简化接口的目的,没有降低耦合度。当然对每一个子系统的接口进行包... (鹰击长空)
5. re: 《Head.First设计模式》的学习笔记(9)--外观模式
如果有必要,这些子系统的接口还可以继续暴露给客户,这就是所谓的高级功能(或称为自定义)。 ------------------------ 这样还怎么解耦阿,大哥。有什么东西不能在facade里包一... (炭炭)
阅读排行榜
1. SQL语句优化技术分析(2840)
2. 《Head.First设计模式》的学习笔记(2)--策略模式(2679)
3. 《Head.First设计模式》的学习笔记(4)--装饰者模式(2191)
4. 《Head.First设计模式》的学习笔记(5)--工厂方法模式(2043)
5. 《Head.First设计模式》的学习笔记(6)--单件模式(1892)
评论排行榜
1. SQL语句优化技术分析(27)
2. 《Head.First设计模式》的学习笔记(2)--策略模式(19)
3. 《Head.First设计模式》的学习笔记(4)--装饰者模式(18)
4. 《Head.First设计模式》的学习笔记(6)--单件模式(18)
5. 《Head.First设计模式》的学习笔记(3)--观察者模式(15)