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

编程入门 行业动态 更新时间:2024-10-27 01:28:10

在项目开发制作中经常回读取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/s/1mLTu2ymQY0Wmvpzb0DQ5iw 密码:j9m6

更多推荐

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

本文发布于:2023-06-14 01:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1420720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表格   数据   Unity   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!