如何将sql表数据导出到excel工作表中?

时间:2016-08-10 07:20:05

标签: sql excel sql-server-2012

我有这个表,我想将其数据导出到我的excel文件中。 到目前为止,我在这里尝试过每一个主题,但没有任何效果。 我试过这两个:

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

当我用喷气机运行时,我会收到此错误:

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

当我运行ACE.OLEDB时,我得到了这个:

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

我也给我的sql帐户完全控制权限。 另外我也运行这两个:

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

如果有人能指出我正确的方向,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用本主题How do you transfer or export SQL Server 2005 data to Excel

中编写的方式

在Excel中使用外部数据菜单连接到数据库