ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 15341|回复: 4

[分享] 如何用Excel连接Sql Server数据库

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-12-30 22:43 | 显示全部楼层 |阅读模式
本文分享三种Excel连接Sql Server数据库的方法。第一种是用Excel自带的获取外部数据功能,第二种是用Excel VBA,第三种是用专业的第三方插件

1、用Excel自带的获取外部数据功能连接Sql Server数据库

点击“数据”->“自其他来源”->“来自Sql Server”->填写登录信息->选择数据库和表->下一步->完成。此时会跳出以下对话框:
wby.png
此处可以读取表中的数据,或是用Excel透视图或透视表来分析这个表

2、用Excel VBA连接Sql Server数据库

点击“工具”->“引用”添加以下引用:
Microsoft ActiveX Data Objects 2.8 Libary
Microsoft ActiveX Data Objects Records 2.8 Libary
Microsoft ADO Ext.2.8 For Dll and Security
Microsoft Forms 2.0 Object Libary

在工作簿里建一个表,我们给它取名为sys并在表中定义好数据库连接信息如下:
cnn.png
这样我们就可以自己配置数据库连接信息了

新建一个模块专门用来写数据库的常用操作比如我们给这个模块取名为Mdl_public,首先在这个模块中定义以下几个公共变量
Public cat As New ADOX.Catalog
Public Conn As New ADODB.Connection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
Public rs As New ADODB.Recordset '定义记录集对象,保存数据表
Public Strsql As String


'打开数据库连接
Public Sub OpenSql()
If Conn.State = 1 Then Conn.Close
If Conn.State = 0 Then
    With ThisWorkbook.Sheets("sys")
        Conn.Open "Provider=sqloledb;" & _
            " Server=" & .Cells(1, 2).Value & _
            ";Database=" & .Cells(2, 2).Value & _
            ";Uid=" & .Cells(3, 2).Value & _
            ";Pwd=" & .Cells(4, 2).Value & ";" '定义数据库链接字符串
    End With
End If
End Sub


'关闭数据库连接
Public Sub CloseConn()
rs.Close
Conn.Close
End Sub


通过以上方法就实现了Sql Server数据库连接的打开和关闭

接下来,我们要使用连接查询数据库中的表,如下:

Sub ViewTop1000Rows(TBName As String)
Strsql = "SELECT TOP 1000 * FROM " & TBName
OpenSql   '打开连接
rs.Open Strsql, Conn   '使用连接
Cells.Clear
Dim i As Integer
For i = 0 To rs.Fields.Count - 1   '处理查询结果
    Cells(1, i + 1).Value = rs.Fields(i).Name
Next i
Cells(2, 1).CopyFromRecordset rs
CloseConn   '关闭连接
End Sub

Sub Test()
Call ViewTop1000Rows("MSreplication_options")
End Sub


这样设计看上去有些繁琐,但是当做比较大型的项目时这样处理好处很多。第一方便配置数据库,第二常用的数据库操作功能封装起来方便调用。比如查询数据库要经历三个步骤:1,打开连接(OpenSql) 2,执行查询语句(rs.Open Strsql, Conn )3,关闭连接(CloseConn)

3、用SqlCel连接Sql Server数据库
SqlCel连接数据库非常方便。SqlCel不仅可以连接Sql Server数据库,同时也可以连接Mysql、Oracle和文件夹。点击数据库连接下的SqlServer会出现以下对话框:
sqlcnn.png
设置好连接信息后点击确定即可。数据库连接上之后点击“查看所有表”即可将数据库中的所有表显示在当前工作簿中,之后点击其中任何一个表都可以对其进行各种操作,如下:
panel.png
关于SqlCel的更多信息可登录以下网站查阅:
http://sqlcel.com
SqlCel软件的下载可见以下网址:
http://sqlcel.com/download/

TA的精华主题

TA的得分主题

