c# - 为 CLR 过程添加程序集

标签 c# sql-server interop clr

我正在尝试创建一个 CLR 过程以将 SQL 数据导出到 Excel,该过程将包含比其他选项更多的功能,例如小计和突出显示。

这要求我引用 Microsoft.Office.Interop.Excel dll,但我不确定在编译代码时如何实际包含该程序集。

如何将 Excel 程序集包含在我的 CLR 过程中?

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

public class ExportToExcel
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void ExportQueryResults(string queryText, string worksheetName, string fileName)
{
    using (SqlConnection cnn = new SqlConnection("context connection=true"))
    {
        //the temp list to hold the results in
        List<object[]> results = new List<object[]>();

        cnn.Open();
        //create the sql command
        SqlCommand cmd = new SqlCommand(queryText, cnn);
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            int fieldCount = reader.FieldCount;
            object[] headers = new object[fieldCount];
            for(int i = 0; i < fieldCount; i++)
            {
                headers[i] = reader.GetName(i);
            }

            //read the results
            while (reader.Read())
            {
                object[] values = new object[fieldCount];
                for (int i = 0; i < fieldCount; i++)
                {
                    values[i] = reader[i];
                }
                results.Add(values);
            }

            //convert the results into a 2-d array to export into Excel
            object[,] exportVals = new object[results.Count, fieldCount];

            for (int row = 0; row < results.Count; row++)
            {
                for (int col = 0; col < fieldCount; col++)
                {
                    exportVals[row, col] = results[row][col];
                }
            }

            Excel.Application _app = new Excel.Application();
            Excel.Workbook _book = _app.Workbooks.Add(Missing.Value);
            Excel.Worksheet _sheet = (Excel.Worksheet)_book.ActiveSheet;
            Excel.Range _range = (Excel.Range)_sheet.Cells[1, 1];

            _range = _sheet.get_Range(_sheet.Cells[1, 1], _sheet.Cells[results.Count, fieldCount]);
            _range.Value2 = exportVals;

            _sheet.Name = worksheetName;

            //remove any extra worksheets
            foreach(Excel.Worksheet sht in _book.Worksheets)
            {
                if (sht.Name != worksheetName)
                    sht.Delete();
            }

            _book.SaveAs(fileName
                , Excel.XlFileFormat.xlWorkbookDefault
                , Missing.Value
                , Missing.Value
                , false
                , false
                , Excel.XlSaveAsAccessMode.xlNoChange
                , Missing.Value
                , Missing.Value
                , Missing.Value
                , Missing.Value
                , Missing.Value);
        }
    }
}
}

最佳答案

不可能在 SQL Server 中使用任意程序集。您只需要引用 subset框架和练习一些basic discipline 。我怀疑具有历史层的怪物应用程序(例如 Excel)是否会加载到此环境中。

您可能想看看更简单的 substitute functionality符合这个标准。

如果您需要更多,请考虑在客户端使用 Excel。

关于c# - 为 CLR 过程添加程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11090717/

相关文章:

c# - nHibernate 中的 Save() 和 SaveOrUpdate() 有什么区别

c# - 正则表达式花费了惊人的长时间

c# - 如何在 C# 中动态加载 VB 6.0 dll?

wpf - 在 XAML WPF 中插入 Winforms 表单

c# - 基本 GUI 事件处理问题 C#

C# XNA 相当于带有比较器的 Java PriorityQueue?

sql-server - 即使是一个非常简单的查询,我在 SQL Server 中的空间索引仍然需要大量读取。为什么?

sql - 将第一张表的数据排序后从另一张表插入到表中

c# - 插入列表 <> 到 SQL Server 表

c# - Office.Interop.Powerpoint 导入