【问题标题】:Load Excel files with different format using SSIS使用 SSIS 加载不同格式的 Excel 文件
【发布时间】:2013-07-25 13:46:45
【问题描述】:

我们有一个问题,我们必须加载不同的 excel 文件模板,这里是模板

模板#1: 姓名地址城市邮编

模板#2: 姓名 城市 州 地址 电话号码

我们不知道我们可以得到什么格式,使用 SSIS 如何加载列名动态变化的 excel 文件?

提前致谢

【问题讨论】:

    标签: excel ssis


    【解决方案1】:

    如果您确定只会获得 2 种不同的模板类型,您可以按照以下方法:

    1. 在控制流中创建脚本任务。在引用中添加 Microsoft.Office.Interop.Excel 库。此任务将用于在开始实际 ETL 操作之前读取 Excel 文件。
    2. 创建一个新变量,例如 templateType。这将用于显示当前输入文件使用的模板。
    3. 创建 2 个数据流任务 - 为 2 个不同的模板中的每一个创建一个。
    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;
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      相关资源
      最近更新 更多