发表于 2018-12-31 01:27 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. 名称:
  2. 从: file:///D:/Program Files/SqlCel/SqlCelAddIn.vsto

  3. ************** 异常文本 **************
  4. System.Deployment.Application.InvalidDeploymentException: 从 file:///D:/Program%20Files/SqlCel/SqlCelAddIn.vsto 读取清单时出现异常: 清单可能无效,或者文件无法打开。 ---> System.Deployment.Application.InvalidDeploymentException: 清单 XML 签名无效。 ---> System.Security.Cryptography.CryptographicException: 未能为所提供的签名算法创建 SignatureDescription。
  5.    在 System.Security.Cryptography.Xml.SignedXml.CheckSignedInfo(AsymmetricAlgorithm key)
  6.    在 System.Security.Cryptography.Xml.SignedXml.CheckSignature(AsymmetricAlgorithm key)
  7.    在 System.Security.Cryptography.Xml.SignedXml.CheckSignatureReturningKey(AsymmetricAlgorithm& signingKey)
  8.    在 System.Deployment.Internal.CodeSigning.SignedCmiManifest.Verify(CmiManifestVerifyFlags verifyFlags)
  9.    在 System.Deployment.Application.Manifest.AssemblyManifest.ValidateSignature(Stream s)
  10.    --- 内部异常堆栈跟踪的结尾 ---
  11.    在 System.Deployment.Application.Manifest.AssemblyManifest.ValidateSignature(Stream s)
  12.    在 System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
  13.    --- 内部异常堆栈跟踪的结尾 ---
  14.    在 Microsoft.VisualStudio.Tools.Applications.Deployment.ClickOnceAddInDeploymentManager.GetManifests(TimeSpan timeout)
  15.    在 Microsoft.VisualStudio.Tools.Applications.Deployment.ClickOnceAddInDeploymentManager.InstallAddIn()
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-12-31 16:22 来自手机 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
gdw831001 发表于 2018-12-31 01:27

请参照
http://sqlcel.com/install
进行安装。
初步看你应该是第二个问题。
可尝试在该页面下载并安装office runtime。
或者加客服qq:438340332帮你解决

TA的精华主题

TA的得分主题

发表于 2021-10-1 23:18 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2023-1-20 09:39 | 显示全部楼层
未能在程序集 SqlCelAddIn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 中创建启动对象 SqlCelAddIn.ThisAddIn 的实例。


************** 异常文本 **************
Microsoft.VisualStudio.Tools.Applications.Runtime.CannotCreateStartupObjectException: 未能在程序集 SqlCelAddIn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 中创建启动对象 SqlCelAddIn.ThisAddIn 的实例。 ---> System.TypeInitializationException: “SqlCelAddIn.ThisAddIn”的类型初始值设定项引发异常。 ---> System.NullReferenceException: 未将对象引用设置到对象的实例。
   在 SqlCelAddIn.ThisAddIn.KiAoyU9FNvHo2ZbRgX7v(Int32 token)
   在 SqlCelAddIn.ThisAddIn..cctor()
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   在 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   在 Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.CreateEntryPoint(String entryPointTypeName)
   --- 内部异常堆栈跟踪的结尾 ---
   在 Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.CreateEntryPoint(String entryPointTypeName)
   在 Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.LoadEntryPoints(IntPtr serviceProvider)


************** 已加载的程序集 **************
mscorlib
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Office.Runtime
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Office.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Office.Runtime.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.Hosting
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Hosting/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Hosting.dll
----------------------------------------
System
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.Runtime
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Runtime.dll
----------------------------------------
System.Deployment
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Deployment/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Deployment.dll
----------------------------------------
System.Core
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Security
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Security/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Security.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.ServerDocument
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.ServerDocument/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll
----------------------------------------
System.Windows.Forms
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Xml.Linq
    程序集版本: 4.0.0.0
    Win32 版本: 4.8.4161.0 built by: NET48REL1
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll
----------------------------------------
Microsoft.Office.Tools
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.dll
----------------------------------------
Microsoft.Office.Tools.Excel.Implementation
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Excel.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Excel.Implementation.dll
----------------------------------------
Microsoft.Office.Tools.Common.Implementation
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.Implementation.dll
----------------------------------------
Microsoft.Office.Tools.Common
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.dll
----------------------------------------
SqlCelAddIn
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0
    基本代码: file:///C:/SqlCel/SqlCelAddIn.DLL
----------------------------------------
Microsoft.Office.Tools.Common.v4.0.Utilities
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.30319.1
    基本代码: file:///C:/SqlCel/Microsoft.Office.Tools.Common.v4.0.Utilities.DLL
----------------------------------------
Microsoft.Office.Tools.Excel
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Excel/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Excel.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.Runtime.resources
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Runtime.resources/v4.0_10.0.0.0_zh-Hans_b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Runtime.resources.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.ServerDocument.resources
    程序集版本: 10.0.0.0
    Win32 版本: 10.0.60828.0
    基本代码: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.ServerDocument.resources/v4.0_10.0.0.0_zh-Hans_b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.ServerDocument.resources.dll
----------------------------------------

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-25 02:55 , Processed in 0.035020 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表