1

我需要帮助从 excel 表中读取所有行作为我的 selenium 测试用例中的测试数据。

我只能使用以下代码读取 Excel 工作表的一行。

//设置硒测试

 public void SetupTest()
    {
        selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.com/");
        selenium.Start();
        verificationErrors = new StringBuilder();
    }

//将我连接到我的 Excel 表 SampleTestData.xls

public void ConnectExcel()
{
 excel.Application excelApp = new excel.Application();
 excelApp.Visible = true;
 string ExcelDataPath = @"C:\SampleTestData.xls";
 excel.Workbook excelWorkBook = excelApp.Workbooks.Open(ExcelDataPath, 0, false, 5, "", "", false, excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
 excel.Sheets excelSheets = excelWorkBook.Worksheets;
 excel.Worksheet DataworkSheet = (excel.Worksheet)excelSheets.get_Item("Sheet1");
 System.String Search1 = ((excel.Range)DataworkSheet.get_Range("A1", Type.Missing)).Value2 as string;
 }

//selenium 代码运行测试

public void StartTest()
{
selenium.open("");
selenium.WaitForPageToLoad("100000");
selenium.Type("gbqfif", search1);
selenium.Click("btnG");
selenium.WaitForPageToLoad("100000");

}

此代码让我只选择包含 search1 值的一行。但是,我需要对其进行迭代,使其继续运行 StartTest() 方法,直到输入了 excel 表中的所有行(50)。

任何帮助,将不胜感激。

4

2 回答 2

0

这个答案可能对您有帮助,也可能对您没有帮助,但我将借此机会发布 2 个您可以使用的代码示例。

示例 #1

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            Excel.Range range ;

            string str;
            int rCnt = 0;
            int cCnt = 0;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;

            for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2 ;
                    MessageBox.Show(str);
                }
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 

    }
}

示例 #2

FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();

//5. Data Reader methods
while (excelReader.Read())
{
    //excelReader.GetInt32(0);
}

//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();

示例#3

using (FileStream fileStream = File.Open(inputFilenames[0], FileMode.Open, FileAccess.Read))
{
    IExcelDataReader excelReader;
    if (Path.GetExtension(inputFilenames[0]) == ".xls")
        excelReader = Factory.CreateReader(fileStream, ExcelFileType.Binary);
    else
        excelReader = Factory.CreateReader(fileStream, ExcelFileType.OpenXml);

    excelReader.NextResult();
    while (excelReader.Name != this.Worksheet)
        excelReader.NextResult();                

    while (excelReader.Read())
    {
        if (FirstRowHasColumnNames)
        {
            FirstRowHasColumnNames = false;
        }
        else
        {
            //do stuff
            var test = GetColumnData(excelReader, 1);
        }
    }

    this.Save(outputFilename);
}

您也可以查看此链接 Read from Excel File

于 2012-08-01T17:15:37.487 回答
0

尝试这个:-

        //Reading from a OpenXml Excel file (2007 format; *.xlsx)
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        // DataSet - Create column names from first row
        excelReader.IsFirstRowAsColumnNames = true;
        DataSet result = excelReader.AsDataSet();

        while (excelReader.Read())
        {    // Reading from row and get columns by index           
            var test = excelReader.GetString(1);

        }
于 2016-09-16T19:48:34.533 回答