Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SSIS中的Excel连接将Sql数据转换为Excel文件_Excel_Ssis - Fatal编程技术网

使用SSIS中的Excel连接将Sql数据转换为Excel文件

使用SSIS中的Excel连接将Sql数据转换为Excel文件,excel,ssis,Excel,Ssis,我想知道是否有机会将Sql数据保存到excel文件,而不使用excel目标任务,也不使用脚本任务中的excel连接。 如果有任何答案,请帮助我。 谢谢。我找到了一种将SQL数据导入Excel工作表的方法,无需Excel任务和ADO.net的Excel连接或任何SSIS Excel连接。我是在一个脚本任务中完成的。我使用了documentformat.openxml包。我在NuGet manager中安装了openxml包。然后它没有工作,显示出一些错误。当我打开代码引用时,发现了一些错误。然后,

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

我找到了一种将SQL数据导入Excel工作表的方法,无需Excel任务和ADO.net的Excel连接或任何SSIS Excel连接。我是在一个脚本任务中完成的。我使用了documentformat.openxml包。我在NuGet manager中安装了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();
            }

        }
    }
}
使用DocumentFormat.OpenXml;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Spreadsheet;
使用制度;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.SqlClient;
命名空间DataToExcel
{
班级计划
{
静态void Main(字符串[]参数)
{
使用(电子表格文档=电子表格文档.Create(@“E:\Work\One.xlsx”,电子表格文档类型.Workbook))
{
string connString=“数据源=本地主机;初始目录=学校;集成安全性=True;”;//Windows身份验证
SqlConnection sqlConn=新的SqlConnection(connString);
sqlConn.Open();
SqlCommand cmd=新的SqlCommand(“测试”,sqlConn);
cmd.CommandType=CommandType.storedProcess;
DataTable dt=新的DataTable();
dt.Load(cmd.ExecuteReader());
sqlConn.Close();
列表头=新列表();
int count=dt.Rows.count;
foreach(dt.Columns中的DataColumn列)
{
header.Add(column.ColumnName);
}
//下面是Excel保存的代码。
WorkbookPart WorkbookPart=document.AddWorkbookPart();
workbookPart.工作簿=新工作簿();
WorksheetPart WorksheetPart=workbookPart.AddNewPart();
var sheetData=新的sheetData();
工作表部分工作表=新工作表(sheetData);
Sheets Sheets=workbookPart.工作簿.AppendChild(新工作表());
Sheet Sheet=new Sheet(){Id=workbookPart.GetIdOfPart(worksheetPart),SheetId=1,Name=“Sheet1”};
附页(页);
Row headerRow=新行();
foreach(标题中的变量项)
{
单元格=新单元格();
cell.DataType=CellValues.String;
cell.CellValue=新的CellValue(项);
头附子(单元);
}
sheetData.AppendChild(headerRow);
foreach(数据行中的数据行)
{
行newRow=新行();
foreach(标题中的字符串列)
{
单元格=新单元格();
cell.DataType=CellValues.String;
cell.CellValue=新的CellValue(dsrow[col].ToString());
newRow.AppendChild(单元格);
}
sheetData.AppendChild(纽罗);
}
workbookPart.Workbook.Save();
}
}
}
}

是的,您可以使用
ScriptTask
并使用
C#
完成任务,但为什么要执行已有工具的操作?或者导出到平面文件CSV(逗号分隔值)并在Excel中打开。