尝试显示excel sheetname时SSIS中出现运行时错误

尝试显示excel sheetname时SSIS中出现运行时错误,excel,ssis,Excel,Ssis,我尝试使用SSIS获取excel的工作表名称,因为工作表名称未知。但是我犯了一些错误 有人能查一下吗?我试图用谷歌搜索,但没有找到适用于我的案例的解决方案 这是我的密码: public void Main() { string excelFile = null; string connectionString = null; OleDbConnection excelConnection = null; DataTable tablesInFile = null

我尝试使用SSIS获取excel的工作表名称,因为工作表名称未知。但是我犯了一些错误

有人能查一下吗?我试图用谷歌搜索,但没有找到适用于我的案例的解决方案

这是我的密码:

public void Main() {
    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;
     }

     Dts.Variables["User::SheetName"].Value = excelTables[0];
     string strMessage = Dts.Variables["User::SheetName"].Value.ToString();
     MessageBox.Show(strMessage);
     Dts.TaskResult = (int)ScriptResults.Success;
}
请按以下步骤操作

ConnectionString=“Provider=Microsoft.Jet.OLEDB.4.0;数据源=“+excelFile+”;扩展属性=\”Excel 8.0;HDR=是;IMEX=1\“;”

显示图纸名称的步骤

Foreach(可执行文件中的变量项){ Dts.Variables[“User::SheetName”]。Value=item.tosing(); 字符串strMessage=Dts.Variables[“User::SheetName”].Value.ToString(); MessageBox.Show(strMessage);}


谢谢

在我更改以下内容后,它现在可以工作了:

  • 已将ConnectionString更改为: ConnectionString=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+excelFile+”;扩展属性=\”Excel 12.0;HDR=是;IMEX=0\“”

  • 有几个地方把“ConnectionString”写成“ConnectionString”,而C#似乎区分大小写?纠正它们之后,它就工作了


  • 谢谢大家

    如果这个答案出现错误,你为什么要接受它。请再说一遍:这发生在哪一行?您做了哪些更改,这是一个与上一行不同的错误。@Nick.McDermaid,因为这是一个新错误,我希望尊重上一个线程中的答案。@Abhishek,区别在于我添加到行下面以显示sheetname:string strMessage=Dts.Variables[“User::sheetname”].Value.ToString();MessageBox.Show(strMessage);非常感谢。我试过了,但还是出错了。现在,我添加了一个try-catch,可以看到错误是Systerm.InvalidOperationException:connectionString属性尚未初始化。我在我的线程中添加了错误截图,顺便说一句,我有office 2010 32位,我尝试打开的excel是.xlsx。这有什么区别吗?我尝试了“ConnectionString=”Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+excelFile+”;扩展属性=\”excel12.0;HDR=是;IMEX=1\“;”;”同样的错误
    public void Main() {
        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;
         }
    
         Dts.Variables["User::SheetName"].Value = excelTables[0];
         string strMessage = Dts.Variables["User::SheetName"].Value.ToString();
         MessageBox.Show(strMessage);
         Dts.TaskResult = (int)ScriptResults.Success;
    }