• Unity读取Excel表格


    一、将读取Excel文件放到Plugins文件下,并添加组件

    二、相关代码

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Data;
    using System.IO;
    using Excel;
    using UnityEngine.UI;
    using Object = UnityEngine.Object;
    
    public class finsh : MonoBehaviour
    {
         //public设置为unity界面配置
        public RectTransform content;
        public GameObject table;
        public GameObject tableDemo;
        private string file1 = "Assets/finsh/cc.xlsx";//读取的文件的路径
    
      
          //根据读取Excel表格的数量通过克隆创建对应的表格
        public void CloneForm()
        {
            Dictionary<string, string> dictionary = LoadInfo(file1);
            foreach (var i in dictionary)
            {
                content = table.GetComponent<RectTransform>();
                var task = Instantiate(tableDemo, content);
                task.gameObject.SetActive(true);
                task.GetComponent<InputField>().text = i.Value;
            }
        }
        
         //读取加载
        public void read()
        {
            GameObject.Find("table").AddComponent<finsh>().LoadInfo(file1);
            print(file1);
            CloneForm();
        }
    
        //找到并读取Excel,转换成DataSet型变量
        DataSet ReadExcel(string path)
        {
            Debug.Log(path + "read");
            FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            IExcelDataReader iExcelDR = ExcelReaderFactory.CreateOpenXmlReader(fs);
            DataSet ds = iExcelDR.AsDataSet();
            fs.Close();
            return ds;
        }
    
        //加载记录表中信息
        public Dictionary<string, string> LoadInfo(string path)
        {
            Debug.Log(path+"load");
            Dictionary<string, string> table_list=new Dictionary<string, string>();
           DataSet ds = ReadExcel(path);
            int num= ds.Tables.Count;  //查询文件有几个表
            print(""+num);
            int columns = ds.Tables[1].Columns.Count; //总列数,Tables[0]为待查询的表1
            int rows = ds.Tables[1].Rows.Count; //总行数
            GetComponent<GridLayoutGroup>().constraintCount = columns;
            print(rows + "---" + columns);
            for (int j = 0; j < rows; j++)
            {
                for (int i = 0; i < columns; i++)
                {
                    //读取表1的第i行第j列
                    string value = ds.Tables[1].Rows[j][i].ToString(); //ds.Tables[0].Rows[i][0]是Object,需强行转换为string
                    string key = (j + 1) + "" + (i + 1) + "";
                    table_list.Add(key,value);
                   // Debug.Log(key + "===" + value);
    
                }
    
               
            }
    
            return table_list;
        }
        
    }
    
    /*创建对象
     private Object CreateSprite(int r, int c)
    {
        GameObject go=new GameObject(r.ToString()+c.ToString());
        go.AddComponent<InputField>();
        go.transform.SetParent(this.transform,false);
        return go;
        print(this.transform.name);
    }*/
  • 相关阅读:
    js 脚本学习 索引
    nodejs 学习索引
    oracle 学习 笔记
    githut 的 管理 使用
    sublime text 插件记录
    web 学习 相关索引
    wpf 自定义 无边框 窗体 resize 实现
    vs 效率工具
    ANDROID开发实用小工具
    iOS开发之Core Animation
  • 原文地址:https://www.cnblogs.com/LiuQizhong/p/12157190.html
Copyright © 2020-2023  润新知