[英]Exporting SQL table to Excel - column names are not exporting, only the data
通過Excel VBA連接到SQL Management Studio之后,我得到了以下代碼(PART OF IT),該代碼從SQL中的表中提取數據。
With BalanceSheet
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "select * from Analytics.dbo.BalSheetKeyLineItemsFinal"
Sheet1.Range("A13").CopyFromRecordset BalanceSheet
' Tidy up
.Close
End With
但是,表中的列名稱不會顯示。 當我將數據導出到Excel時,有什么方法可以獲取要包含的列名?
謝謝!
記錄集包含您要查詢的數據,因此這就是您期望發生的情況。 由於您使用的是.Open
和.ActiveConnection
,因此我假設您使用的是ADO:在這種情況下,您可以從記錄集對象的Fields
集合中獲取字段名稱:
For i = 0 to .Fields.Count - 1
Sheet1.Range("A12").Offset(, i) = .Fields(i).Name
Next
這將在Sheet1
第12行中寫入字段標題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.