简体   繁体   English

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

[英]Export SQL query to excel with multiple worksheets and custom headers

What I need to do is, a client wants to have a report in an excel doc with multiple worksheets with custom headers. 我需要做的是,一个客户希望在excel文档中有一个包含多个带有自定义标题的工作表的报表。 I have tried SSRS 2008 Report Builder 2.0 but naming worksheets is not available with SSRS 2008 Report Builder 2.0. 我已经尝试过SSRS 2008 Report Builder 2.0,但是SSRS 2008 Report Builder 2.0无法使用命名工作表。 I have tried bcp in SQL Server Management Studio, but am not able to export it into multiple worksheets. 我曾在SQL Server Management Studio中尝试过bcp,但无法将其导出到多个工作表中。 I have put the queries into temp tables, is there a way to export those queries into the same excel doc but different worksheets with a different header for each worksheet. 我已将查询放入临时表中,有没有一种方法可以将这些查询导出到相同的excel文档中,但不同的工作表中的每个工作表都有不同的标题。

Like this 像这样 在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

Notice how each worksheet has a different name and a different header. 注意每个工作表如何具有不同的名称和不同的标题。

Is this possible to do with SQL or is there a workaround for SSRS 2008 Report Builder 2.0? 这可能与SQL有关吗?SSRS 2008 Report Builder 2.0是否有解决方法?

You could use SQL Server Integration Services 2008R2 (SSIS) to do this. 您可以使用SQL Server Integration Services 2008R2(SSIS)来执行此操作。 SSIS has an Excel Data Flow Destination that accepts a worksheet name as a parameter. SSIS具有一个Excel数据流目标,该目标接受工作表名称作为参数。 You could construct your SSIS package to populate the various worksheets of a spreadsheet this way. 您可以构造SSIS包以这种方式填充电子表格的各个工作表。

I know, I know... you too you faced the error: 我知道,我知道...您也遇到了错误:

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.

You can do it in SSMS through T-SQL, follow this example : 您可以通过T-SQL在SSMS中完成此操作 ,请遵循以下示例

First you need to allow SSMS to bypass the error: 首先,您需要允许SSMS绕过错误:

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

Then you can save the result in a precise Excel Tab this way: 然后,您可以通过以下方式将结果保存在精确的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

The file .XLSX must already be there with the tab having the precise name [Sheet1$] 文件.XLSX必须已经存在,并且选项卡具有准确的名称[Sheet1$]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM