Unity创建和读取excel表格

之前分享过一篇word文档的创建和修改,那么在实际开发过程中有时候会遇到读取大量的数据(姓名、年级、性别等等),以下分享一下Unity创建和读取excel表格。

一、导入NPOI类库文件

这里用的版本是Unity 2018.4.2,可直接下载类库文件导入Unity,,如果是较低版本,可以看下我之前Unity与word文档交互的博客,对于稍低版本有具体的处理方法。(提取码:kkkk)
https://pan.baidu.com/s/1fE9hmm62ms_KgPj5HffEVA
这里类库文件需要放入Plugins文件夹内。
在这里插入图片描述

二、创建Excel表格

命名空间引入(在最后面会贴上完整代码)

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

开始创建Excel表格

    private string filePath;
    private HSSFWorkbook wk;
    private FileStream fs;          //文件流

    private ISheet sheet;           //工作表
    private IRow row;               //行
    private ICell cell;             //列

    private void Awake()
    {
    
    
        filePath = "C:/david.xls";
    }

    private void Start()
    {
    
    
        CreateExcel();
    }

    /// <summary>
    /// 创建表格
    /// </summary>
    private void CreateExcel()
    {
    
    
        Debug.Log(1);
        wk = new HSSFWorkbook();
        sheet = wk.CreateSheet("mySheet");
        Debug.Log(2);

        for (int i = 0; i <= 20; i++)
        {
    
    
            row = sheet.CreateRow(i);
            cell = row.CreateCell(0);
            cell.SetCellValue(i);
        }

        fs = File.Create(filePath);
        wk.Write(fs);
        fs.Close();
        fs.Dispose();
        Debug.Log("创建表格成功");
    }

路径自定义即可,进入Unity随便挂在到任一游戏物体上,运行即可在指定路径生成excel,可以看到生成了mysheet工作表,其中也有了数据。
在这里插入图片描述

三、读取Excel表格

先创建好要读取的excel表格,这里还是上面的C盘路径,以游戏物体及属性为例。
在这里插入图片描述
通过以下方法,遍历工作表–行--列

    /// <summary>
    /// 读取表格
    /// </summary>
    private void LoadExcel()
    {
    
    
        fs = File.OpenRead(filePath);
        wk = new HSSFWorkbook(fs);
        sheet = wk.GetSheetAt(0);
        for (int j = 1; j <= sheet.LastRowNum; j++)
        {
    
    
            row = sheet.GetRow(j);
            if (row != null)
            {
    
    
                for (int k = 0; k < row.LastCellNum; k++)
                {
    
    
                    Debug.Log(row.GetCell(k).ToString());
                }
            }
        }
    }

效果如下:(这里需要注意以下,无论是创建/读取,都要把excel关闭,在打开状态下Unity会报错)
在这里插入图片描述
接下来就可以进行下一步的解析了,我们需要把这些数据进行整理和分类,先把和表格对应的字段弄出来,用results列表存储获取的所有数据,itemInfos列表获取到所有物品。

    /// <summary>
    /// 接收结果
    /// </summary>
    public List<string> results = new List<string>();

    /// <summary>
    /// 物品信息
    /// </summary>
    public List<ItemInfo> itemInfos = new List<ItemInfo>();

    public struct ItemInfo
    {
    
    
        public string name;
        public int attack;
        public int crit;
        public int panetrate;
        public int hp;
        public int mp;
    }

在获取到具体列的元素时存入result列表内

    /// <summary>
    /// 读取表格
    /// </summary>
    private void LoadExcel()
    {
    
    
        fs = File.OpenRead(filePath);
        wk = new HSSFWorkbook(fs);
        sheet = wk.GetSheetAt(0);
        for (int j = 1; j <= sheet.LastRowNum; j++)
        {
    
    
            row = sheet.GetRow(j);
            if (row != null)
            {
    
    
                for (int k = 0; k < row.LastCellNum; k++)
                {
    
    
                    //Debug.Log(row.GetCell(k).ToString());
                    results.Add(row.GetCell(k).ToString());
                }
            }
        }
    }

