كيفية إدخال البيانات من ورقة Excel في جدول قاعدة البيانات؟

StackOverflow https://stackoverflow.com/questions/1570387

سؤال

أحتاج إلى إدراج البيانات من ورقة Excel إلى جدول Teradata. وأحتاج إلى القيام بذلك باستخدام ماكرو.

لدي بيانات في ورقة Excel مثل

COL1  COL2 COL3 COL4
1      2    3     4
2      5    8     10
.
.
قريباً

وأحتاج إلى الاحتفاظ بزر في ورقة Excel وتعيين ماكرو إلى هذا الزر بحيث عند النقر فوق الزر ، يجب إدخال الصفوف في ورقة Excel في جدول قاعدة البيانات.

المطلب هو أنني سأرسل ورقة Excel الفارغة إلى الشخص ، وسوف يملأ الورقة بالبيانات وينقر على الزر الموجود في Excel ويجب إدراج البيانات في جدول قاعدة البيانات. أفضل القيام بذلك باستخدام ماكرو ..

شكرا لكم جميعا.

هل كانت مفيدة؟

المحلول

القي نظرة على هذه رابط لاستخدام رمز VB/VBA (لماركو) لنقل البيانات من Excel إلى SQL Server.

نصائح أخرى

لقد قمت بإنشاء وظيفة من شأنها تحويل جدول Excel إلى أوامر إدراج متعددة.

انسخ هذا إلى الوحدة النمطية ثم في الصيغة ، وتم إعدادها كمعلمة أولى قيم الخلايا التي تحتاج إلى إدراجها ، يجب أن يكون النطاق الثاني هو أسماء الأعمدة (اضغط على F4 لإعداد هذا على أنه ثابت) ، والثالث (اختياري) اسم الجدول. إذا لم يتم تحديد اسم الجدول ، فسيتم استخدام اسم الورقة كإعداد افتراضي.

في حالتك ، هذه هي الطريقة التي يجب أن تبدو بها جدول البيانات:

