【问题标题】:Is there a way to collect data from excel specific cells to send to SQL Server?有没有办法从 excel 特定单元格中收集数据以发送到 SQL Server?
【发布时间】:2019-05-23 21:53:06
【问题描述】:

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

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

我查看了 Excel 文档。其中说: 使用以下工具之一,一步将数据直接从 Excel 导入 SQL: SQL Server 导入和导出向导 SQL Server 集成服务 (SSIS) OPENROWSET 函数 您可以分两步导入数据,将数据从 Excel 导出为文本,然后使用以下工具之一导入文本文件: 导入平面文件向导 批量插入语句 BCP 复制向导(Azure 数据工厂) Azure 数据工厂 我也对 VBA 进行了一些研究。

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

欢迎提出任何建议。

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

我尝试使用内置于 JavaScript API 的 Microsoft Excel,但找不到将服务器端语言连接到它的方法。

【问题讨论】:

    标签: sql-server excel ssis etl


    【解决方案1】:

    您可以通过两种方式实现:

    使用SQL命令作为数据源

    在 Excel 源代码编辑器中,将访问模式更改为 SQL 命令,并在工作表名称后指定范围,例如:

    SELECT * FROM [Sheet1$A3:A4]
    

    使用脚本组件作为源

    您可以使用脚本组件作为源,并且在脚本中您可以使用 Interop.Excel.ddl 程序集来读取 Excel 单元格并生成所需的输出:

    有用的链接

    【讨论】:

      【解决方案2】:

      要将数据从 SQL Server 拉入 Excel,请运行如下示例代码。

      '  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,请运行如下示例代码。

      '  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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多