Professional Documents
Culture Documents
package
2. Create an SSIS package with a C# script task and configure it for EPPlus
EPPlus can also be used with VB.NET. See the appendix for notes on how to do it. ***
Create a new SSIS package and add a 'Script Task' to the 'Control Flow' panel, then double-click on the 'Script Task' component to bring up the
'Script Task Editor' dialog box.
Zoom in | Open in new window
Click the 'Edit Script' button of the 'Script Task Editor' dialog box to bring up the C# script task code editor..
Right-click the top node of the 'Project Explorer' window of the script task code editor and select the 'Properties' menu item.
Zoom in | Open in new window
Selecct '.NET Framework 3.5' from the 'Target Framework' drop-down menu.
Zoom in | Open in new window
Click the 'Yes' button of the 'Target Framework Change' dialog box.
Zoom in | Open in new window
Close the script task code editor window.
Zoom in | Open in new window
Reopen the script task code editor by again clicking the 'Edit Script' button of the 'Script Task Editor' dialog box.
Right-click the 'References' folder of the 'Project Explorer' and select the 'Add Reference' menu item.
Zoom in | Open in new window
Browse to and select the EPPlus.dll assembly that we previously extracted from the downloaded EPPlus zip file, then click the 'OK' button.
Zoom in | Open in new window
Note the addition of the 'EPPlus' item in the 'References' folder.
Zoom in | Open in new window
Add 'using OfficeOpenXml' and 'using OfficeOpenXml.Style' statements to the namespace list.
Zoom in | Open in new window
At this point the script task is configured to use EPPlus for reading and writing Excel files.
4. Add an ADO.NET connection to the SSIS package pointing to the database where the test table was created
Right-click on the 'Connection Managers' panel and select 'New ADO.NET Connection' from the menu list.
Zoom in | Open in new window
Click the 'New' button of the 'Configure ADO.NET Connection Manager' dialog box.
Select the server and database where the [Market] table was created, then click the 'OK' button.
The database connection has been added to the SSIS package.
Zoom in | Open in new window
5. Paste code that creates and writes to an Excel spreadsheet into the C# script code editor
Reopen the script task code editor and paste the following code over the existing code.
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using OfficeOpenXml;
using OfficeOpenXml.Style;
namespace ST_32854b6cabab4240bf64b339049c3891.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain :
Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
//DEFINE THE DEFAULT FONT TYPE AND SIZE FOR THE WORKSHEET
worksheet.Cells.Style.Font.Size = 11; //Default font size for whole sheet
worksheet.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole
sheet
//SET THE FIRST ROW TO BE WRITTEN TO THE WORKSHEET FROM THE QUERY TO ROW 4
int row_index = 4;
//LOOP THROUGH THE QUERY RESULT SET AND WRITE THE FIELD VALUES TO CELLS IN
THE WORKSHEET
while (reader.Read())
{
worksheet.Cells[row_index, 1].Value = reader["Symbol"].ToString();
worksheet.Cells[row_index, 2].Value = reader["Company"].ToString();
worksheet.Cells[row_index, 3].Value =
System.Convert.ToDecimal(reader["Price"].ToString());
worksheet.Cells[row_index, 4].Value =
System.Convert.ToDecimal(reader["Change"].ToString());
worksheet.Cells[row_index, 5].Value =
System.Convert.ToDecimal(reader["PcntChange"].ToString());
//INCREMENT WRITING TO THE NEXT ROW IN THE WORKSHEET FOR THE NEXT RECORD
IN THE RESULT SET
row_index += 1;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
The code contains comments that describe how EPPlus is being used.
Zoom in | Open in new window
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
'DEFINE THE DEFAULT FONT TYPE AND SIZE FOR THE WORKSHEET
worksheet.Cells.Style.Font.Size = 11
worksheet.Cells.Style.Font.Name = "Calibri"
'SET THE FIRST ROW TO BE WRITTEN TO THE WORKSHEET FROM THE QUERY TO ROW 4
Dim row_index As Integer = 4
'LOOP THROUGH THE QUERY RESULT SET AND WRITE THE FIELD VALUES TO CELLS IN THE
WORKSHEET
While reader.Read()
worksheet.Cells(row_index, 1).Value = reader("Symbol").ToString()
worksheet.Cells(row_index, 2).Value = reader("Company").ToString()
worksheet.Cells(row_index, 3).Value =
System.Convert.ToDecimal(reader("Price").ToString())
worksheet.Cells(row_index, 4).Value =
System.Convert.ToDecimal(reader("Change").ToString())
worksheet.Cells(row_index, 5).Value =
System.Convert.ToDecimal(reader("PcntChange").ToString())
'INCREMENT WRITING TO THE NEXT ROW IN THE WORKSHEET FOR THE NEXT RECORD IN
THE RESULT SET
row_index += 1
End While
Dts.TaskResult = ScriptResults.Success
End Sub
End Class
To change the .NET framework for the VB.NET script, click on the 'Project-Properties' menu item in the VB editor window.
Zoom in | Open in new window
Then click the 'Compile' tab, followed by the 'Advanced Compile Options' button.
Zoom in | Open in new window
Then select '.NET Framework 3.5' from the 'Target framework' dropdown list.
Add the reference for the EPPlus.dll assembly the same way as for the C# code.
Zoom in | Open in new window
The VB.NET code already contains the 'Imports OfficeOpenXml' and 'Imports OfficeOpenXml.Style' statements.