VB.NET中如何将excel中数据导入sql数据库中,十分急!我想知道具体代码,不是通过website实现。而是projec
VB.NET中如何将excel中数据导入sql数据库中,十分急!我想知道具体代码,不是通过website实现。而是project实现。有工程的话请发我114052935@...
VB.NET中如何将excel中数据导入sql数据库中,十分急!我想知道具体代码,不是通过website实现。而是project实现。有工程的话请发我114052935@qq。com
展开
1个回答
展开全部
Dim excelluj As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & (TextBox1.Text & "") & ";Extended Properties='Excel 8.0;HDR=no;IMEX=0'"
Dim comm As OleDb.OleDbConnection = New OleDb.OleDbConnection(excelluj)
comm.Open()
Dim stable As DataTable = comm.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, Nothing)
Dim xlsname As String = "Sheet1$"
xlsname = "[" & xlsname.Replace(" ' ", " ") & "]"
Dim sql As String = "select * from " & xlsname
Dim commxls As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, excelluj)
Dim dos As DataSet = New DataSet()
Dim i As Integer
Dim sqleX As String = ""
Dim kkkl As Integer = 0
commxls.Fill(dos, xlsname)
comm.Close()
'以下是读取数据然后直接写入到CY.txt,你可以直接把数据直接写入到数据库。
Dim Sw As StreamWriter = File.CreateText("D:\My Documents\Visual Studio 2008\Projects\EXECL_TXT\EXECL_TXT\CY.txt")
For i = 0 To dos.Tables(0).Rows.Count - 1
sqleX = "" _
& "'" & dos.Tables(xlsname).Rows(i)(0).ToString & "'," _
& "'" & dos.Tables(xlsname).Rows(i)(1).ToString & "')"
FileOpen(1, "CY.TXT", OpenMode.Output)
FileClose(1)
Sw.WriteLine(sqleX)
Sw.Write("||| ")
Next
Sw.Close()
dos.Dispose()
Dim comm As OleDb.OleDbConnection = New OleDb.OleDbConnection(excelluj)
comm.Open()
Dim stable As DataTable = comm.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, Nothing)
Dim xlsname As String = "Sheet1$"
xlsname = "[" & xlsname.Replace(" ' ", " ") & "]"
Dim sql As String = "select * from " & xlsname
Dim commxls As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, excelluj)
Dim dos As DataSet = New DataSet()
Dim i As Integer
Dim sqleX As String = ""
Dim kkkl As Integer = 0
commxls.Fill(dos, xlsname)
comm.Close()
'以下是读取数据然后直接写入到CY.txt,你可以直接把数据直接写入到数据库。
Dim Sw As StreamWriter = File.CreateText("D:\My Documents\Visual Studio 2008\Projects\EXECL_TXT\EXECL_TXT\CY.txt")
For i = 0 To dos.Tables(0).Rows.Count - 1
sqleX = "" _
& "'" & dos.Tables(xlsname).Rows(i)(0).ToString & "'," _
& "'" & dos.Tables(xlsname).Rows(i)(1).ToString & "')"
FileOpen(1, "CY.TXT", OpenMode.Output)
FileClose(1)
Sw.WriteLine(sqleX)
Sw.Write("||| ")
Next
Sw.Close()
dos.Dispose()
参考资料: QQ:22715663
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询