将select语句的结果放入Excel文件中的特定工作表中

时间:2014-07-02 16:21:45

标签: sql sql-server tsql insert

如何将select语句的结果放入Excel文件中的特定工作表?

Insert into (Sheet1)ExcelFileLocation
Select col1, col2 from table1
where col1 = col2

1 个答案:

答案 0 :(得分:0)

您可以使用“链接服务器”,也可以使用 OPENROWSET (即席远程连接)建立与工作表的连接

如果您选择 OPENROWSET ,则需要在MSSQL2005及更高版本上激活它:

sp_configure 'show advanced options',1
reconfigure
sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

最后这里是在特定工作表中插入excel的语法:

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\ExcelFileLocation\excel_file.xls;','SELECT * FROM [Sheet1$]')
select col1, col2 from table1
 where col1 = col2

有关详细信息,请参阅this link here

相关问题