Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C读取单个excel单元格(A3)#_C#_Ssis - Fatal编程技术网

C# 使用C读取单个excel单元格(A3)#

C# 使用C读取单个excel单元格(A3)#,c#,ssis,C#,Ssis,我试图使用C#读取单个excel单元格(A3),但由于下面提到的错误,此代码失败。我正在使用SSIS脚本任务。请帮帮我!!!谢谢 /* Microsoft SQL Server Integration Services Script Task Write scripts using Microsoft Visual C# 2008. The ScriptMain is the entry point class of the script. */ using System;

我试图使用C#读取单个excel单元格(A3),但由于下面提到的错误,此代码失败。我正在使用SSIS脚本任务。请帮帮我!!!谢谢

/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel; //use the reference in your code

namespace ST_72bc640805c54a799cae807cc596a894.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

        /*
        The execution engine calls this method when the task executes.
        To access the object model, use the Dts property. Connections, variables, events,
        and logging features are available as members of the Dts property as shown in the following examples.

        To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
        To post a log entry, call Dts.Log("This is my log text", 999, null);
        To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

        To use the connections collection use something like the following:
        ConnectionManager cm = Dts.Connections.Add("OLEDB");
        cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

        Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

        To open Help, press F1.
    */
        public void Main()
        {
            object _row = 3;
            object _column = 1;
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            excelApp.Visible = false;
            excelApp.ScreenUpdating = false;
            excelApp.DisplayAlerts = false;
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(@"C:\\ETL Process\\Sample.xlsx", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

             Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            string currentSheet = "Sheet1";
            Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);
            Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.UsedRange;
            string sValue = (range.Cells[_row, _column] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
            MessageBox.Show(sValue); 
            //sValue has your value
        }
    }
}
这里是错误

错误:System.Reflection.TargetInvocationException:异常已被删除 由调用的目标抛出。--> System.IO.FileNotFoundException:无法加载文件或程序集 'Microsoft.Office.Interop.Excel,版本=12.0.0.0,区域性=中性, PublicKeyToken=71e9bce111e9429c'或其依赖项之一。这个 系统找不到指定的文件。文件名: 'Microsoft.Office.Interop.Excel,版本=12.0.0.0,区域性=中性, PublicKeyToken=71e9bce111e9429c'at ST_72BC640805C5454A799CAE807CC596A894.csproj.ScriptMain.Main()

警告:程序集绑定日志记录已关闭。要启用程序集绑定 记录失败,请设置注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog](DWORD)到1。注:有 某些性能损失是否与程序集绑定失败相关 登录中。要关闭此功能,请删除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]

---内部异常堆栈跟踪的结束---at System.RuntimeMethodHandle.\u InvokeMethodFast(对象目标,对象[]) 参数、SignatureStruct&sig、MethodAttributes、MethodAttributes、, RuntimeTypeHandle类型所有者)位于 System.Reflection.RuntimeMethodInfo.Invoke(对象obj,BindingFlags invokeAttr,绑定器绑定器,对象[]参数,CultureInfo区域性, 布尔skipVisibilityChecks)在 System.Reflection.RuntimeMethodInfo.Invoke(对象obj,BindingFlags invokeAttr,Binder Binder,对象[]参数,CultureInfo区域性)
在System.RuntimeType.InvokeMember处(字符串名称,BindingFlags bindingFlags、绑定器绑定器、对象目标、对象[]提供的参数、, ParameterModifier[]修饰符,CultureInfo区域性,字符串[] 命名参数)在 Microsoft.SqlServer.Dts.Tasks.ScriptTask.vstatasksscriptingEngine.ExecuteScript()

我甚至把这个添加到了系统中

您的代码正在使用程序集Microsoft.Office.Interop.Excel。当您安装了MS Office时,您的本地计算机具有此功能

您现在已将代码部署到服务器。此服务器未安装Office。上述程序集不是核心.NET运行时的一部分。当您的包尝试运行时,它抛出
无法加载文件或程序集“Microsoft.Office.Interop.Excel”
,因为您没有提供它所需的资源

如果选择在服务器上安装Office,请意识到此方法会产生许可成本,并且此服务器可以使用的修补程序数量会增加。这可能会导致更长的停机/停机时间,这可能会引起关注,具体取决于您的SLA

如果您认为“这只是一个或两个DLL,我可以将它们从我的机器复制到服务器上”,即使您设法将它们全部复制并运行,您已经有效地安装了Office,并且如果您接受审核,上述许可将适用

否则,您唯一的其他方法就是重新编码您的包以不使用程序集


您是否在该机器上安装了MS Office?引用的错误表明它找不到与Excel关联的DLL。不,我找不到,是它的不同服务器