| 发表于: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 | | |
|