MVC将数据从Excel加载到SQLServer

时间:2010-12-30 16:55:53

标签: excel model-view-controller

我有一个代码可以将Excel中的数据加载到SQLServer表中。它工作正常,但我需要知道是否有最佳方法,我的意思是更快地完成这个过程。 这是因为在Excel文件中我通常有大约7000行,时间加载太长(当然我是逐行加载)。

以下是您分析的代码。

Dim varExcel As Object = CreateObject("Excel.Application")  
Dim varBook As Object = varExcel.Workbooks.Open(varFileName, True, True, , "")  
Dim varSheet As Object = varExcel.Worksheets(1)

Dim varArray As Object = varSheet.Range("A1:A65000").Value  
varCount = 1  
For vari As Integer = 2 To varArray.GetUpperBound(0)  
  If Not String.IsNullOrEmpty(varArray(vari, 1)) Then  
    varCount = varCount + 1  
  Else  
    Exit For  
  End If  
Next  

Try   
  varArray = Nothing  
  varArray = varSheet.Range("A1:L" & varCount).Value  
  For vari As Integer = 2 To varArray.GetUpperBound(0)  
    varCount = vari  

    Dim varSec = funGetSec("mytable")  
    Dim varTemp As New TEMPORAL  
    varTemp.ID_TEMPORAL = varSec.NUMERO_SECUENCIAL  
    If Not varArray(vari, 1) Is Nothing Then varTemp.CEDSOC = varArray(vari, 1).ToString.Trim.ToUpper  
    If Not varMatrizDatos(vari, 2) Is Nothing Then varTemp.DFCOPR = varArray(vari,2).ToString.Trim.ToUpper  
    If Not varArray(vari, 3) Is Nothing Then varTemp.NOMSOC = varArray(vari, 3).ToString.Trim.ToUpper  
    If Not varArray(vari, 4) Is Nothing Then varTemp.DF_FCR = varArray(vari, 4).ToString.Trim.ToUpper  

    If Not varArray(vari, 5) Is Nothing Then If IsNumeric(varArray(vari, 5)) Then varTemp.DFTOVA = CDec(varArray(vari, 5))  
    If NotvarArray(vari, 6) Is Nothing Then If IsNumeric(varArray(vari, 6)) Then varTemp.DFINTE = CDec(varArray(vari, 6))  
    If Not varArray(vari, 7) Is Nothing Then If IsNumeric(varArray(vari, 7)) Then varTemp.K_FALTANTE0 = CDec(varArray(vari, 7))  

    If Not varArray(vari, 8) Is Nothing Then If IsNumeric(varArray(vari, 8)) Then varTemp.CARDIA = CLng(varArray(vari, 8))  
    If Not varArray(vari, 9) Is Nothing Then If IsNumeric(varArray(vari, 9)) Then varTemp.DFNUVA = CLng(varArray(vari, 9))  
    If Not varArray(vari, 10) Is Nothing Then If IsNumeric(varArray(vari, 10)) Then varTemp.BFBECD = CLng(varArray(vari, 10))  

    If Not varArray(vari, 11) Is Nothing Then varTemp.BFBPTX = varArray(vari, 11).ToString.Trim.ToUpper  
    If Not varArray(vari, 12) Is Nothing Then varTemp.A0BFTX = varArray(vari, 12).ToString.Trim.ToUpper  
    varTemp.BFA0CD = CLng(66)  
    varTemp.FECHA = CDate(Now.Date)  

    varEntidades.AddToTEMPORALSet(varTemp)  
    varEntidades.SaveChanges()
  Next

  varExcel.DisplayAlerts = False  
  varBook.Close(SaveChanges:=False)  
  varExcel.Quit()  
  varSheet = Nothing  
  varBook = Nothing  
  varExcel = Nothing  

  ' Exec Stored Procedure '  

  Dim varResp As Long  

  Dim varABC As ABC = GetABC(parIDABC)  
  If Not varEntidades.spABC(parIDABC, varResp, varErrMensaje) Then  
    If Not varEntidades.spErrorABC(varErrMensaje) Then Throw New ArgumentException(varErrMensaje)  
    Throw New ArgumentException(varErrMensaje)  
  End If  

  Catch ex As Exception  
    varExcel.DisplayAlerts = False  
    varBook.Close(SaveChanges:=False)  
    varExcel.Quit()  
    varSheet = Nothing  
    varBook = Nothing  
    varExcel = Nothing  

    If Not varEntidades.spErrorABC(varErrMensaje) Then Throw New ArgumentException(varErrMensaje)  
    Throw New ArgumentException(ex.Message & " - Review Excel file - row number: " & varCount)  
  End Try  

  Return Something

我希望这也有助于其他人。

2 个答案:

答案 0 :(得分:0)

您可以在Excel或SQl Server连接上使用Excel或SQL Server引用,例如:

SELECT Col1, Col2, Col3 INTO 
[ODBC;Description=TEST;DRIVER=SQL Server;SERVER=Some\Instance;Trusted_Connection=Yes;DATABASE=test].TableZ  FROM [Sheet1$]"

您可能需要在SQL Server上启用即席查询,如果您使用的是ACE,则可能需要some other considerations

SELECT * INTO newx
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;HDR=YES;IMEX=1;Database=c:\docs\testdata.xls',
    'SELECT * FROM [Sheet2$]');

答案 1 :(得分:0)

我使用SSIS创建了一个包。这是一个很好的解决方案,从代码我发送参数到Package(Package.dtsx)

大量负荷惊人减少:)