您的位置:程序门 -> .net技术 -> vb.net



excel中数据导入到sql server中(要完不成任务了,急!)


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


excel中数据导入到sql server中(要完不成任务了,急!)[已结贴,结贴人:leep2006]
发表于:2007-01-10 21:53:45 楼主
各位大侠     如何把excel中数据导入到sql   server中?
搞了两天了   还是没有搞出来?有没有那位以前做过,给个例子或是思路!小弟不胜感激.
就只能给这么多了,好像没有分了.
发表于:2007-01-11 08:18:011楼 得分:0
我一般在企业管理器里操作~~~
发表于:2007-01-11 08:30:222楼 得分:0
select       *     into   表名    
    from       opendatasource(       'microsoft.jet.oledb.4.0 ',      
            'data       source= "d:\duty.xsl ";user       id=admin;password=;extended       properties=excel       5.0 ')...c$      
发表于:2007-01-11 08:31:043楼 得分:0
insert   into   tablename   select   *  
from   opendatasource(   'microsoft.jet.oledb.4.0 ',
'data   source=c:\id.xls;user   id=sa;password=abc1234;extended   properties=excel   5.0 ')...[sheet1$]
发表于:2007-01-11 10:49:414楼 得分:20
導入datagridview中
try
                        dim   myoledbcn   as   new   system.data.oledb.oledbconnection

                        dim   filename   as   string   =   " "
                        dim   openfiledialog   as   new   openfiledialog
                        openfiledialog.title   =   "打開 "
                        openfiledialog.filter   =   ".xls ¦*.xls "
                        openfiledialog.initialdirectory   =   my.computer.filesystem.specialdirectories.mydocuments

                        if   (openfiledialog.showdialog()   =   system.windows.forms.dialogresult.ok)   then
                                filename   =   openfiledialog.filename
                        end   if
                        if   filename   =   " "   then
                                exit   sub
                        end   if
                        'if   dir(filename)   <>   " "   then
                        '         kill(filename)
                        'end   if
                        myoledbcn.connectionstring   =   "provider=microsoft.jet.oledb.4.0; "   &   _
                        "data   source= "   &   filename   &   "; "   &   _
                        "extended   properties= " "excel   8.0;hdr=yes;imex=1 " " "
                        myoledbcn.close()
                        myoledbcn.open()
                        dim   comstr   as   string
                        comstr   =   "select   *   from   [sheet   1$] "
                        dim   adapter   as   new   oledb.oledbdataadapter(comstr,   myoledbcn)
                        dt.clear()
                        adapter.fill(dt)
                        dt.acceptchanges()
                        me.datagridview1.datasource   =   dt
                        myoledbcn.close()
                catch   ex   as   exception
                        msgbox(err.description,   msgboxstyle.critical,   "system   error! ")
                end   try

寫入數據庫
  try
                        sqlcnn.close()
                        if   me.combobox1.text.trim   =   " "   then
                                msgbox( "請選擇要保存的表,不能選錯! ",   msgboxstyle.critical,   "system   error! ")
                                exit   sub
                        end   if
                        dim   sqlstr   as   string   =   "select   *   from   tl   "   '&   me.combobox1.text.trim
                        if   me.combobox1.text.trim   =   "領料 "   then
                                dim   i   as   integer
                                dim   str   as   string   =   " "
                                if   dt.rows.count   -   1   > =   0   then

                                        for   i   =   0   to   dt.rows.count   -   1
                                                str   =   " "
                                                str   =   "insert   into   tl(tlf026,tlf027,tlf036,tlf037,tlf06,tlf10,ccc23,tlf11,tlf13,tlf902,[ima02 ¦ ¦ima021])   values( ' "   &   _
                                                iif(dt.rows(i).item(1).tostring   =   " ",   "   ",   dt.rows(i).item(1))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(2).tostring   =   " ",   "   ",   dt.rows(i).item(2))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(3).tostring   =   " ",   "   ",   dt.rows(i).item(3))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(4).tostring   =   " ",   "   ",   dt.rows(i).item(4))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(5).tostring   =   " ",   "   ",   dt.rows(i).item(5))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(6).tostring   =   " ",   "   ",   dt.rows(i).item(6))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(7).tostring   =   " ",   "   ",   dt.rows(i).item(7))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(8).tostring   =   " ",   "   ",   dt.rows(i).item(8))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(9).tostring   =   " ",   "   ",   dt.rows(i).item(9))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(10).tostring   =   " ",   "   ",   dt.rows(i).item(10))   &   " ', ' "   &   _
                                                iif(dt.rows(i).item(11).tostring   =   " ",   "   ",   replace(dt.rows(i).item(11),   " ' ",   " "))   &   " ') "
                                                'iif(dt.rows(i).item(12).tostring   =   " ",   "   ",   dt.rows(i).item(12))   &   " ') "
                                                EXECutesql(str)
                                        next
                                        msgbox( "數據保存成功! ",   msgboxstyle.information,   "system   info! ")
                                        sqlcnn.close()
                                end   if
                        end   if


                catch   ex   as   exception
                        sqlcnn.close()
                        msgbox(err.description,   msgboxstyle.critical,   "system   error! ")
                end   try
发表于:2007-01-11 11:54:155楼 得分:0
怎么会报   'sheet   1$ '   is   not   a   valid   name


  comstr   =   "select   *   from   [sheet   1$] "

这条语句对么
发表于:2007-01-11 13:01:366楼 得分:0
兄弟,数据库管理器里有向导的呀,跟着走就完成了!
发表于:2007-01-11 13:05:107楼 得分:10
从excel里读到datatable里,for一下,动态拼一个sql,一行一行的导进去
发表于:2007-01-11 16:58:498楼 得分:0
是啊   在运行的时候,也是显示:       'sheet1$ '   不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。
我检查了,我要打开的excel子表就是默认名 "sheet1 "啊!大虾们能否给解决一下.


快速检索

最新资讯
热门点击