+---+------+------+------+------+-----------------------------------------+
|   | A    | B    | C    | D    | E                                       |
+---+------+------+------+------+-----------------------------------------+
| 1 | COL1 | COL2 | COL3 | COL4 |                                         |
+---+------+------+------+------+-----------------------------------------+
| 2 | 1    | 2    | 3    | 4    | =Insert2DB(A2:D2,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------+
| 3 | 2    | 5    | 8    | 10   | =Insert2DB(A3:D3,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------+

هذا سوف يولد لك هذين الاستفسارات:

INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (1,2,3,4)
INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (2,5,8,10)

فيما يلي الوظيفة (تعمل بشكل جيد مع Microsoft SQL (TSQL):

    Function Insert2DB(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

      Dim rangeCell As Range
      Dim InsertValues As String
      Dim CellValue As String
      Dim C As Range

        Dim AllColls As String
        Dim SingleCell As Range
        Dim TableColls As String

    InsertValues = ""

    'Start Loop
    For Each rangeCell In InputRange.Cells

    'Recognize data type
    Set C = rangeCell
        If IsEmpty(C) Then
                'DataType is NULL then NULL
                CellValue = "NULL"
            ElseIf Application.IsText(C) Then
                'DataType is VARCHAR or CHAR
                CellValue = "'" & Trim(rangeCell.Value) & "'"
            ElseIf Application.IsLogical(C) Then
                'DataType is bit eg. TRUE / FALSE
                    If rangeCell.Value = True Then
                        CellValue = "1"
                    ElseIf rangeCell.Value = False Then
                        CellValue = "0"
                    End If
            ElseIf Application.IsErr(C) Then
                'If there is an ERROR in cell, the statment will return 0
                CellValue = "NULL"
            ElseIf IsDate(C) Then
                'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
                CellValue = "'" & VBA.Format(rangeCell.Value, "yyyymmdd hh:mm:ss") & "'"
            ElseIf InStr(1, C.Text, ":") <> 0 Then
                'DataType is TIME
                CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
            ElseIf IsNumeric(C) Then
                'DataType is number
                CellValue = rangeCell.Value
        End If

    If (Len(InsertValues) > 0) Then
        InsertValues = InsertValues + "," + CellValue
    Else
        InsertValues = CellValue
    End If

    Next rangeCell
    'END Loop

    If IsMissing(ColumnsNames) Then
        TableColls = ""
        Else

        For Each SingleCell In ColumnsNames.Cells
            If Len(AllColls) > 0 Then
                     AllColls = AllColls + "," + "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
            Else
                    AllColls = "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
            End If
        Next SingleCell
        TableColls = " (" & AllColls & ")"
    End If


    'If TableName is not set, then take the name of a sheet
    If IsMissing(TableName) = True Then
        TableName = ActiveSheet.Name
    Else
    TableName = TableName
    End If

    'Set the return value
        Insert2DB = "INSERT INTO " & TableName & TableColls & " VALUES (" & InsertValues & ")"

    End Function

إذا كان لديك الكثير من البيانات لإدراجها ، فقد لا تحتاج إلى استخدام إدراج في كل أمر ، ثم فقط استخدم وظيفة insert2db في الصف الأول (وكل 500) والباقي استخدم فقط insert2dbvalues:

+---+------+------+------+------+-----------------------------------------------+
|   | A    | B    | C    | D    | E                                             |
+---+------+------+------+------+-----------------------------------------------+
| 1 | COL1 | COL2 | COL3 | COL4 |                                               |
+---+------+------+------+------+-----------------------------------------------+
| 2 | 1    | 2    | 3    | 4    | =Insert2DB(B3:E3,$B$2:$E$2,"TableName")       |
+---+------+------+------+------+-----------------------------------------------+
| 3 | 2    | 5    | 8    | 10   | =Insert2DBValues(A3:D3,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------------+

هذا سوف يعطيك الأوامر التالية:

INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (1,2,3,4)
,(2,5,8,10)


Function Insert2DBValues(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

  Dim rangeCell As Range
  Dim InsertValues As String
  Dim CellValue As String
  Dim C As Range

    Dim AllColls As String
    Dim SingleCell As Range
    Dim TableColls As String

InsertValues = ""

'Start Loop
For Each rangeCell In InputRange.Cells

'Recognize data type
Set C = rangeCell
    If IsEmpty(C) Then
            'DataType is NULL then NULL
            CellValue = "NULL"
        ElseIf Application.IsText(C) Then
            'DataType is VARCHAR or CHAR
            CellValue = "'" & Trim(rangeCell.Value) & "'"
        ElseIf Application.IsLogical(C) Then
            'DataType is bit eg. TRUE / FALSE
                If rangeCell.Value = True Then
                    CellValue = "1"
                ElseIf rangeCell.Value = False Then
                    CellValue = "0"
                End If
        ElseIf Application.IsErr(C) Then
            'If there is an ERROR in cell, the statment will return 0
            CellValue = "NULL"
        ElseIf IsDate(C) Then
            'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
            CellValue = "'" & VBA.Format(rangeCell.Value, "yyyy-mm-dd hh:mm:ss") & "'"
        ElseIf InStr(1, C.Text, ":") <> 0 Then
            'DataType is TIME
            CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
        ElseIf IsNumeric(C) Then
            'DataType is number
            CellValue = rangeCell.Value
    End If

If (Len(InsertValues) > 0) Then
    InsertValues = InsertValues + "," + CellValue
Else
    InsertValues = CellValue
End If

Next rangeCell
'END Loop

If IsMissing(ColumnsNames) Then
    TableColls = ""
    Else

    For Each SingleCell In ColumnsNames.Cells
        If Len(AllColls) > 0 Then
                 AllColls = AllColls + "," + "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
        Else
                AllColls = "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
        End If
    Next SingleCell
    TableColls = " (" & AllColls & ")"
End If


'If TableName is not set, then take the name of a sheet
If IsMissing(TableName) = True Then
    TableName = ActiveSheet.Name
Else
TableName = TableName
End If

'Set the return value
    Insert2DBValues = ",(" & InsertValues & ")"

End Function

وأخيرًا ، إذا كنت تستخدم MySQL ، فهناك هروب مختلف من الأوتار ، لذلك في مثل هذه الحالة ، استخدم insert2dbmysql:

    Function Insert2DBMySQL(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

      Dim rangeCell As Range
      Dim InsertValues As String
      Dim CellValue As String
      Dim C As Range

        Dim AllColls As String
        Dim SingleCell As Range
        Dim TableColls As String

    InsertValues = ""

    'Start Loop
    For Each rangeCell In InputRange.Cells

    'Recognize data type
    Set C = rangeCell
        If IsEmpty(C) Then
                'DataType is NULL then NULL
                CellValue = "NULL"
            ElseIf Application.IsText(C) Then
                'DataType is VARCHAR or CHAR
                CellValue = "'" & Trim(rangeCell.Value) & "'"
            ElseIf Application.IsLogical(C) Then
                'DataType is bit eg. TRUE / FALSE
                    If rangeCell.Value = True Then
                        CellValue = "1"
                    ElseIf rangeCell.Value = False Then
                        CellValue = "0"
                    End If
            ElseIf Application.IsErr(C) Then
                'If there is an ERROR in cell, the statment will return 0
                CellValue = "NULL"
            ElseIf IsDate(C) Then
                'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
                CellValue = "'" & VBA.Format(rangeCell.Value, "yyyy-mm-dd hh:mm:ss") & "'"
            ElseIf InStr(1, C.Text, ":") <> 0 Then
                'DataType is TIME
                CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
            ElseIf IsNumeric(C) Then
                'DataType is number
                CellValue = rangeCell.Value
        End If

    If (Len(InsertValues) > 0) Then
        InsertValues = InsertValues + "," + CellValue
    Else
        InsertValues = CellValue
    End If

    Next rangeCell
    'END Loop

    If IsMissing(ColumnsNames) Then
        TableColls = ""
        Else

        For Each SingleCell In ColumnsNames.Cells
            If Len(AllColls) > 0 Then
                     AllColls = AllColls + "," + "" + Trim(Replace(SingleCell.Value, Chr(160), "")) + ""
            Else
                    AllColls = "" + Trim(Replace(SingleCell.Value, Chr(160), "")) + ""
            End If
        Next SingleCell
        TableColls = " (" & AllColls & ")"
    End If


    'If TableName is not set, then take the name of a sheet
    If IsMissing(TableName) = True Then
        TableName = ActiveSheet.Name
    Else
    TableName = TableName
    End If

    'Set the return value
        Insert2DBMySQL = "INSERT INTO " & TableName & TableColls & " VALUES (" & InsertValues & ");"

    End Function
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top