这是我的excel助手类
join
这是我的主要课程,我在其中调用excel帮助函数
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EAAutoFramework.Helpers
{
public class ExcelHelpers
{
private static List<Datacollection> _dataCol = new
List<Datacollection();
// <summary>
// Storing all the excel values in to the in-memory collections
// </summary>
// <param name="fileName"></param>
public static void PopulateInCollection(string fileName)
{
DataTable table = ExcelToDataTable(fileName);
//Iterate through the rows and columns of the Table
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 0; col < table.Columns.Count; col++)
{
Datacollection dtTable = new Datacollection()
{
rowNumber = row,
colName = table.Columns[col].ColumnName,
colValue = table.Rows[row - 1][col].ToString()
};
//Add all the details for each row
_dataCol.Add(dtTable);
}
}
}
private static DataTable ExcelToDataTable(string fileName)
{
using (var stream = File.Open(fileName, FileMode.Open,
FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet(new
ExcelDataSetConfiguration()
{
ConfigureDataTable = (data) => new
ExcelDataTableConfiguration()
{
UseHeaderRow = true,
}
});
DataTableCollection table = result.Tables;
//Store it in DataTable
DataTable resultTable = table["Sheet1"];
//return
return resultTable;
}
}
}
public static string ReadData(int rowNumber, string columnName)
{
try
{
//Retriving Data using LINQ to reduce much of iterations
string data = (from colData in _dataCol
where colData.colName == columnName &&
colData.rowNumber == rowNumber
select colData.colValue).SingleOrDefault();
return data.ToString();
}
catch (Exception e)
{
return null;
}
}
}
public class Datacollection
{
public int rowNumber { get; set; }
public string colName { get; set; }
public string colValue { get; set; }
}
}
任何人都可以帮我解决这个问题。提前致谢