如何实现asp.net中将Excel导入数据库表中,求完整代码

导入到project_task_effort和project_task_effort_details表中,数据库连接连接名为MetricsConn

用OLEDB进行Excel文件数据读取并返DataSet数据集其几点需要注意:

1.连接字符串参数IMEX 值:
0 is Export mode
1 is Import mode
2 is Linked mode (full update capabilities)
IMEX3值:IMEX=2 EXCEL文档同含字符型数字型比第C列3值2数值型 1231字符型 ABC导入
页面报错库显示数值型123字符型ABC则呈现空值IMEX=1述情况发库确呈现 123 ABC.
2.参数HDR值:
HDR=Yes代表第行标题做数据使用 用HDR=NO则表示第行标题做数据使用系统默认YES
3.参数Excel 8.0
于Excel 97版本都用Excel 8.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#region 获取excel dataset ,即名excel_ds
string excelFilePath = openFileDialog1.FileName;//弹文件选取窗口获取EXCEL文件路径
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
OleDbConnection Oleconn = new OleDbConnection(strConn);
string strExcel = "";
OleDbDataAdapter excelCommand = null;
DataSet excel_ds = new DataSet();
strExcel = "select * from [sheet1$]";
try
{
Oleconn.Open();
excelCommand = new OleDbDataAdapter(strExcel,Oleconn);
excelCommand.Fill(excel_ds,"exdtSource");//dataset
}
catch (System.Exception ex)
{
MessageBox.Show("导入错:" + ex, "错误信息");
}
finally
{
Oleconn.Close();
Oleconn.Dispose();
}
#endregion
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网