Unity3D开发之读取excel数据转换到实例类中

也不知道为啥CSDN的平台这么有意思,下点dll都需要币。哭笑脸。下面是excel数据转成实体类的代码。

excel有两种格式。.xlsx和xls。

当格式为xlsx时,读取的代码为:

IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

 当格式为xls时,读取代码为:

IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

下面对应位置的代码自己替换。 

public class ReadExcel : MonoBehaviour {

	// Use this for initialization
	void Start ()
    {
        string url = Application.dataPath + "/ExcelToObject/Test.xlsx";

        int columnNum = 0, rowNum = 0;
        DataRowCollection collect = Read(url, ref columnNum, ref rowNum);
        List<Item> ItemList=new List<Item>();
        for (int i = 1; i < rowNum; i++)
        {
            Item item = new Item();
            //解析每列的数据
            item.Name = collect[i][0].ToString();
            item.Length = collect[i][1].ToString();
            item.Width = collect[i][2].ToString();
            ItemList.Add(item);
        }

        Debug.Log(ItemList.Find(x=>x.Name.Equals("给对方")).Length);
    }
    DataRowCollection Read(string filePath, ref int columnNum, ref int rowNum)
    {
        FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        DataSet result = excelReader.AsDataSet();
        columnNum = result.Tables[0].Columns.Count;
        rowNum = result.Tables[0].Rows.Count;
        return result.Tables[0].Rows;
    }

    public class  Item
    {
        public string Name;

        public string Length;

        public string Width;

        public string Height;

        public string Id;
    }
}

工程地址 ,里面dll随便拿。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值