我的脚本无法读取excel文件

时间:2018-01-31 09:52:15

标签: c# excel

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using Dapper;

    namespace SeleniumFramework
{
    class program
    {
        [STAThread]
        static void Main(string[] args)
        {
            //Read excel from User
            string excelpath = SampleTest.ReadExcel();
            //"C:\\Users\\hargovind.senapati\\Documents\\Visual Studio 2015\\Projects\\Selenium_Framework\\Sample Excel.xlsx";
            //get excel into a datatable
            if (excelpath != null)
            {
                DataTable dt = SampleTest.ExceltoDataTable(excelpath);
                //getting the url
                string url = dt.Rows[1][3].ToString();
                //getting the userid
                string uid = dt.Rows[2][3].ToString();
                //getting password
                string pwd = dt.Rows[3][3].ToString();
                //IWebDriver driver = new ChromeDriver();
                IWebDriver driver = new ChromeDriver(@"C:\Users\hargovind.senapati\Documents\Visual Studio 2015\Projects\Selenium_Framework\chromedriver.exe");
                driver.Navigate().GoToUrl(url);
                //get the page elements with input tag
                var username = driver.FindElement(By.Name("txtFLloginName"));
                username.SendKeys(uid);
                var password = driver.FindElement(By.Name("txtFLpassword"));
                password.SendKeys(pwd);
                //var InputFields = driver.FindElements(By.Name("txtFLloginName"));
                //var InputFields = driver.FindElements(By.Name("txtFLpassword"));
                //InputFields[0].SendKeys(uid);
                //InputFields[1].SendKeys(pwd);
                //get the page elements with button tag
                var btn = driver.FindElement(By.Name("imgbtnFLsubmit"));
                btn.Click();
            }
            else
            {
                Console.WriteLine("OOPS...!Error in reading the file...!");
            }
        }
    }
}

这是我的代码。上面的代码运行正常,直到它要求浏览选项,我选择我的Excel文件,但选择我的Excel文件后。脚本没有读取excel文件,也没有进一步调试。请帮助我是否需要修改此代码,或者我是否遗漏了某些内容。

下面是为浏览文件并将excel数据填入数据表而编写的代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NUnit.Framework;
using Dapper;

namespace SeleniumFramework
{
    class SampleTest
    {
        public static string ReadExcel()
        {
            OpenFileDialog browseFile = new OpenFileDialog();
            browseFile.DereferenceLinks = true;
            browseFile.Filter = "Excel|*.xls|Excel 2010|*.xlsx";
            //browseFile.Filter = "Link Files (*.lnk)|*.lnk";
            browseFile.Title = "Browse Excel file";
            if (browseFile.ShowDialog() == DialogResult.OK)
            {
                return browseFile.FileName;
            }
            else
            {
                return "";
            }
        }
        public static DataTable ExceltoDataTable(string fileName)
        {
            string ext = Path.GetExtension(fileName);
            string conn = string.Empty;
            DataTable dtexcel = new DataTable();
            if (ext.CompareTo(".xls") == 0)
                conn = @"Provider=Microsoft.jet.OLEDB.4.0;DATA Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"; //for below Excel 2007
            else
                conn = @"Provider=Microsoft.ACE.OLEDB.12.0;DATA Source=" + fileName + ";Extended Properties='Excel 12.0;HRD=NO';"; //for above Excel 2007
            using (OleDbConnection con = new OleDbConnection(conn))
            {
                try
                {
                    OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con); //it will read the data from sheet one
                    oleAdpt.Fill(dtexcel); //fill the excel data into dataTable
                    dtexcel.Rows.Remove(dtexcel.Rows[0]);

                }
                catch { }
            }
            return dtexcel;
        }
    }
}

0 个答案:

没有答案