unity 读取excel内容

一、目的

在这里插入图片描述
将表中的题目进行转换使用,最终还要发布成为webgl版本

二、使用步骤

1.引入库

添加Plugins文件:(PS:我忘记添加的是哪些了,貌似是Excel.dll,ICSharpCode.SharpZipLib.dll,System.Data.dll这些库)
链接:https://pan.baidu.com/s/1urZeVi5DUKpcKX_qduk6gQ
提取码:7wl4

在这里插入图片描述

2.转换数据

将excel表格转换为object
代码如下:

#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;

/// <summary>
/// 利用ScriptableObject创建资源文件
/// </summary>
public class BuildAsset : Editor
{

    [MenuItem("BuildAsset/Build Scriptable Asset")]
    public static void ExcuteBuild()
    {
        BookHolder holder = ScriptableObject.CreateInstance<BookHolder>();

        //查询excel表中数据,赋值给asset文件
        holder.menus = ExcelAccess.SelectMenuTable();

        string path = "Assets/Resources/KaoTi.asset";

        AssetDatabase.CreateAsset(holder, path);
        AssetDatabase.Refresh();

        Debug.Log("BuildAsset Success!");
    }
}

#endif

2.读入数据

数据进行转换以后,可以使用代码直接进行调取。

using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// 基于ScriptObject的BookHolder类
/// </summary>
public class BookHolder : ScriptableObject
{
    public List<Menu> menus;
}
/// <summary>
/// 菜单实体类
/// </summary>
[System.Serializable]
public class Menu
{
    public string m_Id;
    public string m_Projects;
    public string m_Mission;
    public string m_Types;
    public string m_TitleNum;
    public string m_Questions;
    public string m_Options;
    public string m_Answer;
}

读取数据:

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

public class ExcelAccess
{
    public static string Excel = "KaoTi";

    //查询menu表
    public static List<Menu> SelectMenuTable()
    {
        string excelName = Excel + ".xlsx";
        string sheetName = "sheet1";
        DataRowCollection collect = ExcelAccess.ReadExcel(excelName, sheetName);

        List<Menu> menuArray = new List<Menu>();
        for (int i = 1; i < collect.Count; i++)
        {
            if (collect[i][1].ToString() == "") continue;

            Menu menu = new Menu
            {
                m_Id = collect[i][0].ToString(),
                m_Projects = collect[i][1].ToString(),
                m_Mission = collect[i][2].ToString(),
                m_Types = collect[i][3].ToString(),
                m_TitleNum = collect[i][4].ToString(),
                m_Questions = collect[i][5].ToString(),
                m_Options = collect[i][6].ToString(),
                m_Answer = collect[i][7].ToString()
};
            menuArray.Add(menu);
        }
        return menuArray;
    }

    /// <summary>
    /// 读取 Excel ; 需要添加 Excel.dll; System.Data.dll;
    /// </summary>
    /// <param name="excelName">excel文件名</param>
    /// <param name="sheetName">sheet名称</param>
    /// <returns>DataRow的集合</returns>
    static DataRowCollection ReadExcel(string excelName, string sheetName)
    {
        string path = Application.dataPath + "/" + excelName;
        FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        DataSet result = excelReader.AsDataSet();
        //int columns = result.Tables[0].Columns.Count;
        //int rows = result.Tables[0].Rows.Count;

        //tables可以按照sheet名获取,也可以按照sheet索引获取
        //return result.Tables[0].Rows;
        return result.Tables[sheetName].Rows;
    }
}

调取数据:(此代码挂在项目场景中)

using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 读取KaoTi的scriptObject文件
/// 使用Resources直接读取
/// </summary>
public class ReadHolders : MonoBehaviour
{
    readonly string assetName = "KaoTi";

    void Awake()
    {
        if (DanLi.GetDL.ReadHolders == null)
        {
            DanLi.GetDL.ReadHolders = this;
        }
    }
    void Start()
    {
       
        BookHolder asset = Resources.Load<BookHolder>(assetName);
        foreach (Menu gd in asset.menus)
        {
            Debug.Log(gd.m_Id + "     " + gd.m_Projects + "  " + gd.m_Mission + "  " + gd.m_Types + "  " + gd.m_TitleNum + "  " + gd.m_Questions + "  " + gd.m_Options + "  " + gd.m_Answer + "\n");
        }

    }
}

总结

这是我目前使用的一个读取excel表格内容的办法,可能不是最方便的,但是对于目前的我来说,能用就行,(其实也是没有找到简单的);其次主要是为了自己以后可以方便使用,目前还处于使用阶段,里面的好多技术点还没有真正的了解,先会使用,然后再考虑创造吧
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity粉末状在校生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值