1

Good evening everyone. I have to build a SSIS package that does as follows:

1) Execute a VBA code to a XLS file (Transpose a range into another range) 2) Save the XLS (In the same file or as a new file) 3) Import the modified XLS from the Transposed range.

Basically I have to transpose the data inside a XLS that I must import, and I didn't find a good way to do that in SSIS (Since the column range can change between files)

With this simple VBA script I can do that and make SSIS read the data in a very straightforward way. However I'm not finding a way to apply this code without modifying the Excel previously manually to add the script and run the VBA script. I want to automate this so the package prepares the xls, extracts the new data, and save it to a table.

Can anyone shed some ideas on how to apply this code or other ways to do this? The most important point I think is that it's a very specific range that I want to transpose.

Sub myTranspose()
    With Range("a18:ZZ27", Range("a18:ZZ27").End(xlDown))
        .Copy
        Range("a30").PasteSpecial Transpose:=True
    End With
End Sub
4
  • Does the column range change between files in a predictable way? As in, one type of file always has 3 columns and the other has 6. I ask because this could make for a simpler solution. Commented Feb 12, 2015 at 7:26
  • No, the files keep changing the number of columns because they are automatic files generated with data about Years and Months. All of that data is on columns instead of lines. And this year they have a set number of columns but next year they will defnitely grow (add to the current number of columns) Commented Feb 12, 2015 at 9:28
  • Maybe try a script task to transpose the data. then save and import? Commented Feb 12, 2015 at 13:08
  • It was one of the ideas, but how can I start the script from SSIS? It must be executed when the package runs. Commented Feb 12, 2015 at 16:12

1 Answer 1

2
  1. Create a Script Task that is piped into a Data Flow task

    STask to DFtask

  2. Edit the Script Task by double clicking the Script Task and clicking the Edit Script button.

  3. Add references to Excel and CSharp as seen in this answer

  4. Add some code similar to the following:

    public void Main()
    {
      string filepath = @"c:\temp\transpose.xlsx";
      Excel.Application xlApp;
      Excel._Workbook oWB;
    
      try
      {
        xlApp = new Excel.Application();
        xlApp.Visible = false;
        oWB = (Excel.Workbook)xlApp.Workbooks.Open(filepath);
        Excel.Range fromrng = xlApp.get_Range("B4", "F5");
        Object[,] transposedRange = (Object[,])xlApp.WorksheetFunction.Transpose(fromrng);
        Excel.Range to_rng = xlApp.get_Range("A8", "A8");
        to_rng = to_rng.Resize[transposedRange.GetUpperBound(0), transposedRange.GetUpperBound(1)];
        to_rng.Value = transposedRange;
        xlApp.ActiveWorkbook.Save();
        oWB.Close(filepath);
    
      }
      catch (Exception ex)
      {
        //do something
      }
    
    Dts.TaskResult = (int)ScriptResults.Success;
    }
    
  5. This gives the following result in the sample transpose.xlsx I created. Transpose

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I needed. Thank you very much, I'll be able to read from the new range (A8:B12 range in the e.g) with the data in the correct form and with fixed columns. Thank you! I really need to sharp my C# (no pun intended) skills, it really opens a wide range of options, didn't know that C# could work directly with XLS files.

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.