简体   繁体   English

如何将动态SQL Server数据透视表导出到Excel

[英]How to export dynamic SQL Server pivot table to excel

I'm working on a webapp that displays data from a Microsoft SQL Server dynamic pivot table. 我正在使用一个Web应用程序,该应用程序显示Microsoft SQL Server动态数据透视表中的数据。

Normally I'd try and figure out a way to do the dynamic pivot in c#, but in this case the pivot has to be a SQL Server stored procedure because other apps also need access to the pivot table. 通常,我会尝试找出一种使用c#进行动态数据透视的方法,但是在这种情况下,数据透视必须是SQL Server存储过程,因为其他应用程序也需要访问数据透视表。

Here's the SQL: 这是SQL:

DECLARE @DynamicPivot AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

SELECT 
    @ColumnName = ISNULL(@ColumnName + ',', '') 
                  + QUOTENAME(xml_tag_name)
FROM 
    (SELECT DISTINCT xml_tag_name FROM DataEntries) AS TagValues

SET @DynamicPivot =
N'SELECT DISTINCT capture_id, ' + @ColumnName + '
FROM DataEntries
PIVOT(MAX(xml_tag_value) 
FOR xml_tag_name IN (' + @ColumnName + ')) AS PVTTable'

EXEC sp_executesql @DynamicPivot

All the articles I've gone through deal with normal export to Excel or static pivots. 我读过的所有文章都涉及到正常导出到Excel或静态数据透视表。 eg: Export Table from SQL Server to Excel 2007 using C# . 例如: 使用C#将表从SQL Server导出到Excel 2007

How do I go about exporting this dynamic pivot to Excel? 如何将该动态数据透视表导出到Excel?

heres a example for you. 这是给你的例子。

It creates dynamic pivot to global temp table and then exports it to excel. 它创建到全局临时表的动态数据透视表,然后将其导出到excel。 If you find problems with export part - let me know. 如果您发现出口零件有问题,请告诉我。

CREATE TABLE Table1 (ColId INT,ColName VARCHAR(10))
INSERT INTO Table1 VALUES(1, 'Country')
INSERT INTO Table1 VALUES(2, 'Month')
INSERT INTO Table1 VALUES(3, 'Day')

CREATE TABLE Table2 (tID INT,ColID INT,Txt VARCHAR(10))

INSERT INTO Table2 VALUES (1,1, 'US')
INSERT INTO Table2 VALUES (1,2, 'July')
INSERT INTO Table2 VALUES (1,3, '4')
INSERT INTO Table2 VALUES (2,1, 'US')
INSERT INTO Table2 VALUES (2,2, 'Sep')
INSERT INTO Table2 VALUES (2,3, '11')
INSERT INTO Table2 VALUES (3,1, 'US')
INSERT INTO Table2 VALUES (3,2, 'Dec')
INSERT INTO Table2 VALUES (3,3, '25')


DECLARE @cols NVARCHAR(2000);
SELECT  @cols = COALESCE(@cols + ',[' + colName + ']', '[' + colName + ']')
FROM    Table1
ORDER BY colName;
IF OBJECT_ID('tempdb..##t1') IS NOT NULL
    BEGIN
        DROP TABLE ##t1;
    END;

DECLARE @query NVARCHAR(4000);
SET @query = N'SELECT tID, ' + @cols + ' into ##t1
FROM
(SELECT  t2.tID
      , t1.ColName
      , t2.Txt
FROM    Table1 AS t1
        JOIN Table2 AS t2 ON t1.ColId = t2.ColID) p
PIVOT
(
MAX([Txt])
FOR ColName IN
( ' + @cols + ' )
) AS pvt
ORDER BY tID;';


EXECUTE(@query);

SELECT  *
FROM    ##t1;


DECLARE @sql VARCHAR(MAX);
DECLARE @FileName VARCHAR(MAX) = 'C:\Test.xls';
SET @sql = 'INSERT INTO OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',''Excel 12.0;Database='
    + @FileName + ''',''SELECT * FROM [Sheet1$]'') SELECT * FROM ##T1';
EXECUTE(@sql);

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

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