具有可变源的数据流任务

时间:2015-06-19 22:05:12

标签: c# excel ssis

在SSIS包中,我需要将2个SQL查询的内容放在2个Excel文件中。 2个查询的结果是2个对象变量,我想知道我是否可以使用数据流任务。问题:

  1. 我不知道我将拥有哪些列(列数,列名),查询是在执行时创建的 final int[] ints = new int[]{32, 25, 56, 56, 12, 20, 22, 19, 54, 22}; Arrays.sort(ints); System.out.println(Arrays.toString(ints));
  2. 我对Excel文件有同样的问题,我没有精确的模板,我所知道的Excel文件就是我们有2张。这可能吗?如果没有,还有另一种方式(脚本任务,SSRS ......)?

1 个答案:

答案 0 :(得分:2)

作为一般答案,不,你不能用SSIS做到这一点。但是,由于您使用C#对其进行了标记,因此可以使用OLE将工作表添加到Excel文件并将数据添加到这些工作表http://jayadevjyothi.blogspot.com/2013/01/step-1-create-new-project.html。这可以在SSIS之外完成,或者如果您的解决方案需要在SSIS中运行,您可以将C#放在脚本任务中。

           // Excel file path
        string excelFilePath = @"F:\Excel File.xlsx";
        // Connection string for accessing excel file
        string connectionString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=""Excel 12.0 Xml;HDR=YES""", excelFilePath);

        using (OleDbConnection Connection = new OleDbConnection(connectionString))
        {
            try
            {
                Connection.Open();
                using (OleDbCommand Command = new OleDbCommand())
                {
                    Command.Connection = Connection;
                    Command.CommandText = "CREATE TABLE [Students] ([First Name] Char(200), [Last Name] Char(200), [Age] Char(2))";
                    Command.ExecuteNonQuery();
                    Console.WriteLine("Table Created Successfully");
                }
                Console.ReadLine();
            }
            catch (OleDbException ex)
            {
                Console.WriteLine("Message: " + ex.Message);
                Console.ReadLine();
            }
            finally
            {
                Connection.Close();
            }
        }