简体   繁体   English

在Selenium C#中从Excel读取数据

[英]Reading data from excel in selenium C#

I'm trying to read username and password from excel sheet while using Selenium in C# .NET. 我正在尝试在C#.NET中使用Selenium时从excel表中读取用户名和密码。 Below is the code: 下面是代码:

using excel = Microsoft.Office.Interop.Excel;

public void TestMethod1()
{
       excel.Application xlApp = new excel.Application();
       excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:\Test\TestData.xlsx");
       excel._Worksheet xlWorksheet = **xlWorkbook.Sheets[1];**
       excel.Range xlRange = xlWorksheet.UsedRange;
 }

I'm getting the following error at the text that is marked in bold in above code: 我在上面的代码中用粗体标记的文本出现以下错误:

Error CS0656 Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.Convert' Read Data From Excel_Office c:\\users\\tabish.khan\\documents\\visual studio 2015\\Projects\\Read Data From Excel_Office\\Read Data From Excel_Office\\OfficeReadExcel.cs 18 Active 错误CS0656缺少编译器所需的成员'Microsoft.CSharp.RuntimeBinder.Binder.Convert'从Excel_Office读取数据c:\\ users \\ tabish.khan \\ documents \\ visual studio 2015 \\ Projects \\从Excel_Office读取数据\\从Excel_Office \\ OfficeReadExcel读取数据。 CS 18有效

Please help me resolve this issue. 请帮助我解决此问题。

I have the same error with my C# Class Library project. 我的C#类库项目有相同的错误。 I'm using .NET Framework 4.6 and I used NuGet to install the Microsoft.Office.Interop.Excel assembly: 我正在使用.NET Framework 4.6,并且使用了NuGet来安装Microsoft.Office.Interop.Excel程序集:

PM> Install-Package Microsoft.Office.Interop.Excel

Here is a simplified version of the code which causes this error: 这是导致此错误的代码的简化版本:

using MSExcel = Microsoft.Office.Interop.Excel;

namespace ProjectReader
{
    public class ExcelExport
    {

        public ExcelExport()
        {
            xlApp = new MSExcel.Application();
            xlWorkBook = xlApp.Workbooks.Add(System.Reflection.Missing.Value);
        }

        public void CreateFile()
        {
            object missingValue = System.Reflection.Missing.Value;

            foreach (MSExcel.Worksheet sht in xlWorkBook.Sheets)
            {
                if (xlWorkBook.Worksheets.Count > 1)
                {
                    sht.Delete();
                }
            }

            **xlWorkSheet = (MSExcel.Worksheet)xlWorkBook.Sheets[1];**
        }

        private MSExcel.Application xlApp;
        private MSExcel.Workbook xlWorkBook;
        private MSExcel.Worksheet xlWorkSheet;
    }
}

Interestingly, I have no issues referencing the worksheets in the foreach loop just above the offending line. 有趣的是,我在问题行上方的foreach循环中引用工作表没有问题。

Use the following steps:- 使用以下步骤:

  1. Go to your project and right click. 转到您的项目,然后单击鼠标右键。
  2. Click on Add then click on Reference.. 单击添加,然后单击引用。
  3. Select Microsoft.CSharp and click on OK button then clean and rebuild your project. 选择Microsoft.CSharp ,然后单击“确定”按钮,然后清理并重建项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM