感谢您使用微软产品。您可以使用QueryTables的add方法为excel添加来自外部数据库的数据。如下例:
Private Sub Workbook_Open()
    Worksheets(2).Activate
    Dim qt As QueryTable
    sqlstring = "select * from table1"
    connstring = "ODBC;DSN=testdb;Database=testdb"
    With ActiveSheet.QueryTables.Add(Connection:=connstring, _
            Destination:=Range("D1"), Sql:=sqlstring)
        .Refresh
    End With
End Sub详情请参阅:
QueryTables Collection Object
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl10/html/xlobjQueryTables.asp在以下链接点击Example,选择As it applies to the QueryTables object,能够获得相关样例程序。
Add Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl10/html/xlmthadd.asp-  微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。