尝试显示Excel工作表名称时SSIS中的运行时错误

时间:2018-08-13 18:52:39

标签: excel ssis

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

有人可以检查吗?我试图用Google搜索,但是找不到适用于我的案例的解决方案...

enter image description here

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

这是我的代码:

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;
}

2 个答案:

答案 0 :(得分:0)

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

  1. 将ConnectionString更改为: ConnectionString =“ Provider = Microsoft.ACE.OLEDB.12.0; Data Source =” + excelFile +“;扩展属性= \” Excel 12.0; HDR = YES; IMEX = 0 \“”;

  2. 在很多地方,“ ConnectionString”被写为“ connectionString”,而C#似乎区分大小写?纠正它们后就可以了

谢谢大家!

答案 1 :(得分:-1)

请执行以下操作

ConnectionString =“ Provider = Microsoft.Jet.OLEDB.4.0; Data Source =” + 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);}

谢谢