简体   繁体   English

使用查询将SQL Server数据导出到Excel

[英]Export Sql server data to Excel by using Query

I am trying to export the SQL server data to the excel file with using the sql query. 我正在尝试使用sql查询将SQL Server数据导出到excel文件。 But i am getting error 但是我出错了

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)".

The Query which i am using 我正在使用的查询

select * from OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\share\Test1.xlsx;HDR=YES','SELECT id FROM [Sheet1$]') SELECT id FROM Table

I have also enabled 我也启用了

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE


 EXEC master.dbo.sp_MSset_oledb_prop 'Microsoft.Jet.OLEDB.4.0', 'AllowInProcess', 0 
GO 
EXEC master.dbo.sp_MSset_oledb_prop 'Microsoft.Jet.OLEDB.4.0', 'DynamicParameters', 0
GO 

I have also shared my folder with full permissions. 我还以完全权限共享了我的文件夹。 I have Given the Excel file same headers with the table name But Same Error 我已经为Excel文件赋予了与表名相同的标题,但存在相同的错误

The connection you are using is wrong: it is the JET driver which 您使用的连接是错误的:是JET驱动程序

  • is for reading/writing flat files of various types 用于读取/写入各种类型的平面文件
  • is pointing to a file called c:\\share\\test1.xlsx 指向一个名为c:\\ share \\ test1.xlsx的文件
  • is obsolete, and has been replaced by the ACE drivers. 已过时,并且已被ACE驱动程序取代。

You need to re-write your code. 您需要重新编写代码。 This article gives a good idea on how to do it. 本文为如何做到这一点提供了一个好主意。 One bit you must NOT miss out, and should do first, is the para titled Referencing the ADO object library . 您一定不能错过,并且应该首先做的是标题为ADO对象库的引用

You may well have a whole load of questions about things the code is doing (for instance, what is INTEGRATED SECURITY=SSPI? and a load more), so many that I'm not going to answer them because I would have to write a chapter of a book to and I don't know whether you would ask any of them. 您可能会对代码所做的事情有很多疑问(例如,什么是INTEGRATED SECURITY = SSPI?还有更多负载),所以我不想回答它们,因为我不得不写一个一本书的第一章,我不知道您是否会问其中的任何一个。 I suggest that you read the article, try the code, and then post individual questions in invividual posts here. 我建议您阅读本文,尝试使用代码,然后在此处将一些独立的问题发布到非公开帖子中。

Once clue; 一旦线索; this bit of code: 这段代码:

'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=pubs;"

...is specifying the database server and the name of the database. ...正在指定数据库服务器和数据库名称。 Replace the (local) with the name of the database server if it's not your local machine. 如果不是本地计算机,则用数据库服务器的名称替换(本地)。 The pubs name is the name of the database. pubs名称是数据库的名称。 Replace with your database. 用您的数据库替换。

Also, this bit: 另外,这一点:

' Extract the required records.
.Open "SELECT * FROM Authors"

Here, "SELECT * from Authors" is the SQL which defines the data the query will return. 此处,“ SELECT * from Authors”是用于定义查询将返回的数据的SQL。 You need to replace this with the SQL which will get the data you want from your database. 您需要用SQL替换它,它将从数据库中获取所需的数据。 Again, a rather large subject. 同样,这是一个相当大的主题。

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

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