中易网

如何实现asp.net中将Excel导入数据库多个表中?

答案:2  悬赏:80  
解决时间 2021-02-16 09:26
如何实现asp.net中将Excel导入数据库多个表中?
最佳答案
用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
全部回答
用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
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
我负责英语广播的一个栏目,此栏目主要是告诉
中和酸性土壤的碱 有哪些
普提地址在哪,我要去那里办事
为什为和男友睡在一起还舌吻了他也没感觉
生活家爱心商行地址在哪,我要去那里办事
美服lol更新完带客户端有多大?
深圳哪家整容中心好
与越南做生意的话,做哪方面比较好?(我在北
日月星养老护理院有什么特色?
现在鸽子多少钱一只
电吉他推弦推全音,半音,一个半音,要推到几
枣阳市巨星棉业有限公司地址在哪,我要去那里
(丰城服务区)停车场地址有知道的么?有点事
微信公众号如何快速推广?
戴尔T420塔式服务器能够卧倒放置吗?
推荐资讯
长兴农商银行24小时自助银行(三贞路与振兴大
某商场服装部销售一种名牌衬衫,平均每天可售
我是一名网页设计师,在某个网络公司上了八天
什么是qq蛋仔qy粉?
爱手爱脚美甲中心地址在什么地方,我要处理点
请问有谁知道dede网站LOGO在哪修改
隆化县偏坡营满族乡偏坡营中学地址在什么地方
陈庄小学(安庆桐城市)怎么去啊,我要去那办事
罗庄遵铭博裔新城北门这个地址在什么地方,我
陆金所和火理财哪个好
大家说这张是1988版的黎巴嫩的1000。 我想知
周平王迁都后的周朝分为什么时期
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?