简体   繁体   English

从SQL Server查询将数据导入Excel

[英]Import data into Excel from SQL Server query

I find some ways to import data from a SQL Server query to Excel like below but none of them seem to work 我发现了一些将数据从SQL Server查询导入到Excel的方法,如下所示,但它们似乎都不起作用

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
      'Excel 8.0;Database=D:\testing.xls;', 
      'SELECT * FROM [SheetName$]') select * from tblName

Error: 错误:

OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode. OLE DB提供程序'Microsoft.Jet.OLEDB.4.0'不能用于分布式查询,因为该提供程序配置为以单线程单元模式运行。

As indicated by the error message, SQL server will not be able to transfer the data to Excel using this method. 如错误消息所示,SQL Server将无法使用此方法将数据传输到Excel。 One possible solution that does not require programming: Open Excel, click the Data tab, From Other Sources, SQL Server. 一种不需要编程的解决方案:打开Excel,单击“数据”选项卡,从“其他来源”,SQL Server。 A window will open prompting for Server name, provide the server name and instance name separated by a backslash. 将打开一个窗口,提示您输入服务器名称,并提供服务器名称和实例名称,并用反斜杠分隔。 Example: localhost\\sqlexpress (if you are running sql server express edition on your PC, otherwise whatever server you installed SQL Server on, along with the instance name). 示例:localhost \\ sqlexpress(如果您在PC上运行sql server express版本,否则在其上安装SQL Server的任何服务器以及实例名称)。 Authenticate by providing your user id and password as defined in SQL server or just clicking Windows authentication. 通过提供您在SQL Server中定义的用户ID和密码或仅单击Windows身份验证来进行身份验证。 You will then have the opportunity to select the table name. 然后,您将有机会选择表名。 The data will then be transferred into an Excel table that you can refresh at will by right-clicking on it. 然后,数据将被传输到一个Excel表中,您可以通过右键单击该表来随意刷新。 Please, indicate if you want an alternate solution that may require programming. 请指出您是否需要可能需要编程的替代解决方案。

If you have 32-bit Office and you don't want to uninstall it, try to install latest AccessDatabaseEngine from microsoft (64-bit). 如果您有32位Office,并且不想卸载它,请尝试从Microsoft(64位)安装最新的AccessDatabaseEngine。 To install it without uninstalling 32-bit version please go to the download directory in comand line and write 要安装它而不卸载32位版本,请转到comand行中的下载目录并编写

AccessDatabaseEngine.exe /passive

Then in SSMS you can import your Excel with code: 然后,在SSMS中,您可以使用代码导入Excel:

FROM OPENROWSET
('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\path\sheet.xlsx', [Sheet1$])

You should have a look at Pinal Dave's solution. 您应该看看Pinal Dave的解决方案。 Also, possibly you have x64 machine so it won't work due to unavailability of driver. 另外,可能您拥有x64机器,因此由于驱动程序不可用而无法使用。 Check out this answer . 看看这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM