一个Excel单元格中的字符串,读取并作为参数传递给方法

时间:2017-06-12 15:17:15

标签: c# excel

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace Something
{
[TestClass]
public class CBMain:Base
{
    [TestMethod]
    public void Main()
    {
        try
        {

            string ExcelFilePath = "D:\\Excel.xlsx";
            Excel.Application exlApp;
            Excel.Workbook exlWorkBook;
            Excel.Worksheet exlWorkSheet;
            Excel.Range exlRange;

            exlApp = new Excel.Application();
            exlWorkBook = exlApp.Workbooks.Open(@ExcelFilePath);
            exlWorkSheet = (Excel.Worksheet)exlWorkBook.Worksheets.get_Item(1);
            exlRange = exlWorkSheet.UsedRange;

            int exlRowCount = exlRange.Rows.Count;
            int exlColumnCount = exlRange.Columns.Count;

            int rcount = 1, ccount = 1;
            for (rcount = 3; rcount <= exlRowCount; rcount++)
            {

                string[] ExcelToMethod1 = (exlRange.Cells[rcount, (ccount + 8)] as Excel.Range).Value2;

                string[] ExcelToMethod1 = (exlRange.Cells[rcount, (ccount + 10)] as Excel.Range).Value2;
                Console.WriteLine(ExcelToMethod1 + " | " + ExcelToMethod1 );

                //**Calling method for creating program
                ClassName Program = new ClassName();
                Program.MethodCall(ExcelToMethod1 ,ExcelToMethod2);
            }
        }
        catch(Exception e)
        {
            Console.WriteLine(e);
        }
        //Console.WriteLine(exlRowCount);
        //Console.WriteLine(exlColumnCount);
    }
}
}

我想从Excel工作表中读取值并将它们作为参数传递给方法。 在Excel中,值将{"Var1","Var2","Var3"}存储在一个单元格中。因此,当我尝试将它们作为一个整个变量传递时,我收到以下错误

  

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:不能   将类型'string'隐式转换为'string []'   CallSite.Target(Closure,CallSite,Object)at   System.Dynamic.UpdateDelegates.UpdateAndExecute1 [T0,TRET](调用点   网站,T0 arg0)at   BrandmuscleAutomation.Tests.ChannelBuilder.MarketingFundProgram.CBMain.CBMainCall()   在   d:\ GitAutomation \ BrandmuscleAutomation \测试\ ChannelBuilder \ MarketingFundProgram \ Method.cs:行   42

Excel_Stored
enter image description here

0 个答案:

没有答案