有没有更快的方式将Excel中的行导出到SQL Server?

我有8,500行36列,我需要插入到SQL Server表,目前需要9分钟。 我想知道是否有更快的方法来做到这一点。 我已经阅读了批量插入,但我保存,作为最后的手段。

目前我如何做,是我使用每个循环,并有一个if语句来检查每个单元格格式是否正确(可能是其缓慢的原因,但我需要它)。 我把它们连接在一起,立即出口。

提前致谢。

首先,您需要在您的SQl-server实例上安装此驱动程序([Microsoft.ACE.OLEDB.12.0])

这里下载 – 驱动程序

然后在SQL-Sever上执行这个查询:

select * into mytable from OpenDataSource('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=1;Database=c\desktop\myExcelfile.xlsm')...[mySheet$] 

不要删除这个字符“$”

您可以通过使用数组并通过使用缓冲区来构build查询来显着提高性能。 它应该只需要几毫秒来build立一个10000行的查询。 这里是一个例子:

 Sub build_query() Dim buffer As String, length&, data(), r&, c& ' load the data in an array ' data = Range("A2:C5") ' create the string buffer ' buffer = String$(2048, vbNullChar) ' write the header ' Append buffer, length, "INSERT INTO [MyTable] ('ColA', 'ColB', 'ColB') Values(" & vbCrLf ' iterate each cell ' For r = 1 To UBound(data) Append buffer, length, "(" For c = 1 To UBound(data, 2) If c > 1 Then Append buffer, length, "," ' convert each type ' Select Case VarType(data(r, c)) Case vbDate: Append buffer, length, format(data(r, c), "'yyyy-mm-dd HH:nn:ss'") Case vbString: Append buffer, length, "'" & data(r, c) & "'" Case Else: Append buffer, length, CStr(data(r, c)) End Select Next Append buffer, length, ")," & vbCrLf Next ' write the footer ' length = length - 3 ' remove comma ' Append buffer, length, ");" ' display the result ' Debug.Print Left$(buffer, length) End Sub Private Sub Append(buffer$, length&, text$) length = length + Len(text) If length > Len(buffer) Then buffer = buffer & String$(Len(buffer) * (length \ Len(buffer)), vbNullChar) End If Mid$(buffer, length - Len(text) + 1) = text End Sub 

我创build了一个Excel加载项,您可以将Excel数据导出到SQL Server。 做了8500行和40列的testing,并花了4.5秒将Excel中的行插入到SQL Server中。

我把它作为一个商业产品出售,但是如果这是一个时间有限的工作,你可以免费使用全function的试用版(如果你需要的话,只需要给我发一封电子邮件,我会延长你的试用期)。

你可以在这里下载http://sqlspreads.com/download-sql-spreads/