将 Excel 单元格值发送到 SharePoint 列表列
Send Excel Cell Value to SharePoint List column
我想要一个工具(可能是 VBA?)select 一个现有的 Excel 文件(甚至是一个文件夹中的多个 Excel 文件)并把将固定单元格的值放入 SharePoint 列表的列(例如:将 Excel 文件的 A1 的值放入 Sharepoint 列表的第 1 列)。
这可能吗?如果是这样,VBA 是一个合乎逻辑的选择吗?
您可以为此使用 ADO - 下面是向列表添加新记录的示例。
如果您进入列表设置并查看 URL,您可以获得列表名称:列表 guid 在 List=
querystring 参数之后。
https://yourCompany.sharepoint.com/sites/Site1/_layouts/15/metadatacolsettings.aspx?List={af83a2e4-a2e4-4890-1111-3431bde70e5e}
Sub SPListAdd()
Const SERVERUrl As String = "https://yourCompany.sharepoint.com/sites/Site1/"
Const ListName As String = "{af83a2e4-a2e4-4890-1111-3431bde70e5e}"
Dim Conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim Sql As String
Dim objWksheet As Worksheet
Dim f As ADODB.Field, i As Long
Set objWksheet = ThisWorkbook.Worksheets("List Items")
objWksheet.Cells.Clear
On Error GoTo ErrHand
' Open the connection and submit the update
With Conn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;" & _
"DATABASE=" & SERVERUrl & ";" & _
"LIST=" & ListName & ";"
.Open
End With
'inserting a new record....
Sql = "SELECT * FROM [" & ListName & "] where false" 'get empty recordset
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open Sql, Conn, adOpenKeyset, adLockOptimistic
rs.AddNew
rs.Fields("Title") = "This is a title"
rs.Update
rs.Close
'query all records for display
Sql = "SELECT * FROM [" & ListName & "]"
rs.Open Sql, Conn, adOpenStatic
If Not rs.EOF Then
For Each f In rs.Fields
objWksheet.Range("A1").Offset(0, i).Value = f.Name
i = i + 1
Next f
objWksheet.Range("A2").CopyFromRecordset rs
End If
rs.Close
Conn.Close
ErrHand:
Debug.Print Err.Number, Err.Description
End Sub
我想要一个工具(可能是 VBA?)select 一个现有的 Excel 文件(甚至是一个文件夹中的多个 Excel 文件)并把将固定单元格的值放入 SharePoint 列表的列(例如:将 Excel 文件的 A1 的值放入 Sharepoint 列表的第 1 列)。
这可能吗?如果是这样,VBA 是一个合乎逻辑的选择吗?
您可以为此使用 ADO - 下面是向列表添加新记录的示例。
如果您进入列表设置并查看 URL,您可以获得列表名称:列表 guid 在 List=
querystring 参数之后。
https://yourCompany.sharepoint.com/sites/Site1/_layouts/15/metadatacolsettings.aspx?List={af83a2e4-a2e4-4890-1111-3431bde70e5e}
Sub SPListAdd()
Const SERVERUrl As String = "https://yourCompany.sharepoint.com/sites/Site1/"
Const ListName As String = "{af83a2e4-a2e4-4890-1111-3431bde70e5e}"
Dim Conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim Sql As String
Dim objWksheet As Worksheet
Dim f As ADODB.Field, i As Long
Set objWksheet = ThisWorkbook.Worksheets("List Items")
objWksheet.Cells.Clear
On Error GoTo ErrHand
' Open the connection and submit the update
With Conn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;" & _
"DATABASE=" & SERVERUrl & ";" & _
"LIST=" & ListName & ";"
.Open
End With
'inserting a new record....
Sql = "SELECT * FROM [" & ListName & "] where false" 'get empty recordset
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open Sql, Conn, adOpenKeyset, adLockOptimistic
rs.AddNew
rs.Fields("Title") = "This is a title"
rs.Update
rs.Close
'query all records for display
Sql = "SELECT * FROM [" & ListName & "]"
rs.Open Sql, Conn, adOpenStatic
If Not rs.EOF Then
For Each f In rs.Fields
objWksheet.Range("A1").Offset(0, i).Value = f.Name
i = i + 1
Next f
objWksheet.Range("A2").CopyFromRecordset rs
End If
rs.Close
Conn.Close
ErrHand:
Debug.Print Err.Number, Err.Description
End Sub