【发布时间】:2013-07-31 08:38:38
【问题描述】:
我正在尝试使用 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' 在 ST_72bc640805c54a799cae807cc596a894.csproj.ScriptMain.Main()
警告:程序集绑定日志记录已关闭。启用程序集绑定 失败记录,设置注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 为 1。注意:有 是与装配绑定失败相关的一些性能损失 记录。要关闭此功能,请删除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。
--- 内部异常堆栈跟踪结束--- at System.RuntimeMethodHandle._InvokeMethodFast(对象目标,对象 [] 参数,SignatureStruct& sig,MethodAttributes 方法属性, RuntimeTypeHandle typeOwner) 在 System.Reflection.RuntimeMethodInfo.Invoke(对象 obj,BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化、 Boolean skipVisibilityChecks)在 System.Reflection.RuntimeMethodInfo.Invoke(对象 obj,BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化)
在 System.RuntimeType.InvokeMember(字符串名称,BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] 修饰符、CultureInfo 文化、String[] 命名参数)在 Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
我什至将此添加到系统中 http://msdn.microsoft.com/en-us/library/kh3965hw%28VS.80%29.aspx
【问题讨论】:
-
那台机器上安装了 MS Office 吗?引用的错误表明它找不到与 Excel 关联的 DLL。
-
不,我不知道,它的服务器不同