0

さまざまな Excel ファイル テンプレートをロードする必要があるという問題があります。テンプレートは次のとおりです。

テンプレート # 1 : 名前 住所 市区町村 郵便番号

テンプレート # 2 : 名前 市区町村 住所 電話番号

取得できる形式がわかりません.SSISを使用して、列名が動的に変化するExcelファイルをどのようにロードできますか?

前もって感謝します

4

1 に答える 1

0

取得するテンプレート タイプが 2 つだけであることが確実な場合は、次のアプローチに従うことができます。

  1. 制御フローでスクリプト タスクを作成します。Microsoft.Office.Interop.Excel ライブラリを参照に追加します。このタスクは、実際の ETL 操作を開始する前に Excel ファイルを読み取るために使用されます。
  2. templateType などの新しい変数を作成します。これは、現在の入力ファイルで使用されているテンプレートを表示するために使用されます。
  3. 2 つのデータ フロー タスクを作成します (2 つの異なるテンプレートのそれぞれに 1 つずつ)。
  4. スクリプト タスクの出力をデータ フローに接続します。各接続で、優先順位制約の評価タイプを式に設定し、式を @templateType==ValueToRepresentTemplateType に設定します。
  5. 以下のコードを参照して、スクリプト タスクを編集します。

        //using excel = Microsoft.Office.Interop.Excel;
    
        excel.Application xlapp = null;
        excel.Workbook xlbook = null;
        excel.Worksheet xlsheet = null;
    
        string src = @"filename.xls";
    
        try
        {
            xlapp = new excel.Application();
            xlbook = xlapp.Workbooks.Open(src, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            xlsheet = (excel.Worksheet) xlbook.Worksheets[0];
    
    
            //If fifth cell is empty, template has 4 columns i.e. first template
            excel.Range decider = (excel.Range)xlsheet.Cells[1, 5];
            if (string.IsNullOrEmpty(decider.Text.ToString()))
            {
                Dts.Variables["User::templateType"].Value = 1;//first template
            }
            else
            {
                Dts.Variables["User::templateType"].Value = 2;//second template
            }
    
    xlbook.Close(false);
    xlapp.Quit();
    
        }
        catch (Exception ex)
        {
        }
        finally
        {
            xlsheet = null;
            xlbook = null;
            xlapp = null;
        }
        Dts.TaskResult = (int)ScriptResults.Success;
    
于 2013-07-25T05:46:45.953 に答える