使用C#在脚本任务SSIS中获取Excel工作表

问题描述

我是C#的新手。但是我正在研究SSIS C#脚本,该脚本必须处理文件列表并重命名每个文件的工作表。 我有一个Excel文件列表,它可以是.xls或.xlsx格式。我在SSIS脚本任务中编写一个c#脚本,以循环浏览文件名和工作表名称,并想用一些硬编码值重命名工作表名称,例如:文件夹具有 file1.xls:工作表1,工作表2,工作表3 ,file2.xls:工作表1,工作表2 ,file3.xls:sheet1,sheet2,sheet3,sheet4

因此,每当有一个名为sheet1的工作表名称可用时,应将其重命名为ABC,每有sheet2可用的表名称应为DEF,如果文件之一缺少sheet3,则应将其忽略。我写了一段代码,在那里我陷入了获取工作表名称的困境。如果有的话,请帮助我修复代码或提出更好的主意。

添加了这些命名空间

using System.IO;
using System.Data.OleDb;
using System.Data.sqlClient;

Added this in Main method

public void Main()
        {
            // Todo: Add your code here
            String FolderPath = Dts.Variables["User::SourceFolderPath"].Value.ToString();
            String fileFullPath = "";
            var directory = new DirectoryInfo(FolderPath);
            FileInfo[] files = directory.GetFiles();
            foreach (FileInfo file in files)
            {
                fileFullPath = FolderPath + "\\" + file.Name;

                //Create Excel Connection
                string ConStr;
                string HDR;
                HDR = "YES";
                ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileFullPath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
                string sheetname;
                sheetname = "";
                    
    



        #region ScriptResults declaration
                /// <summary>
                /// This enum provides a convenient shorthand within the scope of this class for setting the
                /// result of the script.
                /// 
                /// This code was generated automatically.
                /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.sqlServer.Dts.Runtime.DTSExecResult.Success,Failure = Microsoft.sqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }

非常感谢您。 最好的祝福 拉朱

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)