SSIS 脚本任务 C# 编码,以动态选择具有特定名称的最新 Excel 文件的第一个工作表

本文关键字:定名称 最新 Excel 第一个工作 文件 任务 脚本 编码 选择 动态 SSIS | 更新日期: 2023-09-27 18:36:36

我对C#编码很陌生。通过不断搜索,我能够获得以下代码来选择名称包含字符串"国家"的最新 Excel 文件。现在我只需要动态选择第一张纸,因为每次我得到一个文件时,工作表名称都会改变。我修改了如下代码:

public void Main()
    {
        // TODO: Add your code here
        var directory = new     DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());

        FileInfo[] files = directory.GetFiles();
        DateTime lastModified = DateTime.MinValue;
        foreach (FileInfo file in files)
        {
            Match m = Regex.Match(file.FullName, "Country");
            if (file.LastWriteTime > lastModified && m.Success)
            {
                lastModified = file.LastWriteTime;
                Dts.Variables["User::VarFileName"].Value = file.ToString();

                string filename = (string)Dts.Variables["User::VarFileName"].Value;
                string sheetName = null;
                string connStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='"EXCEL 12.0;HDR=YES;'"", filename);
                var conn = new OleDbConnection(connStr);
                try
                {
                    conn.Open();
                    using (var dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
                    {
                        var row0 = dtSheet.Rows[0];
                        sheetName = row0["TABLE_NAME"].ToString();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                }
                if (!String.IsNullOrEmpty(sheetName))
                {
                    bool dummy = true;
                    Dts.Variables["SheetName"].Value = sheetName;
                    Dts.Events.FireInformation(1, "User::SheetName", sheetName, "", 0, ref dummy);
                    Dts.TaskResult = (int)ScriptResults.Success;
                }
                else
                {
                    Dts.Events.FireError(0, "User::SheetName", "No SheetName found!", String.Empty, 0);
                    Dts.TaskResult = (int)ScriptResults.Failure;
                }

            }
        }
MessageBox.Show(Dts.Variables["User::VarFileName"].Value.ToString());
            MessageBox.Show(Dts.Variables["User::SheetName"].Value.ToString());
            Dts.TaskResult = (int)ScriptResults.Success;
        }

但是我收到以下错误:

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.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providerArgs, ParameterModifier[] 修饰符, CultureInfo culture, String[] namedParams)at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

SSIS 脚本任务 C# 编码,以动态选择具有特定名称的最新 Excel 文件的第一个工作表

假设您的文件路径正确并且您对该文件夹有足够的权限; 修改代码并在连接中添加完整路径而不是文件名

    string fullPath = file.FullName;
    string connStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
Extended Properties='"EXCEL 12.0;HDR=YES;'"", fullPath);

此外,请确保您提到的所有变量都是只读的,或者根据您在脚本任务中执行的活动进行读写。

错误

之一"Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()"是由文件夹的安全权限引起的。当您更改权限时,此错误将消失。我只记得.dll文件的位置。