Is there a way to collect data from excel specific cells to send to SQL Server?

ε祈祈猫儿з 提交于 2021-02-09 01:06:24

问题


I have a template excel sheet that I want users to fill out daily. One sheet tells me what cells are read/write (meaning which ones I want to send to my data base). The form is not tubular ie. sometimes data is setup (A3 -> A4) or (A3-> B3).

I was wondering is there a excel add on or any way I can read certain cells then send them to my db after the form has been completed.

I have looked into Excel documentation. which says: import data in a single step, directly from Excel to SQL, by using one of the following tools: The SQL Server Import and Export Wizard SQL Server Integration Services (SSIS) The OPENROWSET function You can import data in two steps, by exporting your data from Excel as text, and then using one of the following tools to import the text file: The Import Flat File Wizard The BULK INSERT statement BCP The Copy Wizard (Azure Data Factory) Azure Data Factory I also have looked into VBA a bit.

Would one of these be the best path?

any suggestions are appreciated.

I have tried to use The SQL Server Import and Export Wizard with SQL Server Integration Services (SSIS) But my templates dont follow a normalized table layout, which I cannot control.

I have tried to use Microsoft Excels built in JavaScript API's but I couldn't find a way to connect a sever side language to it.


回答1:


You can achieve that in two approaches:

Using SQL command as data source

In the Excel Source editor, change the Access mode to SQL Command and specify the range after the sheet name, as example:

SELECT * FROM [Sheet1$A3:A4]
  • Ignoring column from Excel file while importing to SQL Server

Using Script Component as Source

You can use a Script Component as Source and within the Script you can use Interop.Excel.ddl assembly to read the Excel cells and generate desired output:

Helpful links

  • Bulk insert from excel to sql for selective fields on the basis of cell location
  • SSIS Script Component as Source



回答2:


To pull data into Excel from SQL Server, run sample code like this.

'  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

To push data from Excel into SQL Server, run sample code like this.

'  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


来源:https://stackoverflow.com/questions/56283397/is-there-a-way-to-collect-data-from-excel-specific-cells-to-send-to-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!