0

In an SSIS package I need to put the content of 2 SQL queries in 2 sheets of an Excel file. The results of the 2 queries are in 2 object variables, I'm wondering if I can use a Data Flow Task for this. Issues :

  1. I don't know what columns I will have (number of columns, names of columns), the query is created at the moment of the execution Set @qry = 'Select ...' EXEC(@qry)
  2. I have the same problem with the Excel file, I can't have a precise Template, all I know about the Excel File is that we'll have 2 sheets. Is this possible? If not, is there another way (Script task, SSRS...)?

1 Answer 1

2

As a general answer, no, you can't do this with SSIS. Since you tagged this with C# however, you can use OLE to add sheets to an Excel file and add data to those sheets, http://jayadevjyothi.blogspot.com/2013/01/step-1-create-new-project.html . This can be done outside of SSIS, or if your solution needs to run inside SSIS, you can put the C# inside a script task.

           // 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();
            }
        }
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the answer, the thing is that I can't use Command.CommandText = "CREATE TABLE [Students] ([First Name] Char(200), [Last Name] Char(200), [Age] Char(2))"; Because I don't know what columns I will have.
You can't hardcode the create table statement, I understand that. You say that the results of the two queries are in two object variables. I can't say that I've done that much in SSIS, but I'm assuming that the storage mechanism is an ADO.Net DataTable, and you can cast the object variable value back into an instance of a DataTable, then loop over the columns collection pulling out the column name and concatenate together a "create table ..." string. You would then do the same type of thing to dynamically piece together the insert statement.
Thank you @shirop it works. Is there a way of controlling the columns size in the file (to have the column adjusted to the content)?
not that I can think of thru OLE. if you used a C# library for creating the Excel spreadsheet, then maybe, but I think even with that, it's more of a client function after the data has been added.
Thank you again. If I find a solution I will post it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.