Unity數據讀取一 讀取Excel數據

下載需要的庫:

鏈接:https://pan.baidu.com/s/1HWgb3zAVomW22zoy6bC07g 
提取碼:49ev

//包含的需要的庫 案例的excel表 和整體的工程文件包

下載解壓放到Plugins文件夾下

//編輯excel表   表放到上面的百度雲盤裏了

//加載excel表數據

using UnityEngine;
using System.Collections;
using System.Data;
using System.IO;
using Excel;
using System.Collections.Generic;
using System;

/**藥瓶道具類*/
public class YaoPropData : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        LoadInfo("Assets/Resources/Prop.xlsx");
    }
    public struct TableData
    {
        public string propName;            //道具名稱
        public string propDesc;            //道具介紹
        public int  propMoney;             //道具價格
    }
    //找到並讀取Excel,轉換成DataSet型變量
    DataSet ReadExcel(string path)
    {
        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader iExcelDR = ExcelReaderFactory.CreateOpenXmlReader(fs);
        DataSet ds = iExcelDR.AsDataSet();
        return ds;
    }
    //加載記錄表中信息
    public void LoadInfo(string path)
    {

        List<TableData> data = new List<TableData>();
        DataSet ds = ReadExcel(path);
        int rows = ds.Tables[0].Rows.Count;         //總行數 Tables[0]爲待查詢的表1
        for (int i = 1; i < rows; i++)
        {
            TableData td = new TableData();
            td.propName = ds.Tables[0].Rows[i][0].ToString();
            td.propDesc = ds.Tables[0].Rows[i][1].ToString();
            td.propMoney = Convert.ToInt32(ds.Tables[0].Rows[i][2]);
            Debug.Log(td.propName + "  " + td.propDesc + "  " + td.propMoney);
            data.Add(td);
        }
    }
   
}

//案例的excel表 和整體的工程文件包 已經整體放到百度雲盤上了

這裏根據具體項目擴充一下

跳轉:https://www.jianshu.com/p/e1ec7c64f1ac

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章