简体   繁体   中英

how to export sql table data into excel worksheet?

I have this table that I want to export its data into my excel file. so far I've tried every topic here but nothing worked. I've tried these 2:

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


insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0',
    'Excel 8.0;Database=D:\excel\testing.xls;', 
    'SELECT * FROM [newStart$]') select * from OutputResult

when I run it with jet I'll get this error:

OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" returned message "The Microsoft Jet database engine could not find the object 'newStart$'.  Make sure the object exists and that you spell its name and the path name correctly.".
 Msg 7350, Level 16, State 2, Line 1 Cannot get the column information from OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".

and when I ran the ACE.OLEDB I get this one:

OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "The Microsoft Office Access database engine could not find the object 'newStart$'.  Make sure the object exists and that you spell its name and the path name correctly.".
Msg 7350, Level 16, State 2, Line 1
Cannot get the column information from OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".

I give my sql account full control permission as well. plus I run these two as well:

USE [master]
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
GO

EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
GO

2:

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

I would appreciate it if anyone could point me into the right direction.

strSqlQry = "Place SQL Qry Here"
vFilePath = "Database Path"

'Create Objects
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

'Open Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & vFilePath & ";" & _
 "Extended Properties=""Excel 8.0;HDR=Yes;"";"

'Execute Qry
rs.Open strSqlQry, conn, adOpenStatic, adLockOptimistic

'Place recorset into excel worksheet
ActiveSheet.Range("A1").CopyFromRecordset (NewRS)

rs.Close

Set rs = Nothing
Set conn = Nothing

You can use way written in this topic How do you transfer or export SQL Server 2005 data to Excel

In excel using External data menu connect to database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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