Unity开发——读取Excel表格数据

在项目开发制作中经常回读取excel表格数据,其实有多种实现方式,FlexReader是比较方便快捷读取Excel文件内容,支持CSV,XLSX等格式。支持所以平台上运行。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using FlexFramework.Excel;
using UnityEngine.Networking;
using System.Linq;

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //开始读取表格数据
       // LoadExcel();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            LoadExcel();
            print("load");
            
        }
    }
 
    public delegate void DownloadHandler(byte[] bytes);
    //加载excel
    public  void LoadExcel()
    {
            // StreamingAssets
        
            StartCoroutine(LoadFileAsync("checkdata.xlsx", bytes =>
            {
                var book = new WorkBook(bytes);
                //Populate(book[0]);
                SetDataRow(book[0]);
                //this.modal.Show("Loaded XLSX!");
            }));
     }
   //读取列表和列表
    void SetDataRow(IEnumerable<Row> rows)
    {
        int index = -1;
      
        int count = rows.Count(r => !r.IsEmpty());
        if (count == 0)
            return;
        //将二维数字存到列表 ,通过行列读取 
        List<Row> rowData = new List<Row>(rows);
        for (int j = 1; j < rowData.Count; j++)//行
        {
            for (int i = 0; i < rowData[j].Count; i++)//列
            {
                Debug.Log(rowData[j][i].Text);
            }
        }
    }
    //异步加载
    private IEnumerator LoadFileAsync(string path, DownloadHandler handler)
     {
            // streaming assets should be loaded via web request
            // on WebGL/Android platforms, this folder is in a compressed directory
            var url = Path.Combine(Application.streamingAssetsPath, path);
            using (var req = UnityWebRequest.Get(url))
            {
                yield return req.SendWebRequest();
                var bytes = req.downloadHandler.data;
                handler(bytes);
            }
     }
}

插件:可以上Asset Store上直接搜FlexReader
下载地址:链接:https://pan.baidu.com/s/1mLTu2ymQY0Wmvpzb0DQ5iw 密码:j9m6

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值