excelssisssmsssms-16ssis-2017

Load data into multiple excel sheets depending on conditions dynamically


I have a situation please help me out. I have to create multiple sheet in one excel file with different queries. Like i have to check if the particular column is null then the record against this query should be in excel file in new sheet and i have to check another column with other name if it is null or empty and then create a sheet for it and sheet should be created only if the query returns some result otherwise there should not be any empty sheet. i have 8 different columns to check .

For Example I have to execute following query which will be in source

SELECT DISTINCT AgencySourceSystemCode,SourceAgencyID,ProgramCode,PolicyNumber,EffectiveDate,AgencyName 
FROM POL.vw_PolicyPremiumData 
WHERE AgencyName IS NULL OR AgencyName = '' 

And Sample result is

AgencySourceSystemCode SourceAgencyID
ProgramCode PolicyNumber
EffectiveDate AgencyName
GEN 1050-  CAB DN17000008
2010-06-10 NULL
GEN 1050-  CAB DN17000008
2011-06-10 NULL
GEN 1050-  CAB DN17000008
2012-06-10 NULL
GEN 1050-  CAB DN17000010
2010-06-10 NULL
GEN 1050-  CAB DN17000010
2012-06-10 NULL
GEN 1050-  CAB DN17000012
2010-06-22 NULL
GEN 1050-  CAB DN17000012
2011-06-22 NULL

Here Agency Name is NULL like this i will have source query where Effective can be null .


