当前位置: 代码迷 >> Sql Server >> Excel 数据导入SQl解决方法
  详细解决方案

Excel 数据导入SQl解决方法

热度:5   发布时间:2016-04-27 14:23:39.0
Excel 数据导入SQl
请教:
如何在EXCEL里写语句,
把Excel里面 的数据导入到SQl里,
不需要用 SQL 自带的导入导出功能

------解决方案--------------------
用VBA 写程序.
用程序连接 SQL 数据库服务器,然后执行将数据导入SQL的语句.


------解决方案--------------------
在EXCEL中用VBA写,代码如下:
SQL code
Sub aa()    Dim cn As New ADODB.Connection    Dim rs As New Recordset    Str1 = "select 区域,内=SUM(case when 新替换区内外='替换区内' then 1 else 0 end ),外=SUM(case when 新替换区内外='替换区外' then 1 else 0 end ) from 宏站 group by 区域"    strcn = "Provider=sqloledb;Database=test;Uid=sa;Pwd=30687724;data source=127.0.0.1,1433;"    cn.Open strcn    rs.Open Str1, cnDim iFor i = 1 To rs.Fields.CountCells(1, i) = rs.Fields(i - 1).Name  Range("a2").CopyFromRecordset rsNext iEnd Sub
------解决方案--------------------
#3有错,我写的是导出,导入到SQL应该是下面这个:

SQL code
    Dim cnn As New ADODB.Connection    strcn = "Provider=sqloledb;Database=小区系统;Uid=sa;Pwd=30687724;data source=127.0.0.1,1433;"    cnn.Open strcn    Dim y As Long    Dim arr '数据太大,现在分5次导入 '第一次    arr = Range("a1:ak90000")For y = 1 To UBound(arr, 1)        ssql = "INSERT INTO [201108(1-30)]  VALUES ('" & arr(y, 1) & " ','" & arr(y, 2) & " ','" & arr _               (y, 3) & " ','" & arr(y, 4) & " ','" & arr(y, 5) & " ','" & arr(y, 6) & " ','" & arr(y, 7) & " ','" & arr _               (y, 8) & " ','" & arr(y, 9) & " ','" & arr(y, 10) & " ','" & arr(y, 11) & " ','" & arr(y, 12) & " ','" & arr _               (y, 13) & " ','" & arr(y, 14) & " ','" & arr(y, 15) & " ','" & arr(y, 16) & " ','" & arr(y, 17) & " ','" & arr _               (y, 18) & " ','" & arr(y, 19) & " ','" & arr(y, 20) & " ','" & arr(y, 21) & " ','" & arr(y, 22) & " ','" & arr _               (y, 23) & " ','" & arr(y, 24) & " ','" & arr(y, 25) & " ','" & arr(y, 26) & " ','" & arr(y, 27) & " ','" & arr _               (y, 28) & " ','" & arr(y, 29) & " ','" & arr(y, 30) & " ','" & arr(y, 31) & " ','" & arr(y, 32) & " ','" & arr _               (y, 33) & " ','" & arr(y, 34) & " ','" & arr(y, 35) & " ','" & arr(y, 36) & " ','" & arr(y, 37) & " ')"        cnn.Execute ssqlNext yErase arrSet ssql = Nothingcnn.Close    Windows(" 优化统计指标(0-23).txt").Close SaveChanges:=FalseSheets("sheet1").Range("a1000").End(xlUp).Offset(1, 0) = maxhNext x
------解决方案--------------------
还有个方法就是,如果你是用的EXCEL2007,可以点“数据”——自它来源——MICRSSOFT query 进行一看就明白了,

得先建立连接数据源!
  相关解决方案