日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

SQLSERVER使用CLR Stored Procedure导出数据到Excel

發(fā)布時間:2025/5/22 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLSERVER使用CLR Stored Procedure导出数据到Excel 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

????? 在實際應(yīng)用中,我們經(jīng)常需要導(dǎo)出數(shù)據(jù)到Excel文件,你可以使用DTS或SSIS來做。但有時,我們并不需要這么重量級的工具,直接用CLR SP實現(xiàn)就可以了。
假設(shè)你已經(jīng)寫好了導(dǎo)出Excel的C# code:

1: /// <summary> 2: /// This is the method registered with SQL Server as a 3: /// CLR stored procedure. The attribute, Microsoft.SqlServer.Server.SqlProcedure, is 4: /// required for the method to be a CLR stored procedure. 5: /// </summary> 6: [Microsoft.SqlServer.Server.SqlProcedure] 7: public static void ExportToExcel(SqlString procName, SqlString filePath, SqlString fileName, SqlXml xmlParams) 8: { 9: DataSet exportData = new DataSet(); 10: ? 11: //check for empty parameters 12: ? 13: if (procName.Value == string.Empty) 14: throw new Exception("Procedure name value is missing."); 15: ? 16: if (filePath.Value == string.Empty) 17: throw new Exception("Missing file path location."); 18: ? 19: if (fileName.Value == string.Empty) 20: throw new Exception("Missing name of file."); 21: ? 22: using (SqlConnection conn = new SqlConnection("context connection=true")) 23: { 24: SqlCommand getOutput = new SqlCommand(); 25: ? 26: getOutput.CommandText = procName.ToString(); ; 27: getOutput.CommandType = CommandType.StoredProcedure; 28: getOutput.CommandTimeout = 120; 29: ? 30: //To allow for multiple parameters, xml is used 31: //and must then be parsed to set up the paramaters 32: //for the command object. 33: using (XmlReader parms = xmlParams.CreateReader()) 34: { 35: while(parms.Read()) 36: { 37: if (parms.Name == "param") 38: { 39: string paramName; 40: paramName = parms.GetAttribute("name"); 41: ? 42: string paramValue; 43: paramValue = parms.GetAttribute("value"); 44: ? 45: getOutput.Parameters.AddWithValue(paramName, paramValue); 46: } 47: } 48: } 49: ? 50: getOutput.Connection = conn; 51: ? 52: conn.Open(); 53: SqlDataAdapter da = new SqlDataAdapter(getOutput); 54: da.Fill(exportData); 55: conn.Close(); 56: } 57: ? 58: ExcelExportUtility exportUtil = new ExcelExportUtility(fileName.ToString(),filePath.ToString()); 59: //This allows for flexible naming of the tabs in the workbook 60: exportUtil.SheetNameColumnOrdinal = 0; 61: exportUtil.Export(exportData); 62: }

C#導(dǎo)出Excel細節(jié)代碼在這兒就不貼了。編譯成Assembly,這里我們叫ExcelExport.dll。

首先,啟用SQL CLR

1: sp_configure'clr',1 2: reconfigure

設(shè)置Database可依賴性:

ALTER DATABASE [AdventureWorks] SET TRUSTWORTHY ON


接下來,我們在AdventureWorks 數(shù)據(jù)庫中注冊這個Assembly.

1: CREATE ASSEMBLY ExportToExcel 2: FROM 'D:\ExcelExport.dll' 3: WITH PERMISSION_SET = EXTERNAL_ACCESS


創(chuàng)建一個存儲過程關(guān)聯(lián)它:

1: CREATE PROCEDURE [dbo].[prc_ExportToExcel] 2: @proc [nvarchar](100), 3: @path [nvarchar](200), 4: @filename [nvarchar](100), 5: @params xml 6: AS 7: EXTERNAL NAME [ExportToExcel].[StoredProcedures].[ExportToExcel]

好的。讓我們創(chuàng)建一個查詢數(shù)據(jù)的SP,加一個'MyContactList'列,導(dǎo)出時將取這個名稱做為Sheet的名稱,另返回多個數(shù)據(jù)集將生成多個Sheet頁。:

1: CREATE PROC GetContactByFirstName 2: @FirstName nvarchar(50) 3: AS 4: BEGIN 5: SELECT 'MyContactList' 6: ,[ContactID] 7: ,[FirstName] 8: ,[LastName] 9: ,[EmailAddress] 10: ,[Phone] 11: FROM [AdventureWorks].[Person].[Contact] Where FirstName=@FirstName 12: END

現(xiàn)在,我們可以使用了,

Declare @params xmlSet @params = '<params><param name="FirstName" value="Peter" /></params>'--Set @params = '<params />'exec [AdventureWorks].[dbo].[prc_ExportToExcel] '[AdventureWorks].[dbo].[GetContactByFirstName]', 'D:\', 'MyContact', @params

注意,傳遞參數(shù)是xml,如果無參數(shù)剛傳’<params />’,FirstName是之前那個SP的參數(shù)。最后,運行,將生成文件 D:\MyContact.xls

?

完了,由此我們可以看到SQL CLR的強大。

Author:Petter Liu?? http://wintersun.cnblogs.com

希望對您有幫助!

總結(jié)

以上是生活随笔為你收集整理的SQLSERVER使用CLR Stored Procedure导出数据到Excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。