Solution

  • I used this code snippet to create dynamic Excel file sheets .

    using System;
    using System.Data;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    
    namespace ST_db9b6187d17c4dc99314d6ccb6ee7b08
    {
        [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
        public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
        {
            string query = string.Empty;
            string FileName = string.Empty;
            string TableName = string.Empty;
            string connstring = string.Empty;
            string FolderPath = string.Empty;
            string where = string.Empty;
            string PackageName = "Error";
            int SourceId = 0;
            int BatchId = 0;
            int flag = 0;
    
            public void Main()
            {
                string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
    
                try
                {
                    FolderPath = Dts.Variables["$Package::TempFolderPath"].Value.ToString();
                    FolderPath = FolderPath+"\\";
                    //if (FolderPath.LastIndexOf("\\")="\\")
                    //{
                    //    FolderPath = Dts.Variables["$Package::TempFolderPath"].Value.ToString();
                    //}
                    if (File.Exists(FolderPath + PackageName + "File.XLSX"))
                    {
                        File.Delete(FolderPath + PackageName + "File.XLSX");
                    }
    
                    if (FolderPath.Contains(".xlsx"))
                    {
                        FolderPath = FolderPath.Trim('\\');
                        FolderPath = FolderPath.Remove(FolderPath.LastIndexOf('\\') + 1);
                    }
    
                    //USE ADO.NET Connection from SSIS Package to get data from table
                    string con = Dts.Connections["oledb_conn"].ConnectionString.ToString();
                    OleDbConnection _connection = new OleDbConnection(con);
                    OleDbCommand cmd = new OleDbCommand();
    
                    //Read distinct error euerries 
                    query = "select distinct ErrorQuery, SheetName from ErrorMapping where FileType = 'PremiumFile'";
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandTimeout = 0;
                    cmd.CommandText = query;
                    cmd.Connection = _connection;
                    _connection.Open();
                    DataTable dt_ErrorMapping = new DataTable();
                    dt_ErrorMapping.Load(cmd.ExecuteReader());
                    _connection.Close();
    
                    if (dt_ErrorMapping != null && dt_ErrorMapping.Rows.Count > 0)
                    {
                        foreach (DataRow dt_ErrorMapping_row in dt_ErrorMapping.Rows)
                        {
                            query = dt_ErrorMapping_row["ErrorQuery"].ToString();
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = query;
                            cmd.CommandTimeout = 0;
                            cmd.Connection = _connection;
                            _connection.Open();
                            DataTable dt_ErrorInfo = new DataTable();
                            dt_ErrorInfo.Load(cmd.ExecuteReader());
                            _connection.Close();
    
                            if (dt_ErrorInfo != null && dt_ErrorInfo.Rows.Count > 0)
                            {
                                Error(dt_ErrorMapping_row["SheetName"].ToString());
                            }
    
                        }
                    }
    
                    Dts.TaskResult = (int)ScriptResults.Success;
                }
                catch (Exception exception)
                {
                    using (StreamWriter sw = File.CreateText(Dts.Variables["$Package::TempFolderPath"].Value.ToString() + "\\" +
                        "Error" + datetime + ".log"))
                    {
                        sw.WriteLine("Error Message: " + exception.Message.ToString());
                        Dts.TaskResult = (int)ScriptResults.Failure;
                    }
                }
            }
    
            public void ExportExcelFile(DataSet ds, string connstring, string SheetName)
            {
                OleDbConnection Excel_OLE_Con = new OleDbConnection();
                OleDbCommand Excel_OLE_Cmd = new OleDbCommand();
                //Get Header Columns
                string TableColumns = "";
                // Get the Column List from Data Table so can create Excel Sheet with Header
                foreach (DataTable table in ds.Tables)
                {
                    foreach (DataColumn column in table.Columns)
                    {
                        TableColumns += column + "],[";
                    }
                }
    
                // Replace most right comma from Columnlist
                TableColumns = ("[" + TableColumns.Replace(",", " Text,").TrimEnd(','));
                TableColumns = TableColumns.Remove(TableColumns.Length - 2);
    
                //Use OLE DB Connection and Create Excel Sheet
                Excel_OLE_Con.ConnectionString = connstring;
                Excel_OLE_Cmd.CommandTimeout = 0;
                Excel_OLE_Con.Open();
                Excel_OLE_Cmd.Connection = Excel_OLE_Con;
                Excel_OLE_Cmd.CommandText = "Create table [" + SheetName + "] (" + TableColumns + ")";
                Excel_OLE_Cmd.ExecuteNonQuery();
                Excel_OLE_Con.Close();
    
                //Write Data to Excel Sheet from DataTable dynamically
                foreach (DataTable table in ds.Tables)
                {
                    bool firstRow = true;
                    String sqlCommandInsert = "";
                    String sqlCommandValue = "";
                    foreach (DataColumn dataColumn in table.Columns)
                    {
                        sqlCommandValue += dataColumn + "],[";
                    }
    
                    sqlCommandValue = "[" + sqlCommandValue.TrimEnd(',');
                    sqlCommandValue = sqlCommandValue.Remove(sqlCommandValue.Length - 2);
                    sqlCommandInsert = "INSERT into [" + SheetName + "] (" + sqlCommandValue + ") VALUES(";
    
                    int columnCount = table.Columns.Count;
                    Excel_OLE_Con.Open();
                    Excel_OLE_Cmd.CommandTimeout = 0;
                    foreach (DataRow row in table.Rows)
                    {
                        string columnvalues = "";
                        for (int i = 0; i < columnCount; i++)
                        {
                            int index = table.Rows.IndexOf(row);
                            columnvalues += "'" + table.Rows[index].ItemArray[i].ToString() + "',";
                        }
    
                        columnvalues = columnvalues.TrimEnd(',');
                        var command = sqlCommandInsert + columnvalues + ")";
    
                        Excel_OLE_Cmd.CommandText = command;
                        Excel_OLE_Cmd.ExecuteNonQuery();
                    }
                    Excel_OLE_Con.Close();
    
    
                }
            }
    
            public void Error(string ActualSheetName)
            {
                //USE ADO.NET Connection from SSIS Package to get data from table            
                string con = Dts.Connections["oledb_conn"].ConnectionString.ToString();
                OleDbConnection _connection = new OleDbConnection(con);
                OleDbCommand cmd = new OleDbCommand();
                //drop Excel file if exists
    
                if (!string.IsNullOrEmpty(ActualSheetName))
                {
                    //FileType='PremiumFile'"
    
                    query = "Select ErrorQuery,SheetName,FileType from pol.ErrorMapping Where SheetName = '" + ActualSheetName + "' and FileType='PremiumFile'";
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = query;
                    cmd.CommandTimeout = 0;
                    cmd.Connection = _connection;
                    _connection.Open();
                    DataTable dt_ErrorInfo = new DataTable();
                    dt_ErrorInfo.Load(cmd.ExecuteReader());
                    //cmd.ExecuteNonQuery();
                    _connection.Close();
    
                    if (dt_ErrorInfo != null && dt_ErrorInfo.Rows.Count > 0)
                    {
                        foreach (DataRow dt_ErrorInfo_row in dt_ErrorInfo.Rows)
                        {
    
                            query = dt_ErrorInfo_row["ErrorQuery"].ToString();
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = query;
                            //cmd.CommandTimeout = 600;
                            cmd.Connection = _connection;
                            cmd.CommandTimeout = 0;
                            _connection.Open();
                            DataTable dt_Actual_data = new DataTable();
                            dt_Actual_data.Load(cmd.ExecuteReader());
                            //cmd.ExecuteNonQuery();
                            _connection.Close();
    
                            FileName = PackageName + dt_ErrorInfo_row["FileType"].ToString();
                            //TableName = "ErrorFileInfo ";
    
                            //Construct ConnectionString for Excel
                            connstring = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FolderPath + FileName + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
    
                            string SheetName = "";
                            object[] array = dt_ErrorInfo_row.ItemArray;
                            SheetName = array[1].ToString();
    
                            //Load Data into DataTable from SQL ServerTable
                            //string queryString = "SELECT * from " + TableName + " ";
                            //OleDbDataAdapter adapter = new OleDbDataAdapter(query, _connection);
                            DataSet ds = new DataSet();
                            ds.Tables.Add(dt_Actual_data);
                            //adapter.Fill(ds);
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                ExportExcelFile(ds, connstring, SheetName);
                                flag++;
                            }
                        }
                    }
    
    
                }
            }
    
    
    
    
            #region ScriptResults declaration
            /// <summary>
            /// This enum provides a convenient shorthand within the scope of this class for setting the
            /// result of the script.
            /// 
            /// This code was generated automatically.
            /// </summary>
            enum ScriptResults
            {
                Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
                Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
            };
            #endregion
        }
    }