根据条件动态地将数据加载到多个 excel 工作表中

Load data into multiple excel sheets depending on conditions dynamically

我有情况请帮帮我。我必须使用不同的查询在一个 excel 文件中创建多个 sheet。就像我必须检查特定列是否为 null 那么针对此查询的记录应该在新 sheet 中的 excel 文件中,如果它为 null 或为空,我必须检查具有其他名称的另一列,并且然后为它创建一个 sheet 并且只有当查询 returns 一些结果时才应该创建 sheet 否则不应该有任何空的 sheet。我有 8 个不同的列要检查。

例如,我必须执行以下将在源代码中的查询

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

样本结果是

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

这里的 Agency Name 是 NULL 像这样我将有源查询,其中 Effective 可以是 null。

我使用此代码片段创建了动态 Excel 文件表。

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
    }
}