Unity读取Excel表

网上找了一篇很好的博客 并对其修正+优化了一下https://blog.csdn.net/wangjiangrong/article/details/79980447

不多介绍先直接上手吧

准备工作

首先下载Excel.dll ,ICSharpCode.SharpZioLib.dll ,System.Data.dll 这些类库下载下来 放在Unity目录下的Plugin目录下,没有的话就新建一个

下载连接:https://download.csdn.net/download/qq_36848370/11985271

接着找到需要使用的表

目的

准备工作做完之后,我们要看看我们根据这个表,要的结果是什么?我们会把表中的内容读取到一个对应的class实例(ItemManager)中,然后将这个实例生成一个Asset资源,解释一下 每次代码编译都会读取一下Excel表中的内容 从而实例出asset,读取这个肯定要比直接读取Excel快的多。

如图:

会发现这个Asset包含了表里的所有内容,然后我们就可以使用这个Asset来获取我们需要的数据。比如将这个Asset文件放在Resources文件夹下,用如下代码遍历:

  public GameObject A;  //这个A 是一个gameobject 可以随意修改  也可以Excel表中配置路径 自由发挥
    void Start()
    {
        ItemManager man = Resources.Load<ItemManager>("DataAssets/Unity_list");
        foreach (Item i in man.dataArray)
        {
            Debug.Log(i.itemName + "---" + i.itemPos);
            GameObject go = GameObject.Instantiate(A, i.itemPos, Quaternion.identity);
            go.name = i.itemName;
            go.transform.eulerAngles = i.itemPos;
        }
    }

打出的Log如下: 

当然我们也可以将这个Asset打成一个ab包来读取。

知识点:
1.ScriptableObject:我们要将一个class的实例生成一个UnityEngine.Object文件,然后将这个Object生成为Asset文件,我们的class需要继承ScriptableObject。

2.Serializable:可以序列化一个类,使这个被序列化的对象在Inspector面板上显示, 并可以赋予相应的值

3.读取excel的操作和打包AB一样,不能在程序运行时执行,代码要放在Editor文件夹下。 

定义类

我们首先需要定义两个类,第一个类用来存放每行的表数据(Item)该类需要添加Serializable字段,才能在Asset中显示数据。

第二个类用来存放前者的数组即所有的表数据(ItemManager)。该类即要生成Object的类,需要继承ScriptableObject

新建工具类 读取表内容  当然可以设置第几张表

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

namespace Data
{
    [System.Serializable]
    public class Item
    {
        public string itemName;
        public Vector3 itemPos;
    }
    public class ItemManager : ScriptableObject
    {
        public Item[] dataArray;
    }
}

namespace EditorTool
{
    public class ExcelConfig
    {
        /// <summary>
        /// 存放excel表文件夹的的路径,本例xecel表放在了"Assets/Excels/"当中
        /// </summary>
        public static readonly string excelsFolderPath = Application.dataPath + "/Excels/";
        /// <summary>
        /// 存放Excel转化CS文件的文件夹路径
        /// </summary>
        public static readonly string assetPath = "Assets/Resources/DataAssets/";
    }
    public class ExcelTool
    {
        /// <summary>
        /// 读取表数据,生成对应的数组
        /// </summary>
        /// <param name="filePath">excel文件全路径</param>
        /// <returns>Item数组</returns>
        public static Item[] CreateItemArrayWithExcel(string filePath)
        {
            //获得表数据
            int columnNum = 0, rowNum = 0;
            DataRowCollection collect = ReadExcel(filePath, ref columnNum, ref rowNum);
            //根据excel的定义,第三行开始才是数据
            Item[] array = new Item[rowNum - 2];
            for (int i = 2; i < rowNum; i++)
            {
                Item item = new Item();
                //解析每列的数据
                item.itemName = collect[i][0].ToString();
                item.itemPos = new Vector3(float.Parse(collect[i][1].ToString()), float.Parse(collect[i][2].ToString()), float.Parse(collect[i][3].ToString()));
                array[i - 2] = item;
            }
            return array;
        }
        /// <summary>
        /// 读取excel文件内容
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="columnNum">行数</param>
        /// <param name="rowNum">列数</param>
        /// <returns></returns>
        static DataRowCollection ReadExcel(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();
            //Tables[0] 下标0表示excel文件中第一张表的数据
            columnNum = result.Tables[0].Columns.Count;
            rowNum = result.Tables[0].Rows.Count;
            return result.Tables[0].Rows;
        }
    }
}

我们将拿到的数据存放在ItemManager当中,并生成一个Asset: 

using System.IO;
using Data;
using EditorTool;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

public class ExcelBuild : Editor
{
    [MenuItem("CustomEditor/CreateItemAsset")]
    [DidReloadScripts]// 现代代码自动编译会执行此方法
    public static void CreateItemAsset()
    {
        ItemManager manager = ScriptableObject.CreateInstance<ItemManager>();
        //赋值
        manager.dataArray = ExcelTool.CreateItemArrayWithExcel(ExcelConfig.excelsFolderPath + "Unity_list.xlsx");

        //确保文件夹存在
        if (!Directory.Exists(ExcelConfig.assetPath))
        {
            Directory.CreateDirectory(ExcelConfig.assetPath);
        }

        //asset文件的路径 要以"Assets/..."开始,否则CreateAsset会报错
        string assetPath = string.Format("{0}{1}.asset", ExcelConfig.assetPath, "Unity_list");
        //生成一个Asset文件
        AssetDatabase.CreateAsset(manager, assetPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
}

这样我们就大功告成了!!!在Unity菜单项,选择我们自定义的菜单CustomEditor->CreateItemAsset(目前的asset 每次代码编译都会重新生成一遍)。即可生成我们需要的Asset。

附加


备注:上面这种方法,我们自己手动根据对应的表写对应的类,然后针对性解析。但是如果我们有几十个表,那么就会显得很愚蠢。所以有个优化方案就是,我们在表中存好字段名称和字段类型,然后遍历所有的表读取这两个信息,用代码自动生成我们需要的.cs文件。当类都生成好后,再去遍历表解析数据,利用反射存入对应的类中,然后生成对应的Asset。 

注意:在AssetDatabase.CreateAsset()中,我们要传的路径应该是"Assets/"开头的Unity路径,而不是Windows中的全路径,类似于"E://..."。否则会报如下错误:Couldn't create asset file!

注意:在ScriptableObject类中,基本数据类型以外的成员类型需要加 SerializeField 关键字,自定义数据类型被ScriptableObject对象使用的时候,该类需要加 Serializable 关键字。如果没有加的话生成的Asset资源Inspector窗口就无法看见对应数据。

补充:

System.Data.dll如果冲突的话,删除即可。

经测试发现,分以下两种情况:

1. 如果xlsx文件的后缀为.xlsx,读取的代码应该为

IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);


若使用CreateBinaryReader读取,则在excelReader.AsDataSet();会报错NullReferenceException: Object reference not set to an instance of an object

2.如果xlsx文件的后缀为.xls,读取的代码应该为

IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);


若使用CreateOpenXmlReader读取,则在CreateOpenXmlReader处会报错ArgumentNullException: Value cannot be null.

  原博客连接 https://blog.csdn.net/wangjiangrong/article/details/79980447 

猜你喜欢

转载自blog.csdn.net/qq_36848370/article/details/103143888