SQLSERVER使用CLR Stored Procedure导出数据到Excel
在实际应用中,我们经常需要导出数据到Excel文件,你可以使用DTS或SSIS来做。但有时,我们并不需要这么重量级的工具,直接用CLR SP实现就可以了。
假设你已经写好了导出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#导出Excel细节代码在这儿就不贴了。编译成Assembly,这里我们叫ExcelExport.dll。
首先,启用SQL CLR
1: sp_configure'clr',1
2: reconfigure
设置Database可依赖性:
ALTER DATABASE [AdventureWorks] SET TRUSTWORTHY ON
接下来,我们在AdventureWorks 数据库中注册这个Assembly.
1: CREATE ASSEMBLY ExportToExcel
2: FROM 'D:\ExcelExport.dll'
3: WITH PERMISSION_SET = EXTERNAL_ACCESS
创建一个存储过程关联它:
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]
好的。让我们创建一个查询数据的SP,加一个'MyContactList'列,导出时将取这个名称做为Sheet的名称,另返回多个数据集将生成多个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
现在,我们可以使用了,
Declare @params xml
Set @params = '<params><param name="FirstName" value="Peter" /></params>'
--Set @params = '<params />'
exec [AdventureWorks].[dbo].[prc_ExportToExcel]
'[AdventureWorks].[dbo].[GetContactByFirstName]', 'D:\', 'MyContact', @params
注意,传递参数是xml,如果无参数刚传’<params />’,FirstName是之前那个SP的参数。最后,运行,将生成文件 D:\MyContact.xls
完了,由此我们可以看到SQL CLR的强大。
Author:Petter Liu http://wintersun.cnblogs.com
希望对您有帮助!
SQLSERVER使用CLR Stored Procedure导出数据到Excel相关推荐
- java导出为excel文件_java导出数据到excel文件
有的时候,将一些有用的数据导出到excel是很有必要的.比如说,我现在在做一个学校的在线教学平台,有一个需求是:将学生成绩导出到excel文件中去. 那怎样实现用java导出数据到excel文件呢?? ...
- 百度指数常见php框架,怎么导出数据到excel表格-如何将百度指数数据导出到Excel表格...
如何将百度指数数据导出到Excel表格 第一步:打开CAD.CAD命令行输入"Li"."选择对象"选需要提取坐标的多段线.回车. 第二步:将CAD文本框中的数据 ...
- arcengine遍历属性表_【程序之坑】小程序云开发导出数据到excel表
本文解决了小程序云开发导出数据到excel的bug,并给出了分析的过程,同时简化了程序为一个云函数,一个本地函数,逻辑更简单.文章结尾给出了完整源代码 小程序云开发为没有服务器的小程序开发者提供了便利 ...
- python输出数据到excel-python如何导出数据到excel文件
python导出数据到excel文件的方法: 1.调用Workbook()对象中的add_sheet()方法wb = xlwt.Workbook() ws = wb.add_sheet('A Test ...
- python输出数据到excel-Python从数据库导出数据到Excel
#coding=utf-8 import pymysql import xlwt import importlib,sys importlib.reload(sys) # __author__ = ' ...
- (转)另一篇:VC导出数据到EXCEL
另一篇:VC导出数据到EXCEL 2010-08-28 17:03:47| 分类: 默认分类 | 标签: |字号大中小 订阅 我们制作应用软件的时候,经常要把结果以报表的形式输出,当前使用较为广泛 ...
- 前端利用JS导出数据到Excel表 数字是文本类型 无法计算
问题描述:前端利用JS导出数据到Excel表 数字是文本类型 无法进行公式计算:前端利用JS导出数据到Excel表 数字是文本类型 无法计算 解决办法:参考https://bbs.csdn.net/t ...
- spring boot实现导出数据到excel
1.导入依赖; <!--数据导出依赖 excel--> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --&g ...
- 从DataTable高效率导出数据到Excel
首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: 1 using Microsoft.Office ...
最新文章
- 《李宏毅机器学习特训营》免费开放!直播教学!
- error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or
- Mysql统计分组区间的人数和 1
- 云信小课堂|如何实现音视频通话
- mongodb系列教程
- linux无盘windows系统,Linux无盘启动Win2000终端
- 容器和云服务器集群,什么是docker集群与镜像
- Nodejs Guides(四)
- Jmeter 使用脚本 生成html测试报告
- 【codevs3160】 LCS 【后缀自动机】
- spring配置文件最全约束
- 数据结构期末复习(四)
- rsyslog官方文档
- 拓端tecdat|matlab如何从信号中去除60 Hz Hum电源线噪声
- 电信私有云 oracle,行业观察:Oracle私有云升级
- AI和大数据结合,智能运维平台助力流利说提升竞争力
- 软件源码破解工具De4Dot用法,net破解、反混淆
- 文件夹批量重命名编号的快速方法
- MATLAB高斯迭代算法,matlab实现高斯赛德尔迭代法解方程组
- Vue3.0 项目启动(打造企业级音乐App)