使用SSIS导入EXCEL而不知道sheename

时间:2018-08-12 15:59:12

标签: excel ssis

我正在尝试使用SSIS从一个文件夹导入多个文件,但我不知道SheetName

因此,我正在根据下面的链接创建脚本任务,以获得SheetName,但是脚本任务出现错误“无法在变量声明中指定数组大小”

http://www.anupamanatarajan.com/2011/01/dynamic-sheet-name-in-ssis-excel.html

public void Main()
        {
            // TODO: Add your code here


            string excelFile = null;
            string connectionString = null;
            OleDbConnection excelConnection = null;
            DataTable tablesInFile = null;
            int tableCount = 0;
            DataRow tableInFile = null;
            string currentTable = null;
            int tableIndex = 0;
            string[] excelTables = null;



            excelFile = Dts.Variables["User::BBGFilePath"].Value.ToString();

            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";Extended Properties=Excel 8.0";

            excelConnection = new OleDbConnection(connectionString);

            excelConnection.Open();
            tablesInFile = excelConnection.GetSchema("Tables");

            tableCount = tablesInFile.Rows.Count;
            excelTables = new string[tableCount];

            foreach (DataRow tableInFile_loopVariable in tablesInFile.Rows)
            {
                tableInFile = tableInFile_loopVariable;
                currentTable = tableInFile["TABLE_NAME"].ToString();
                excelTables[tableIndex] = currentTable;
                tableIndex += 1;
            }
        }

            //Provide value to the shetename variable
            Dts.Variables["User::SheetName"].Value = excelTables[0];



            //Display file name
            string strMessage = Dts.Variables["User::BBGFilePath"].Value.ToString();
            MessageBox.Show(strMessage);


            Dts.TaskResult = (int)ScriptResults.Success;
        }

因此,我尝试将[User:SheetName]变量添加到Script task,但是它不起作用。

任何人都可以检查缺少的内容吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

正如我之前提到的,该错误确实表明您在类级别有一些无效的非声明语句。

脚本任务中的代码在右括号处存在一些问题-

public void Main()
        {
            // TODO: Add your code here

            string excelFile = null;
            string connectionString = null;
            OleDbConnection excelConnection = null;
            DataTable tablesInFile = null;
            int tableCount = 0;
            DataRow tableInFile = null;
            string currentTable = null;
            int tableIndex = 0;
            string[] excelTables = null;

            excelFile = Dts.Variables["User::BBGFilePath"].Value.ToString();

            //Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\CESLtd\ELKAY\Reports\Work2\Book1.xls; Extended Properties = "EXCEL 8.0;HDR=YES";
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";Extended Properties=Excel 8.0;HDR=YES";

            excelConnection = new OleDbConnection(connectionString);

            excelConnection.Open();
            tablesInFile = excelConnection.GetSchema("Tables");

            tableCount = tablesInFile.Rows.Count;
            excelTables = new string[tableCount];

            foreach (DataRow tableInFile_loopVariable in tablesInFile.Rows)
            {
                tableInFile = tableInFile_loopVariable;
                currentTable = tableInFile["TABLE_NAME"].ToString();
                excelTables[tableIndex] = currentTable;
                tableIndex += 1;
            }
        //} **commented this line now you are good to go**

            //Provide value to the shetename variable
            Dts.Variables["User::SheetName"].Value = excelTables[0];



            //Display file name
            string strMessage = Dts.Variables["User::BBGFilePath"].Value.ToString();
            MessageBox.Show(strMessage);

            Dts.TaskResult = (int)ScriptResults.Success;

        }