SQLServer數據導出到excel有很多種方法
下面我先演示一下我實現的效果
exec BulkCopyToXls
/*
開始導出數據
文件 d:/test/testTable
文件 d:/test/testTable
文件 d:/test/testTable
文件 d:/test/testTable
文件 d:/test/testTable
文件 d:/test/testTable
文件 d:/test/testTable
文件 d:/test/testTable
導出數據完成
共
*/
上面的BulkCopyToXls存儲過程是自定的CLR存儲過程
第一個是sql語句用來獲取數據集
第二個是文件保存的路徑
第三個是結果集的名字
第四個是限制單個文件可以保存多少條記錄
前 三個參數沒有什麼特別
另外每個文件都可以輸出字段名作為表頭
用微軟公開的biff
下面我們來看下CLR代碼
using System;
using System
using System
using System
using Microsoft
public partial class StoredProcedures
{
/// <summary>
/// 導出數據
/// </summary>
/// <param name="sql"></param>
/// <param name="savePath"></param>
/// <param name="tableName"></param>
/// <param name="maxRecordCount"></param>
[Microsoft
public static void BulkCopyToXls(SqlString sql
{
if (sql
{
SqlContext
}
ushort _maxRecordCount = ushort
if (maxRecordCount
_maxRecordCount = (ushort )maxRecordCount
ExportXls(sql
}
/// <summary>
/// 查詢數據
/// </summary>
/// <param name="sql"></param>
/// <param name="savePath"></param>
/// <param name="tableName"></param>
/// <param name="maxRecordCount"></param>
private static void ExportXls(string sql
{
if (System
{
System
}
using (SqlConnection conn = new SqlConnection ("context connection=true" ))
{
conn
using (SqlCommand command = conn
{
command
using (SqlDataReader reader = command
{
int i =
int totalCount =
int tick = System
SqlContext
while (true )
{
string fileName = string
int iExp = Write(reader
long size = new System
totalCount += iExp;
SqlContext
if (iExp < maxRecordCount) break ;
}
tick = System
SqlContext
SqlContext
SqlContext
}
}
}
}
/// <summary>
/// 寫單元格
/// </summary>
/// <param name="writer"></param>
/// <param name="obj"></param>
/// <param name="x"></param>
/// <param name="y"></param>
private static void WriteObject(ExcelWriter writer
{
string type = obj
switch (type)
{
case "SqlBoolean" :
case "SqlByte" :
case "SqlDecimal" :
case "SqlDouble" :
case "SqlInt
case "SqlInt
case "SqlInt
case "SqlMoney" :
case "SqlSingle" :
if (obj
writer
else
writer
break ;
default :
writer
break ;
}
}
/// <summary>
/// 寫一批數據到一個excel 文件
/// </summary>
/// <param name="reader"></param>
/// <param name="count"></param>
/// <param name="fileName"></param>
/// <returns></returns>
private static int Write(SqlDataReader reader
{
int iExp = count;
ExcelWriter writer = new ExcelWriter (fileName);
writer
for (System
{
writer
}
for (System
{
if (reader
{
iExp = i
break ;
}
for (System
{
WriteObject(writer
}
}
writer
return iExp;
}
/// <summary>
/// 寫excel 的對象
/// </summary>
public class ExcelWriter
{
System
public ExcelWriter(string strPath)
{
_wirter = new System
}
/// <summary>
/// 寫入short 數組
/// </summary>
/// <param name="values"></param>
private void _writeFile(System
{
foreach (System
{
byte [] b = System
_wirter
}
}
/// <summary>
/// 寫文件頭
/// </summary>
public void BeginWrite()
{
_writeFile(new System
}
/// <summary>
/// 寫文件尾
/// </summary>
public void EndWrite()
{
_writeFile(new System
_wirter
}
/// <summary>
/// 寫一個數字到單元格x
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="value"></param>
public void WriteNumber(System
{
_writeFile(new System
byte [] b = System
_wirter
}
/// <summary>
/// 寫一個字符到單元格x
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="value"></param>
public void WriteString(System
{
byte [] b = System
_writeFile(new System
_wirter
}
}
};
把上面代碼編譯為TestExcel
CREATE ASSEMBLY TestExcelForSQLCLR FROM
go
CREATE proc dbo
(
@sql nvarchar ( max )
@savePath nvarchar (
@tableName nvarchar (
@bathCount int
)
AS EXTERNAL NAME TestExcelForSQLCLR
go
當 這項技術掌握在我們自己手中的時候
From:http://tw.wingwit.com/Article/program/MySQL/201311/29574.html