首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有没有一种方法可以从excel特定的单元格中收集数据并发送到SQL Server?

有没有一种方法可以从excel特定的单元格中收集数据并发送到SQL Server?
EN

Stack Overflow用户
提问于 2019-05-24 05:53:06
回答 1查看 1K关注 0票数 4

我有一个模板excel表,我希望用户每天填写。一张工作表告诉我哪些单元格是可读/写的(这意味着我想要发送到我的数据库)。形式不是管状的,即。有时设置数据(A3-> A4)或(A3 -> B3)。

我想知道有没有一个excel插件或任何方式,我可以读取某些单元格,然后将它们发送到我的数据库后,表格已经完成。

我已经查阅了Excel文档。这说明:导入数据在单个步骤中,直接从Excel到SQL,通过使用以下工具之一: SQL Server导入和导出向导SQL Server Integration Services (SSIS) OPENROWSET功能您可以分两步导入数据,通过将您的数据作为文本从Excel导出,然后使用以下工具之一导入文本文件:导入平面文件向导批量插入语句BCP复制向导(Azure Data Factory) Azure Data Factory我也研究了一下VBA。

其中之一会是最好的路径吗?

如有任何建议,欢迎光临。

我曾尝试将SQL Server导入和导出向导与SQL Server Integration Services (SSIS)一起使用,但我的模板不遵循规范化的表布局,这是我无法控制的。

我试过使用内置在JavaScript应用程序接口中的Microsoft Excels,但我找不到一种方法来连接服务器端语言。

EN

回答 1

Stack Overflow用户

发布于 2019-06-12 21:21:12

要将数据从SQL Server提取到Excel中,请运行示例代码,如下所示。

代码语言:javascript
复制
'  Tools > References > Microsoft ActiveX Data Objects 2.8 Library
Sub TestMacro()

' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection

' Provide the connection string.
Dim strConn As String

'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"

'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=NORTHWIND.MDF;"

'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"

'Now open the connection.
cnPubs.Open strConn

' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset

With rsPubs
    ' Assign the Connection object.
    .ActiveConnection = cnPubs
    ' Extract the required records.
    .Open "SELECT * FROM YourTable"
    ' Copy the records into cell A1 on Sheet1.
    Sheet1.Range("A1").CopyFromRecordset rsPubs

    ' Tidy up
    .Close
End With

cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing

End Sub

要将数据从Excel推送到SQL Server,请运行示例代码,如下所示。

代码语言:javascript
复制
'  Tools > References > Microsoft ActiveX Data Objects 2.8 Library
Sub InsertInto()

'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String

'Create a new Connection object
Set cnn = New adodb.Connection

'Set the connection string
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=Your_Server_Name_Here"


'Create a new Command object
Set cmd = New adodb.Command

'Open the Connection to the database
cnn.Open

'Associate the command with the connection
cmd.ActiveConnection = cnn

'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText

'Create the SQL...using a Where clause here...can be anything you want...
strSQL = "UPDATE TBL SET JOIN_DT = '2019-06-12' WHERE EMPID = 1"

'Pass the SQL to the Command object
cmd.CommandText = strSQL


'Execute the bit of SQL to update the database
cmd.Execute

'Close the connection again
cnn.Close

'Remove the objects
Set cmd = Nothing
Set cnn = Nothing

End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56283397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档