代码之家  ›  专栏  ›  技术社区  ›  jjohnson

将SQL查询导出到带有多个工作表和自定义标题的excel

  •  2
  • jjohnson  · 技术社区  · 10 年前

    我需要做的是,客户希望在excel文档中有一个带有多个自定义标题的工作表的报告。我尝试过SSRS 2008 Report Builder 2.0,但命名工作表在SSRS 2008报表生成器2.0中不可用。我在SQLServerManagementStudio中尝试过bcp,但无法将其导出到多个工作表中。我已经将查询放入临时表中,是否有方法将这些查询导出到同一个excel文档中,但每个工作表都有不同的标题。

    这样地 enter image description here enter image description here enter image description here enter image description here

    请注意,每个工作表都有不同的名称和不同的标题。

    这是否可能与SQL有关,或者SSRS 2008 Report Builder 2.0是否有解决方法?

    2 回复  |  直到 10 年前
        1
  •  3
  •   Greg the Incredulous    10 年前

    您可以使用SQL Server Integration Services 2008R2(SSIS)来执行此操作。SSIS的Excel数据流目标接受工作表名称作为参数。您可以构造SSIS包来以这种方式填充电子表格的各种工作表。

        2
  •  1
  •   Francesco Mantovani    6 年前

    我知道,我知道。。。你也面临着错误:

    Msg 15281, Level 16, State 1, Line 1
    SQL Server blocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad Hoc Distributed Queries’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘Ad Hoc Distributed Queries’ by using sp_configure. For more information about enabling ‘Ad Hoc Distributed Queries’, search for ‘Ad Hoc Distributed Queries’ in SQL Server Books Online.
    

    您可以通过T-SQL在SSMS中执行,如下所示 this example :

    首先,您需要允许SSMS绕过错误:

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

    然后,您可以通过以下方式将结果保存在精确的Excel选项卡中:

    INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;
    Database=C:\Users\Zivko\Desktop\SQL Data.xlsx;','SELECT * FROM [Sheet1$]') 
    SELECT * FROM dbo.DimScenario
    

    文件.XLSX必须已经存在,且选项卡具有精确名称 [Sheet1$]