SSIS Excel导入(列名更改)

时间:2017-12-07 11:19:36

标签: ssis

我有一个松散类似于以下格式的Excel文件:

enter image description here

我将首先解释SSIS元素的下一步,因为列名不是“重要的”,因为我在数据流中取消数据流以开始使其可用:

enter image description here

问题是,文件将被更新 - 年份和季度将被删除(历史),添加新的文件以替换旧文件。这意味着,众所周知,数据流上的元数据已被破坏。

细胞范围和位置等将始终保持不变。

有没有办法在列名称(2016q1)流畅的数据流中处理?

由于

2 个答案:

答案 0 :(得分:1)

你会喜欢这个,因为它也是枢轴:

使用C#Script组件源:

添加命名空间: 使用System.Data.OleDb;

添加4个输出列并选择数据类型:

将代码添加到新行部分。

public override void CreateNewOutputRows()
    {
        /*
          Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
          For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
        */
        string fileName = @"C:\test.xlsx";
        string SheetName = "Sheet1";
        string cstr = "Provider.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";

        OleDbConnection xlConn = new OleDbConnection(cstr);
        xlConn.Open();

        OleDbCommand xlCmd = xlConn.CreateCommand();
        xlCmd.CommandText = "Select * from [" + SheetName + "]";
        xlCmd.CommandType = CommandType.Text;
        OleDbDataReader rdr = xlCmd.ExecuteReader();

        //int rowCt = 0; //Counter

        while (rdr.Read())
        {
            for (int i = 2; i < rdr.FieldCount; i++) //loop from 3 column to last
            {
                Output0Buffer.AddRow();
                Output0Buffer.ColA = rdr[0].ToString();
                Output0Buffer.ColB = rdr[1].ToString();
                Output0Buffer.FactName = rdr.GetName(i);
                Output0Buffer.FactValue = rdr.GetDouble(i);
            }


            //rowCt++; //increment counter
        }
        xlConn.Close();
    }

答案 1 :(得分:0)

如果列保持有序,那么您可以跳过标题行并选择第1行不包含标题。

相关问题