1
//以下的源码里分别给出了将DBF,XLS,XML,MDB文件导入C#DataGrid的方法,供各位参考。
2
3
//PutInDataSet.cs的源码
4
using System;
5
using System.Data.Odbc;
6
using System.Data.OleDb;
7
using System.Data;
8
using System.Collections;
9
10
namespace PutInDataSet
11
{
12
/// <summary>
13
/// DataSetTransIn 的摘要说明。
14
/// </summary>
15
public class PutInDataSet
16
{
17
/// <summary>
18
/// 传入的文件变量
19
/// </summary>
20
private DataSet my_Ds;//存放文件的数据集
21
private string my_Err;//错误信息
22
private string my_TableName;//传入的文件名
23
private TableType my_TableType;//传入的文件类型
24
private string my_TablePath;//传入的文件路径
25
private int my_TableIndex;//表的索引
26
OleDbCommandBuilder my_Builder;//命令串
27
28
/// <summary>
29
/// 数据库连接变量
30
/// </summary>
31
private string my_StrConnection;//连接字符串
32
private string my_StrSelect;//select语句
33
34
/// <summary>
35
/// 可以处理的文件类型
36
/// </summary>
37
public enum TableType
38
{
39
MDB,XLS,DBF,DOC,TXT,XML,HTML
40
}
41
42
public PutInDataSet(string TablePath,string TableName,TableType TableType)
43
{
44
///<summary>
45
///获得传入的路径,文件名及文件类型;
46
///</summary>
47
this.my_TablePath=TablePath;//路径
48
this.my_TableName=TableName;//文件名
49
this.my_TableType=TableType;//文件类型
50
}
51
52
public DataSet Convert()
53
{
54
DataSet iRtn_Ds=new DataSet();
55
switch (this.my_TableType)
56
{
57
case TableType.DBF:
58
iRtn_Ds = this.DbfToDs();
59
break;
60
61
case TableType.MDB:
62
iRtn_Ds = this.MdbToDs();
63
break;
64
65
case TableType.XLS:
66
iRtn_Ds = this.XlsToDs();
67
break;
68
69
case TableType.XML:
70
iRtn_Ds = this.XMLToDs();
71
break;
72
}
73
return iRtn_Ds;
74
}
75
76
///<summary>
77
///将DBF文件放入DataSet
78
///</summary>
79
private DataSet DbfToDs()
80
{
81
//数据库连接定义
82
OdbcConnection my_conn; //数据连接
83
OdbcDataAdapter my_Adapter;//数据适配器
84
85
//数据库连接
86
this.my_StrConnection= "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + this.my_TablePath;
87
this.my_StrSelect="SELECT * FROM " + this.my_TableName;
88
my_conn = new OdbcConnection(this.my_StrConnection);
89
my_conn.Open();
90
my_Adapter = new OdbcDataAdapter(this.my_StrSelect,my_conn);
91
this.my_Ds=new DataSet();
92
93
//填充数据集
94
my_Adapter.Fill(this.my_Ds,this.my_TableName);
95
return this.my_
96
97
98
Ds;
99
}
100
101
///<summary>
102
///将MDB文件放入DataSet
103
///</summary>
104
private DataSet MdbToDs()
105
{
106
//数据库连接定义
107
OleDbConnection my_conn;
108
OleDbDataAdapter my_Adapter;
109
110
//数据库连接
111
this.my_StrConnection= "Provider=Microsoft.JET.OLEDB.4.0;data source=" + this.my_TablePath;
112
this.my_StrSelect="SELECT * FROM " + this.my_TableName;
113
my_conn = new OleDbConnection(this.my_StrConnection);
114
my_conn.Open();
115
my_Adapter = new OleDbDataAdapter(this.my_StrSelect,my_conn);
116
this.my_Ds=new DataSet();
117
118
//填充数据集
119
my_Adapter.Fill(this.my_Ds,this.my_TableName);
120
return this.my_Ds;
121
}
122
123
///<summary>
124
///将XML文件放入DataSet
125
///</summary>
126
private DataSet XMLToDs()
127
{
128
129
//填充数据集
130
this.my_Ds=new DataSet();
131
this.my_Ds.ReadXML(this.my_TablePath+this.my_TableName,XMLReadMode.ReadSchema);
132
this.my_Ds.DataSetName="XMLData";
133
return this.my_Ds;
134
}
135
136
///<summary>
137
///将Excel文件放入DataSet
138
///</summary>
139
private DataSet XlsToDs()
140
{
141
OleDbConnection my_conn;
142
OleDbDataAdapter my_Adapter;
143
144
//数据库连接
145
this.my_StrConnection= "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=Excel 8.0;data source="+this.my_TablePath+this.my_TableName;
146
this.my_StrSelect="SELECT * FROM [SHEET1$]";
147
my_conn = new OleDbConnection(this.my_StrConnection);
148
my_conn.Open();
149
my_Adapter = new OleDbDataAdapter(this.my_StrSelect,my_conn);
150
this.my_Builder=new OleDbCommandBuilder(my_Adapter);
151
this.my_Ds=new DataSet();
152
153
//填充数据集
154
my_Adapter.Fill(this.my_Ds,"ExcelData");
155
return this.my_Ds;
156
}
157
158
159
}
160
}
161
162
163
//Form_PutInDataSet.cs的源码
164
165
using System;
166
using System.Data;
167
using System.Drawing;
168
using System.Collections;
169
using System.ComponentModel;
170
using System.Windows.Forms;
171
using DataAccess.SysManage;
172
using BusinessRules;
173
using DataSetTrans;
174
175
176
namespace WinForm.Common
177
{
178
/// <summary>
179
/// FormDesktop 的摘要说明。
180
/// </summary>
181
public class FormDesktop : System.Windows.Forms.Form
182
{
183
private WinForm.Common.DeskTop deskTop1;
184
private System.Windows.Forms.Button button1;
185
/// <summary>
186
/// 必需的设计器变量。
187
/// </summary>
188
private System.ComponentModel.Container components = null;
189
190
private DataSet m_ds = new DataSet();
191
private System.Windows.Forms.DataGrid dataGrid1; //数据源
192
private string m_TableName; //外部文件名称
193
194
public FormDesktop()
195
{
196
//
197
// Windows 窗体设计器支持所必需的
198
//
199
InitializeComponent();
200
201
//
202
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
203
//
204
}
205
206
/// <summary>
207
/// 清理所有正在使用的资源。
208
/// </summary>
209
protected override void Dispose( bool disposing )
210
{
211
if( disposing )
212
{
213
if(components != null)
214
{
215
components.Dispose();
216
}
217
}
218
base.Dispose( disposing );
219
}
220
221
Windows Form Designer generated code
280
281
private void FormDesktop_Load(object sender, System.EventArgs e)
282
{
283
284
}
285
286
/// <summary>
287
/// 当窗口改变大小时自动居中。
288
/// </summary>
289
private void FormDesktop_Resize(object sender, System.EventArgs e)
290
{
291
if (this.WindowState != FormWindowState.Minimized)
292
{
293
if (this.Width > this.deskTop1.Width && this.Height > this.deskTop1.Height )
294
{
295
this.deskTop1.Left = (this.Width - this.deskTop1.Width) / 2;
296
this.deskTop1.Top = (this.Height- this.deskTop1.Height)/ 2;
297
}
298
}
299
}
300
301
private void button1_Click(object sender, System.EventArgs e)
302
{
303
DataSet out_Ds=new DataSet();
304
PutInDataSet obj=new PutInDataSet("文件路径","文件名",PutInDataSet.TableType.文件格式);//调用PutInDataSet类
305
out_Ds=obj.Convert();//转换到DataSet中
306
this.dataGrid1.DataSource=out_Ds.Tables["表名"];//在DataGrid中显示
307
308
}
309
}
310
}
311
//以下的源码里分别给出了将DBF,XLS,XML,MDB文件导入C#DataGrid的方法,供各位参考。 2

