github地址 https://github.com/exceldatareader/exceldatareader
1.在NuGet中输入excel搜索,点击安装这两个如图
2.新建一个类,写入代码
class ExcelReader
{
public List<Dictionary<string, Object>> getData(string filePath)
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
// Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
// - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var configuration = new ExcelDataSetConfiguration { ConfigureDataTable = tableReader => new ExcelDataTableConfiguration { UseHeaderRow = true } };
DataSet result = reader.AsDataSet(configuration);
for (int i = 0; i < result.Tables[0].Rows.Count; i++)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
for (int j = 0; j < result.Tables[0].Columns.Count; j++)
{
dict.Add(result.Tables[0].Columns[j].ColumnName, result.Tables[0].Rows[i][j]);
}
list.Add(dict);
}
}
}
return list;
}
}