将数据从 SQL Server 2014 导出到 Excel

Export Data from SQL Server 2014 to Excel

我尝试将数据从 SQL Server 2014 导出到 Excel。

我这样试过

insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0
Xml;HDR=YES;Database=D:\testing.xlsx;', 
'SELECT OI_NAME,OI_ADDRESSS FROM [Sheet1$]') select OI_NAME,
OI_ADDRESSS from OI_TEMP

但是我得到这个错误

OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "Unspecified error".

Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".

我在网上搜索了一下,发现了这个:

The SQL Server Error Message if a user have no rights for SQL Server TEMP directory OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" returned message "Unspecified error".
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".

但是我分配权限还是不行

为什么?

我几年前试过,那样做但不可能,我建议您使用此表单将数据从 SQL -server 导出到 Excel。

字体:Microsoft Developer Network

创建一个 excel 文件,然后选择数据 -> 来自其他来源 -> 来自 SQL 服务器

Select 输入您的服务器、用户和密码,然后 select 数据库并选择您的 table。

观看此视频:

Video Tutorial - SQL server to Excel

然后,您可以在 VBA 上编写一个宏来自动更新 excel 文件

sub Auto_Open ()

ActiveWorkbook.RefreshAll

end sub

希望对您有所帮助!

要将数据从 SQL 导出到 Excel,您需要执行 2 个步骤:

  • 第 1 步:将 Excel 连接到您的 SQL 数据库‍(Microsoft SQL 服务器、MySQL、PostgreSQL...)
  • 第 2 步:将您的 SQL 数据导入 Excel

结果将是您要从 SQL 数据库查询数据到 Excel 的 table 的列表:

第 1 步:将 Excel 连接到外部数据源:您的 SQL 数据库

  1. 安装 ODBC
  2. 安装驱动程序
  3. 避免常见错误
  4. 创建 DSN

第 2 步:将您的 SQL 数据导入 Excel

  1. 单击您想要的轴心位置Table
  2. 点击插入
  3. 单击轴 Table
  4. 点击使用外部数据源,然后选择连接
  5. 单击“系统 DSN”选项卡
  6. Select 在 ODBC 管理器中创建的 DSN
  7. 填写请求的用户名和密码
  8. 避免常见错误
  9. 访问 Microsoft 查询对话框
  10. 单击箭头可查看数据库中 table 的列表
  11. Select table 你想从你的 SQL 数据库查询数据到 Excel
  12. 完成选择后单击 Return 数据

要自动更新导出,还有 2 个额外的步骤:

  1. 使用外部 SQL 数据源创建数据透视表 Table
  2. 使用 GETPIVOTDATA 函数
  3. 自动更新 Excel 中的 SQL 数据

我创建了一个step-by-step tutorial about this whole process, from connecting Excel to SQL, up to having the whole thing automatically updated. You might find the detailed explanations and screenshots有用的。