3
//PutInDataSet.cs的源码 4
using System; 5
using System.Data.Odbc; 6
using System.Data.OleDb; 7
using System.Data; 8
using System.Collections; 9

10
namespace PutInDataSet 11
{ 12
/// <summary> 13
/// DataSetTransIn 的摘要说明。 14
/// </summary> 15
public class PutInDataSet 16
{ 17
/// <summary> 18
/// 传入的文件变量 19
/// </summary> 20
private DataSet my_Ds;//存放文件的数据集 21
private string my_Err;//错误信息 22
private string my_TableName;//传入的文件名 23
private TableType my_TableType;//传入的文件类型 24
private string my_TablePath;//传入的文件路径 25
private int my_TableIndex;//表的索引 26
OleDbCommandBuilder my_Builder;//命令串 27

28
/// <summary> 29
/// 数据库连接变量 30
/// </summary> 31
private string my_StrConnection;//连接字符串 32
private string my_StrSelect;//select语句 33

34
/// <summary> 35
/// 可以处理的文件类型 36
/// </summary> 37
public enum TableType 38
{ 39
MDB,XLS,DBF,DOC,TXT,XML,HTML 40
} 41

42
public PutInDataSet(string TablePath,string TableName,TableType TableType) 43
{ 44
///<summary> 45
///获得传入的路径,文件名及文件类型; 46
///</summary> 47
this.my_TablePath=TablePath;//路径 48
this.my_TableName=TableName;//文件名 49
this.my_TableType=TableType;//文件类型 50
} 51

52
public DataSet Convert() 53
{ 54
DataSet iRtn_Ds=new DataSet(); 55
switch (this.my_TableType) 56
{ 57
case TableType.DBF: 58
iRtn_Ds = this.DbfToDs(); 59
break; 60

61
case TableType.MDB: 62
iRtn_Ds = this.MdbToDs(); 63
break; 64

65
case TableType.XLS: 66
iRtn_Ds = this.XlsToDs(); 67
break; 68

69
case TableType.XML: 70
iRtn_Ds = this.XMLToDs(); 71
break; 72
} 73
return iRtn_Ds; 74
} 75

76
///<summary> 77
///将DBF文件放入DataSet 78
///</summary> 79
private DataSet DbfToDs() 80
{ 81
//数据库连接定义 82
OdbcConnection my_conn; //数据连接 83
OdbcDataAdapter my_Adapter;//数据适配器 84

85
//数据库连接 86
this.my_StrConnection= "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + this.my_TablePath; 87
this.my_StrSelect="SELECT * FROM " + this.my_TableName; 88
my_conn = new OdbcConnection(this.my_StrConnection); 89
my_conn.Open(); 90
my_Adapter = new OdbcDataAdapter(this.my_StrSelect,my_conn); 91
this.my_Ds=new DataSet(); 92

93
//填充数据集 94
my_Adapter.Fill(this.my_Ds,this.my_TableName); 95
return this.my_96

97

98
Ds; 99
} 100

101
///<summary> 102
///将MDB文件放入DataSet 103
///</summary> 104
private DataSet MdbToDs() 105
{ 106
//数据库连接定义 107
OleDbConnection my_conn; 108
OleDbDataAdapter my_Adapter; 109

110
//数据库连接 111
this.my_StrConnection= "Provider=Microsoft.JET.OLEDB.4.0;data source=" + this.my_TablePath; 112
this.my_StrSelect="SELECT * FROM " + this.my_TableName; 113
my_conn = new OleDbConnection(this.my_StrConnection); 114
my_conn.Open(); 115
my_Adapter = new OleDbDataAdapter(this.my_StrSelect,my_conn); 116
this.my_Ds=new DataSet(); 117

118
//填充数据集 119
my_Adapter.Fill(this.my_Ds,this.my_TableName); 120
return this.my_Ds; 121
} 122

123
///<summary> 124
///将XML文件放入DataSet 125
///</summary> 126
private DataSet XMLToDs() 127
{ 128

129
//填充数据集 130
this.my_Ds=new DataSet(); 131
this.my_Ds.ReadXML(this.my_TablePath+this.my_TableName,XMLReadMode.ReadSchema); 132
this.my_Ds.DataSetName="XMLData"; 133
return this.my_Ds; 134
} 135

136
///<summary> 137
///将Excel文件放入DataSet 138
///</summary> 139
private DataSet XlsToDs() 140
{ 141
OleDbConnection my_conn; 142
OleDbDataAdapter my_Adapter; 143

144
//数据库连接 145
this.my_StrConnection= "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=Excel 8.0;data source="+this.my_TablePath+this.my_TableName; 146
this.my_StrSelect="SELECT * FROM [SHEET1$]"; 147
my_conn = new OleDbConnection(this.my_StrConnection); 148
my_conn.Open(); 149
my_Adapter = new OleDbDataAdapter(this.my_StrSelect,my_conn); 150
this.my_Builder=new OleDbCommandBuilder(my_Adapter); 151
this.my_Ds=new DataSet(); 152

153
//填充数据集 154
my_Adapter.Fill(this.my_Ds,"ExcelData"); 155
return this.my_Ds; 156
} 157

158

159
} 160
} 161

