0

Does anyone know the way to import Excel data in to SQL database by column name using VBA?

For example, I have an Excel file with 5 columns: column1, column2, column3, column 4, column 5. Position of these column are generated randomly I only want to import data of 3 columns: column1, column3, column5 in to SQL database using VBA.

4

3 回答 3

0

您可以使用 ADO 连接。

您可以打开与目标服务器的连接,循环数据,然后将其插入目标数据库。这可以让你开始: http ://www.vbexplorer.com/VBExplorer/vb_feature/june2000/Database_Beginner_ADO_DAO.asp

否则,根据您的目标数据库,它可能有一些专有的导入工具——这通常需要您将您的 excel 导出为它识别的某种格式。

对于完整的解决方案,您可以考虑使用ETL工具

于 2012-07-11T18:24:24.170 回答
0

因此,正如您从变量名中看到的那样,这是用于莲花文件的,但是,我将它用于 csv。所以你的excel文件的结构是一样的。如果您只想将其保存为 CSV 文件,并祝您生活愉快。我知道已经 6 年了,所以我想你不再需要这个了,但可能会帮助遇到这个问题的其他人。

    Dim LotusCn As Object
    Dim rsLotus As Object
    Dim strSql, CombFileName, GotoRange As String
    Dim rsLotusFiles As DAO.Recordset
    Dim rsDAO1 As DAO.Recordset
    Dim strName1 As String

    Set LotusCn = CreateObject("ADODB.Connection")
    Set rsLotus = CreateObject("ADODB.Recordset")

    strSql = "Provider=" & _ 'This is where the file is located
    CurrentProject.Connection.Provider & _
    ";Data Source=" & Directory & _
    ";Extended Properties='text;HDR=YES;FMT=Delimited'"

    LotusCn.Open strSql
    Dim fld1 As ADODB.Field

    strSql = "SELECT * FROM NUMBDATM.CSV" 'This is the file name and please open it
    rsLotus.Open strSql, LotusCn, adOpenFowardOnly, adLockReadOnly, adCmdText


    Set rsDAO1 = CurrentDb.OpenRecordset("NUMBDATM", _ 'This here is the table you 
    dbOpenTable, dbAppendOnly + dbFailOnError)          want to import into 

    Do Until rsLotus.EOF 'Here tell it what values you want from the excel Sheet
          RegNumber = rsLotus![Reg# Number]
          CompanyName = rsLotus![Company Name]
          SalesGrowth1 = rsLotus![Sales Growth % 1 ]
          FixedAssets1 = rsLotus![Fixed Assets 1 ]
          PeriodEnding1 = rsLotus![Period Ending 1 ]
          TotalSales1 = rsLotus![Total Sales 1 ]
          SalesGrowth2 = rsLotus![Sales Growth % 2 ]
          SalesGrowth3 = rsLotus![Sales Growth % 3 ]
          PreTaxProfit3 = rsLotus![Pretax Profit 3 ]
          PreTaxProfit2 = rsLotus![Pretax Profit 2 ]
          PreTaxProfit1 = rsLotus![Pretax Profit 1 ]
          PrProfitMarg = rsLotus![Pr#Profit Margin  % 1 ]
          Week1 = rsLotus![Weeks 1 ]
          Week2 = rsLotus![Weeks 2 ]
          Week3 = rsLotus![Weeks 3 ]
        rsDAO1.AddNew 'Here please add the values from above into the relevant table
            rsDAO1![Reg# Number] = RegNumber
            rsDAO1![Company Name] = CompanyName
            rsDAO1![Sales Growth % 1 ] = SalesGrowth1
            rsDAO1![Fixed Assets 1 ] = FixedAssets1
            rsDAO1![Period Ending 1 ] = PeriodEnding1
            rsDAO1![Total Sales 1] = TotalSales1
            rsDAO1![Sales Growth % 2 ] = SalesGrowth2
            rsDAO1![Sales Growth % 3 ] = SalesGrowth3
            rsDAO1![Pretax Profit 3 ] = PreTaxProfit3
            rsDAO1![Pretax Profit 2 ] = PreTaxProfit2
            rsDAO1![Pretax Profit 1 ] = PreTaxProfit1
            rsDAO1![Pr#Profit Margin  % 1 ] = PrProfitMarg
            rsDAO1![Weeks 1 ] = Week1
            rsDAO1![Weeks 2 ] = Week2
            rsDAO1![Weeks 3 ] = Week3
        rsDAO1.Update
        rsLotus.MoveNext
    Loop
    rsDAO1.Close
    Set rsDAO1 = Nothing
    rsLotus.Close
    Set rsLotus = Nothing
    LotusCn.Close
于 2018-08-31T10:45:21.227 回答
-1
Protected Sub just_Click(sender As Object, e As EventArgs) Handles just.Click


Dim cnn As SqlConnection

Dim sql As String

Dim i, j As Integer

Dim xlApp As Excel.Application

Dim xlWorkBook As Excel.Workbook

Dim xlWorkSheet As Excel.Worksheet

Dim misValue As Object = System.Reflection.Missing.Value

xlApp = New Excel.ApplicationClass

xlWorkBook = xlApp.Workbooks.Add(misValue)

xlWorkSheet = xlWorkBook.Sheets("sheet1")

cnn = New SqlConnection("***your connection string***")

cnn.Open()

sql = "SELECT * FROM exceltable"

Dim dscmd As New SqlDataAdapter(sql, cnn)

Dim ds As New DataSet

dscmd.Fill(ds)

For j = 0 To ds.Tables(0).Columns.Count - 1

xlWorkSheet.Cells(i + 1, j + 1) = _

ds.Tables(0).Columns(j).ColumnName

Next

For i = 0 To ds.Tables(0).Rows.Count - 1


For j = 0 To ds.Tables(0).Columns.Count - 1

xlWorkSheet.Cells(i + 2, j + 1) = _

ds.Tables(0).Rows(i).Item(j)


Next

Next

xlWorkSheet.SaveAs("D:\pappy.xlsx")

xlWorkBook.Close()

xlApp.Quit()

cnn.Close()

End Sub
于 2015-06-01T08:45:00.760 回答