Unity数据读取一 读取Excel数据

下载需要的库:

链接:https://pan.baidu.com/s/1HWgb3zAVomW22zoy6bC07g 
提取码:49ev

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

下载解压放到Plugins文件夹下

//编辑excel表   表放到上面的百度云盘里了

Unity数据读取一 读取Excel数据_第1张图片

//加载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 data = new List();
        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表 和整体的工程文件包 已经整体放到百度云盘上了

后续:发布失败

GG了,发布出来exe读取不到excel表里的内容这里还有坑。

我查找了很多文件:

https://www.cnblogs.com/XRTSDUT2008/p/6964856.html

1.你的unity版本是多少,去对应的安装目录中取dll
2.System.Data.dll 在D:\Program Files\Unity2017.2\Editor\Data\Mono\lib\mono\2.0
3.I18N开头的dll 在 D:\Program Files\Unity2017.2\Editor\Data\Mono\lib\mono\unity

但是还是失败了,后来下载网上有几篇带源码直接运行项目中的exe发现还是GG。

临时解决转读csv去。

当前版本是2019.3  但是 有解决方法的大佬可以回复一下,在此拜谢!

 

你可能感兴趣的:(U3D,excel,unity)