遍历results,将具体的Item物体进行赋值操作。

    /// <summary>
    /// 获取所有数据
    /// </summary>
    private void GetAllDatas()
    {
    
    
        for (int i = 0; i < results.Count; i += 6)
        {
    
    
            ItemInfo item;
            item.name = results[i];
            item.attack = int.Parse(results[i+1]);
            item.crit = int.Parse(results[i + 2]);
            item.panetrate = int.Parse(results[i + 3]);
            item.hp = int.Parse(results[i + 4]);
            item.mp = int.Parse(results[i + 5]);
            itemInfos.Add(item);
        }

        for (int i = 0; i < itemInfos.Count; i++)
        {
    
    
            Debug.Log(string.Format("物品名称:{0} 攻击力:{1} 暴击:{2} 穿透:{3} 血:{4} 蓝:{5}", 
                itemInfos[i].name, itemInfos[i].attack, itemInfos[i].crit, 
                itemInfos[i].panetrate, itemInfos[i].hp, itemInfos[i].mp));
        }
    }

最后结果如下图。
在这里插入图片描述
完整代码:

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

/// <summary>
/// 读取表格
/// </summary>
public class LoadTable : MonoBehaviour
{
    
    
    private string filePath;
    private HSSFWorkbook wk;
    private FileStream fs;          //文件流

    private ISheet sheet;           //工作表
    private IRow row;               //行
    private ICell cell;             //列

    private void Awake()
    {
    
    
        filePath = "C:/david.xls";
    }

    private void Start()
    {
    
    
        LoadExcel();
        GetAllDatas();
    }

    /// <summary>
    /// 创建表格
    /// </summary>
    private void CreateExcel()
    {
    
    
        Debug.Log(1);
        wk = new HSSFWorkbook();
        sheet = wk.CreateSheet("mySheet");
        Debug.Log(2);

        for (int i = 0; i <= 20; i++)
        {
    
    
            row = sheet.CreateRow(i);
            cell = row.CreateCell(0);
            cell.SetCellValue(i);
        }

        fs = File.Create(filePath);
        wk.Write(fs);
        fs.Close();
        fs.Dispose();
        Debug.Log("创建表格成功");
    }

    /// <summary>
    /// 读取表格
    /// </summary>
    private void LoadExcel()
    {
    
    
        fs = File.OpenRead(filePath);
        wk = new HSSFWorkbook(fs);
        sheet = wk.GetSheetAt(0);
        for (int j = 1; j <= sheet.LastRowNum; j++)
        {
    
    
            row = sheet.GetRow(j);
            if (row != null)
            {
    
    
                for (int k = 0; k < row.LastCellNum; k++)
                {
    
    
                    //Debug.Log(row.GetCell(k).ToString());
                    results.Add(row.GetCell(k).ToString());
                }
            }
        }
    }

    /// <summary>
    /// 获取所有数据
    /// </summary>
    private void GetAllDatas()
    {
    
    
        for (int i = 0; i < results.Count; i += 6)
        {
    
    
            ItemInfo item;
            item.name = results[i];
            item.attack = int.Parse(results[i+1]);
            item.crit = int.Parse(results[i + 2]);
            item.panetrate = int.Parse(results[i + 3]);
            item.hp = int.Parse(results[i + 4]);
            item.mp = int.Parse(results[i + 5]);
            itemInfos.Add(item);
        }

        for (int i = 0; i < itemInfos.Count; i++)
        {
    
    
            Debug.Log(string.Format("物品名称:{0} 攻击力:{1} 暴击:{2} 穿透:{3} 血:{4} 蓝:{5}", 
                itemInfos[i].name, itemInfos[i].attack, itemInfos[i].crit, 
                itemInfos[i].panetrate, itemInfos[i].hp, itemInfos[i].mp));
        }
    }

    /// <summary>
    /// 接收结果
    /// </summary>
    public List<string> results = new List<string>();

    /// <summary>
    /// 物品信息
    /// </summary>
    public List<ItemInfo> itemInfos = new List<ItemInfo>();

    public struct ItemInfo
    {
    
    
        public string name;
        public int attack;
        public int crit;
        public int panetrate;
        public int hp;
        public int mp;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38484443/article/details/104988564