【问题标题】:SQL Server stored procedure export to Excel workbook with multiple sheetsSQL Server 存储过程导出到具有多个工作表的 Excel 工作簿
【发布时间】:2017-04-06 16:44:00
【问题描述】:

如何使用少量 sql 语句从存储过程导出到 Excel 工作簿到多个工作表?

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

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

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

如何将三个 sql 语句中的数据导出到一个 Excel 工作簿中的三个工作表

【问题讨论】:

    标签: sql-server excel stored-procedures


    【解决方案1】:
    1. 创建一个空的 Excel 文件,其中包含您需要的工作表(我的示例 sales.xls 包含工作表“sheet1”、“sheet2”)

    2. 将空文件复制到所需位置/名称

    3. 使用您的 select 语句获取 sheet1 所需的信息;将数据插入到excel文件中:

      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. 使用您的 select 语句获取 sheet2 所需的信息;将数据插入到excel文件中:

      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线程:
    SQL Server export to Excel with OPENROWSET
    error on sql script with 'openrowset'

    【讨论】:

    • 不适用于 64bit Windows 7。它不灵活,因为它依赖于 Microsoft.Jet.OLEDB.4.0。
    • 检查您安装的 ms office 版本..并根据 office 连接可再发行版本使用它们的连接字符串(oledb 版本)
    • 检查这些link 和excel 相关信息.. 这不是告诉你连接字符串.. 只是告诉你在你的openrowset 中使用office 兼容规范......希望这对你有所帮助你的平台版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多