162

163
//Form_PutInDataSet.cs的源码 164

165
using System; 166
using System.Data; 167
using System.Drawing; 168
using System.Collections; 169
using System.ComponentModel; 170
using System.Windows.Forms; 171
using DataAccess.SysManage; 172
using BusinessRules; 173
using DataSetTrans; 174

175

176
namespace WinForm.Common 177
{ 178
/// <summary> 179
/// FormDesktop 的摘要说明。 180
/// </summary> 181
public class FormDesktop : System.Windows.Forms.Form 182
{ 183
private WinForm.Common.DeskTop deskTop1; 184
private System.Windows.Forms.Button button1; 185
/// <summary> 186
/// 必需的设计器变量。 187
/// </summary> 188
private System.ComponentModel.Container components = null; 189

190
private DataSet m_ds = new DataSet(); 191
private System.Windows.Forms.DataGrid dataGrid1; //数据源 192
private string m_TableName; //外部文件名称 193

194
public FormDesktop() 195
{ 196
// 197
// Windows 窗体设计器支持所必需的 198
// 199
InitializeComponent(); 200

201
// 202
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 203
// 204
} 205

206
/// <summary> 207
/// 清理所有正在使用的资源。 208
/// </summary> 209
protected override void Dispose( bool disposing ) 210
{ 211
if( disposing ) 212
{ 213
if(components != null) 214
{ 215
components.Dispose(); 216
} 217
} 218
base.Dispose( disposing ); 219
} 220

221
Windows Form Designer generated code 280

281
private void FormDesktop_Load(object sender, System.EventArgs e) 282
{ 283

284
} 285

286
/// <summary> 287
/// 当窗口改变大小时自动居中。 288
/// </summary> 289
private void FormDesktop_Resize(object sender, System.EventArgs e) 290
{ 291
if (this.WindowState != FormWindowState.Minimized) 292
{ 293
if (this.Width > this.deskTop1.Width && this.Height > this.deskTop1.Height ) 294
{ 295
this.deskTop1.Left = (this.Width - this.deskTop1.Width) / 2; 296
this.deskTop1.Top = (this.Height- this.deskTop1.Height)/ 2; 297
} 298
} 299
} 300

301
private void button1_Click(object sender, System.EventArgs e) 302
{ 303
DataSet out_Ds=new DataSet(); 304
PutInDataSet obj=new PutInDataSet("文件路径","文件名",PutInDataSet.TableType.文件格式);//调用PutInDataSet类 305
out_Ds=obj.Convert();//转换到DataSet中 306
this.dataGrid1.DataSource=out_Ds.Tables["表名"];//在DataGrid中显示 307

308
} 309
} 310
} 311


