【unity】读取Excel 文件

需要把这两个文件加入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;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值