将数据从excel文件导出到Sqlserver 2012

时间:2014-06-09 09:54:08

标签: c# excel

根据我的要求,我想从excel(驻留在网络驱动器上)读取数据并将其导出到sqlserver 2012。 我将使用c#(。net framework 4.5)创建控制台应用程序(exe)..这个控制台 应用程序将安排在Web服务器上使用窗口调度程序每天运行。

你能让我知道做这件事的最佳方法是什么......同时牢记性能。  如果任何人有现成的代码/组件,那么请分享。

1 个答案:

答案 0 :(得分:0)

我从前一段时间写的应用程序II中拼凑了这段代码。它使用.NET OLEDB从Excel文件导入数据,在这种情况下导入MS-Access数据库,但SQL-Server的概念是相同的。您需要使其适应表格,Excel文件和您正在使用的数据:

需要替换:

 YourConnectionString
 YourSLQTable
 Your  SQL Columns: col1, col2, coln
 Your Column Valuess: @cdata1, cdata2, cdatan
 UniqueValue: the columns(s) that make your SQL table unique

 Note that parameter values must be supplied in the order they are used in the SQL statement, not in the named list, ex. New String() {"UV", "cdata1", "cdata2"}, _


Public Shared connString As String = _
    Configuration.ConfigurationManager.ConnectionStrings("xls").ConnectionString
Public Shared connStringX As String = _
   Configuration.ConfigurationManager.ConnectionStrings("xlsx").ConnectionString

Public Shared Sub ImportExcelFile(ByRef Filepath As String)
    Dim ConnectionString = IIf(Filepath.EndsWith("xlsx"), connStringX, connString)
    Try
        Using axsCon = New OleDbConnection("YourConectionString")
            axsCon.Open()
            Using m_connexcel As OleDbConnection = _
                New OleDbConnection(String.Format(ConnectionString, Filepath))
                m_connexcel.Open()
                Dim Sheetname As String = GetFirstExcelWorksheetName(m_connexcel, Filepath)
                Using cmd As OleDbCommand = New OleDbCommand("SELECT  * FROM [" & Sheetname & "]", m_connexcel)
                    Using oRDR As OleDbDataReader = cmd.ExecuteReader
                        While (oRDR.Read)
                            If Common.GetScalar(axsCon, "SELECT Count(*) FROM YourSQLTable WHERE [UniqueValue]=@UV ", _
                                New String() {"UV"}, New Object() {oRDR.GetValue(0)}) = 0 Then
                                Common.ExecuteSQL(axsCon, _
                                    "INSERT INTO YouSQLTable(col1, col2, coln) " & _
                                    "VALUES(@C1,@C2,@CN)", New String() {"Cdata1", "Cdata2", "CdataN"}, _
                                        New Object() {oRDR.GetValue(0), oRDR.GetValue(1), oRDR.GetValue(2)})
                            Else
                                Common.ExecuteSQL(axsCon, _
                                    "UPDATE YourSQLTable SET col1=@cdata1,col2=@cdata2,UNiqueValue=@UV " & _
                                    "WHERE UniqueValue=@UV", New String() {"UV", "cdata1", "cdata2"}, _
                                        New Object() {oRDR.GetValue(1), oRDR.GetValue(2), oRDR.GetValue(0)})
                            End If
                        End While
                    End Using
                End Using
                m_connexcel.Close()
            End Using
        End Using
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try
End Sub

'找到第一个工作表需要此功能,可能不适用于您的应用程序。

Private Shared Function GetFirstExcelWorksheetName(ByVal m_connexcel As OleDbConnection, ByVal Filepath As String) As String
    Try
        Dim ExcelSheets As DataTable = m_connexcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
        Return ExcelSheets.Rows(0).Item("TABLE_NAME")
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try
End Function