【问题标题】:Sql Data to Excel File with Excel connection in SSISSSIS中带有Excel连接的Sql数据到Excel文件
【发布时间】:2024-05-31 09:15:01
【问题描述】:

我想知道是否有机会在不使用 Excel 目标任务且不使用脚本任务中的 Excel 连接的情况下将 Sql 数据保存到 excel 文件。 如果有任何答案,请帮助我。 谢谢。

【问题讨论】:

  • 是的,您可以使用ScriptTask 并使用C# 完成所有工作,但是您为什么要执行已经存在工具的事情。
  • 或导出为平面文件 CSV(逗号分隔值),它将在 Excel 中打开。

标签: excel ssis


【解决方案1】:

我找到了一种无需 Excel 任务和 ADO.net 的 excel 连接或任何 SSIS Excel 连接即可将 SQL 数据导入 Excel 工作表的方法。我在脚本任务中做到了。我使用了 documentformat.openxml 包。我在 NuGet 管理器中安装了 openxml 包。然后它没有显示一些错误。当我打开代码参考时,出现了一些错误。然后,我手动更改了包的引用路径并尝试了。有效。希望它会帮助某人。 谢谢,

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DataToExcel
{
    class Program
    {

        static void Main(string[] args)
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Create(@"E:\Work\One.xlsx", SpreadsheetDocumentType.Workbook))
            {

                string connString = "Data Source=localhost;Initial Catalog=School;Integrated Security=True;";//Windows authentication
                
                SqlConnection sqlConn = new SqlConnection(connString);
                sqlConn.Open();
                SqlCommand cmd = new SqlCommand("Test", sqlConn);
                cmd.CommandType = CommandType.StoredProcedure;
                
                DataTable dt = new DataTable();
                dt.Load(cmd.ExecuteReader());
                sqlConn.Close();
                List<string> header = new List<string>();
                int count = dt.Rows.Count;
                foreach (DataColumn column in dt.Columns)
                {
                    header.Add(column.ColumnName);
                }

                //Here is the code for Excel saving.
                WorkbookPart workbookPart = document.AddWorkbookPart();
                workbookPart.Workbook = new Workbook();

                WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                var sheetData = new SheetData();
                worksheetPart.Worksheet = new Worksheet(sheetData);

                Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
                Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };

                sheets.Append(sheet);

                Row headerRow = new Row();

                foreach (var item in header)
                {
                    Cell cell = new Cell();
                    cell.DataType = CellValues.String;
                    cell.CellValue = new CellValue(item);
                    headerRow.AppendChild(cell);
                }

                sheetData.AppendChild(headerRow);

                foreach (DataRow dsrow in dt.Rows)
                {
                    Row newRow = new Row();
                    foreach (String col in header)
                    {
                        Cell cell = new Cell();
                        cell.DataType = CellValues.String;
                        cell.CellValue = new CellValue(dsrow[col].ToString());
                        newRow.AppendChild(cell);
                    }

                    sheetData.AppendChild(newRow);
                }

                workbookPart.Workbook.Save();
            }

        }
    }
}

【讨论】:

    最近更新 更多