SQL Server存储过程导出到具有多个工作表的Excel工作簿

6

如何使用少量的SQL语句从存储过程中将数据导出到Excel工作簿的多个工作表中?

我目前正在使用以下语句:

EXEC proc_generate__excel 'db', 'temp',@filename, @SeqNo, @ext, @sqlorder

如果有三个SQL语句,它将创建三个Excel工作簿。

我如何将来自三个SQL语句的数据导出到一个Excel工作簿中的三个工作表中?

1个回答

7
  1. Create an empty Excel file with the sheets in it you need (my example sales.xls with sheets "sheet1","sheet2")

  2. Copy empty file to desired location/name

  3. Using your select statement to get the desired information for sheet1; insert the data into the excel file:

    insert into OPENROWSET(
       'Microsoft.Jet.OLEDB.4.0', 
       'Excel 8.0;Database=d:\export\sales.xls;;HDR=YES', 
       'SELECT * FROM [Sheet1$]')
    select * from sales_part1
    
  4. Using your select statement to get the desired information for sheet2; insert the data into the excel file:

    insert into OPENROWSET(
        'Microsoft.Jet.OLEDB.4.0', 
        'Excel 8.0;Database=d:\export\sales.xls;;HDR=YES', 
        'SELECT * FROM [Sheet2$]')
    select * from sales_part2
    
请参考以下链接:
http://www.sqlservercentral.com/Forums/Topic487837-19-1.aspx
http://www.sqlservercentral.com/Forums/Topic660148-338-1.aspx
http://www.databasejournal.com/features/mssql/article.php/10894_3331881_1

一些相关的SO帖子:
使用OPENROWSET将SQL Server导出到Excel
'openrowset' SQL脚本错误


不适用于64位的Windows 7。它不够灵活,因为它依赖于Microsoft.Jet.OLEDB.4.0。 - kelvinfix
请检查您安装的 MS Office 版本,并根据 Office 连接可再发行版使用其连接字符串(OLEDB 版本)。 - Niranjan Singh
请查看以下链接(http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891)以及与Excel相关的信息。这并不是为了告诉您连接字符串的内容,而是要告诉您在openrowset中使用与Office兼容的规范...希望这能够根据您的平台版本帮助到您。 - Niranjan Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接