简体   繁体   English

如何通过Selenium读取一个Excel的数据文件,而C#的本机没有安装Interop和Microsoft Excel?

[英]How to read an Excel data file through Selenium without Interop and Microsoft Excel installed in the Local machine in C#?

I am trying to work on a solution to get data driven automation using Selenium C# binding in Microsoft Visual Studio.我正在尝试使用 Microsoft Visual Studio 中的 Selenium C# 绑定来获得数据驱动的自动化解决方案。 My challenge is to read from Excel without Excel application preferably using Closed XML or OLEDB connections.我的挑战是在没有 Excel 应用程序的情况下从 Excel 读取,最好使用 Closed XML 或 OLEDB 连接。 Any help or thought?有什么帮助或想法吗?

[21:28] Elizha, Sheeba [21:28] 以利沙,席巴

private DataSet GetExcelDataSet(string path) 
{ 
    string sheetName; 
    string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "; Jet OLEDB:Engine Type = 5; Extended Properties =\"Excel 8.0;\""; 
    DataSet ds = new DataSet(); 

    using (OleDbConnection con = new OleDbConnection(ConnectionString)) 
    { 
        using (OleDbCommand cmd = new OleDbCommand()) 
        { 
            using (OleDbDataAdapter oda = new OleDbDataAdapter()) 
            { 
                cmd.Connection = con; 
                con.Open(); 
 
                DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 

                for (int i = 0; i < dtExcelSchema.Rows.Count; i++) 
                { 
                    sheetName = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString();
                    DataTable dt = new DataTable(sheetName); 

                    cmd.Connection = con; 
                    cmd.CommandText = "SELECT * FROM [" + sheetName + "]"; 
                    oda.SelectCommand = cmd; 
                    oda.Fill(dt); 
                    dt.TableName = sheetName; 

                    ds.Tables.Add(dt); 
                } 
            } 
        } 
    } 

    return ds; 
}

Am getting data not in the format error.我得到的数据不是格式错误。

Consider using ExcelDataReader .考虑使用ExcelDataReader The following reads all WorkSheets and includes column headers which is optional.以下读取所有工作表并包括可选的列标题。

public static DataSet GetSheets(string excelFileName)
{
    using (var stream = File.Open(excelFileName, FileMode.Open, FileAccess.Read))
    {
        using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
            var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration()
            {
                UseColumnDataType = true,
                ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = true
                }
            });

            return dataSet;
        }
    }
}

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

相关问题 如何在不使用 Microsoft.Office.Interop.Excel 库的情况下在 C# 中读取 excel 文件 - How to read an excel file in C# without using Microsoft.Office.Interop.Excel libraries C#如何使用Interop从Excel读取和写入数据,而不显示Excel? - C# How to read and write data from Excel using Interop, without making Excel visible? 如何在没有安装MS Office的计算机上使用Microsoft.Office.Interop.Excel? - How to use Microsoft.Office.Interop.Excel on a machine without installed MS Office? 如何在不使用互操作的情况下通过c#取消隐藏excel表 - How to unhide an excel sheet through c# without using interop 如何使用 Interop 在 C# 中读取受保护视图的 Excel 文件? - How to read Excel file that is protected view in C# using Interop? C# (Microsoft.Office.Interop.Excel),将数据另存为 Excel 2003 文件(xls) - C# (Microsoft.Office.Interop.Excel), Save Data as Excel 2003 File(xls) 将Excel文件另存为只读(Interop.Excel)C# - Saving Excel File as Read Only (Interop.Excel) C# C#使用Microsoft.Office.Interop.Excel读取数据 - C# Read data using using Microsoft.Office.Interop.Excel 如何使用C#Excel Interop读取Excel列表元素(数据验证)? - How to read excel list elements (data validation) using C# Excel Interop? 如何使用 C# 中的 interop.excel 从 excel 文件中删除只读模式 - How to remove read-only mode from excel file using interop.excel in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM