无法设置活动的Excel工作表的C# [英] Unable to set active Excel sheet C#

查看:379
本文介绍了无法设置活动的Excel工作表的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行了一些C#代码在我的SSIS包运行的脚本组件的一部分。我试图打开一个Excel文件,并在我的SSIS包的下一步导入文件之前,改变工作表的名称。我在哪里,我想进行初始化oSheet线得到一个错误。



错误规定:错误编译一个动态表情要求1的一个或多个类型无法找到是否缺少参考C:?\Temp\Vsta \SSIS_ST110\VstaTP9LtckEMUWOXYp4Zy3YpQ\Vstau3xOw__Ey1kaOxXFoq0ff8g\ScriptMain.cs 107 26 ST_005c649f34584ed6873a7fde862ab2c9



我没有用C#一段时间,希望有人可以点我在正确的方向。 !在此先感谢



代码:

 公共无效的主要() 
{
的String =(字符串)Dts.Variables [文件路径]值。
字符串文件名= s.Substring(45,s.Length - 45); // 45 =文件名
MessageBox.Show(文件名)开始的知名指数硬编码值;
Excel.Application OXL;
Excel._Workbook OWB;
Excel._Worksheet oSheet;
Excel.Range ORNG;


{
OXL =新Microsoft.Office.Interop.Excel.Application();
oXL.Visible = FALSE;
OWB =(Excel.Workbook)oXL.Workbooks.Open(S);
oSheet =(Excel._Worksheet)oWB.ActiveSheet;
// oSheet =(Excel._Worksheet)oXL.ActiveSheet;
// oSheet =(Excel._Worksheet)oWB.Worksheets.Item(0);
// oSheet =(Excel._Worksheet)oXL.Worksheets [文件名]。
oSheet.Name =NLTWNH;
oWB.Close(S);

}
赶上(异常前)
{
//什么也不做
}

Dts.TaskResult =(INT )ScriptResults.Success;
}


解决方案

我缺少一个参考在我的SSIS脚本任务Microsoft.CSharp.dll。要添加在Visual Studio 2012中单击项目引用,添加引用,然后在框架标签滚动查找Miscrosoft.CSharp,选中对应的复选框,然后单击确定。


I am running some C# code as part of a script component running in my SSIS package. I am trying to open an Excel file and change the name of the sheet prior to importing the file in the next step of my SSIS package. I am getting an error on the line where I am trying to initialize "oSheet".

The error specifies: "Error 1 One or more types required to compile a dynamic expression cannot be found. Are you missing a reference? C:\Temp\Vsta\SSIS_ST110\VstaTP9LtckEMUWOXYp4Zy3YpQ\Vstau3xOw__Ey1kaOxXFoq0ff8g\ScriptMain.cs 107 26 ST_005c649f34584ed6873a7fde862ab2c9 "

I've not used C# for a while and was hoping someone could point me in the right direction. Thanks in advance!

Code:

        public void Main()
    {
        String s = (String)Dts.Variables["FilePath"].Value;
        String FileName = s.Substring(45,s.Length - 45); //45 = hardcoded value for known index of the start of the file name
        MessageBox.Show(FileName);
        Excel.Application oXL;
        Excel._Workbook oWB;
        Excel._Worksheet oSheet;
        Excel.Range oRng;

        try
        {
            oXL = new Microsoft.Office.Interop.Excel.Application();
            oXL.Visible = false;
            oWB = (Excel.Workbook)oXL.Workbooks.Open(s);
            oSheet = (Excel._Worksheet)oWB.ActiveSheet;
            //oSheet = (Excel._Worksheet)oXL.ActiveSheet;
            //oSheet = (Excel._Worksheet)oWB.Worksheets.Item(0);
            //oSheet = (Excel._Worksheet)oXL.Worksheets[FileName];
            oSheet.Name = "NLTWNH";
            oWB.Close(s);

        }
        catch (Exception ex)
        {
            //do nothing
        }

        Dts.TaskResult = (int)ScriptResults.Success;
    }      

解决方案

I was missing a reference to "Microsoft.CSharp.dll" in my SSIS script task. To add the reference in Visual Studio 2012 click Project, Add Reference, then in the Framework tab scroll to find Miscrosoft.CSharp, check the corresponding box, and click OK.

这篇关于无法设置活动的Excel工作表的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