[英]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.