需要把这两个文件加入plguin 文件夹下, Excel.dll 和ICSharpCode.SharpZipLib,
官方链接 http://exceldatareader.codeplex.com/
读取文件
public class ExcelLoader
{
public static DataRowCollection ReadExcel(string path, string sheetName)
{
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
return result.Tables[sheetName].Rows;
}
}
解析文件
private List<AnimalInfo> SelectMenuTable(string path)
{
string sheetName = "sheet1";
DataRowCollection collect = ExcelLoader.ReadExcel(path, sheetName);
List<AnimalInfo> animalInfos = new List<AnimalInfo>();
_islandName = collect[0][0].ToString();
for (int i = 2; i < collect.Count; i++)
{
AnimalInfo info = new AnimalInfo
{
Id = int.Parse(collect[i][0].ToString()),
AnimalName = collect[i][1].ToString(),
Height = collect[i][2] + " " + collect[i][3],
HumanHeight = float.Parse(collect[i][4].ToString()),
AnimalHeight = float.Parse(collect[i][5].ToString()),
Weight = collect[i][6].ToString(),
WeightItem = collect[i][7].ToString(),
WeightItemCount = float.Parse(collect[i][8].ToString()),
WeightUnit = collect[i][9].ToString(),
FoodCategory = collect[i][10].ToString(),
ClassCategory = collect[i][11].ToString(),
};
animalInfos.Add(info);
}
return animalInfos;
}