如何实现asp.net中将Excel导入数据库多个表中?
答案:2 悬赏:80
解决时间 2021-02-16 09:26
- 提问者网友:醉归
- 2021-02-15 09:25
如何实现asp.net中将Excel导入数据库多个表中?
最佳答案
- 二级知识专家网友:说多了都是废话
- 2021-02-15 10:01
用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
#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
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
#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
全部回答
- 1楼网友:湫止没有不同
- 2021-02-15 11:09
用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 exc...用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
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |