unity2021版本Excel读取

废话不多说直接上读取代码,写入的大家就自己找一下 我这个工程没用

 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<int, List<string>> Data = new Dictionary<int, List<string>>();
  
     void LoadInfo(string path)
    {
    
    
      //  Debug.Log(path + "load");
     
        DataSet ds = ReadExcel(path);
       // Debug.Log(ds);
        int num = ds.Tables.Count;  //查询文件有几个表
      //  print("表" + num);
        int columns = ds.Tables[0].Columns.Count; //总列数,Tables[0]为待查询的表1
        int rows = ds.Tables[0].Rows.Count; //总行数
        print(rows + "---" + columns);
        for (int j = 0; j < rows; j++)
        {
    
    
            if (j>1)
            {
    
    
                Data.Add(j,new List<string>());
            }
       
            for (int i = 0; i < columns; i++)
            {
    
    
                //读取表1的第i行第j列
             
                if (ds.Tables[0].Rows[j][i].ToString()!=null&& ds.Tables[0].Rows[j][i].ToString() !="")
                {
    
    
                    string value = ds.Tables[0].Rows[j][i].ToString(); //ds.Tables[0].Rows[i][0]是Object,需强行转换为string
                   // string key = (j + 1) + "行" + (i + 1) + "列";
                
                    if (ds.Tables[0].Rows[j][i].ToString()!=null&& ds.Tables[0].Rows[j][i].ToString()!="")
                    {
    
    
                        if (j > 1 && i > 0)
                        {
    
    
                           
                            if (Data.ContainsKey(j))
                            {
    
    
                                Data[j].Add(value);
                            }
                        }
                        else if(j==0)
                        {
    
    
                            //Debug.Log(ds.Tables[0].Rows[j][i].ToString());
                            Task.Add(ds.Tables[0].Rows[j][i].ToString());
                        }
                    }
                    
                } 
            }
        }
    }
       public IEnumerator LoadExcel() {
    
     LoadInfo(Application.streamingAssetsPath + "/" + "fzsj.xlsx");yield return null; }

必须的三个dll链接:https://pan.baidu.com/s/1MtzlpAJXzzW7_4VRkRTUmw
提取码:zdb2
打包exe 会报错在编辑器版本中找到这个位置 低版本可能会不一样 但是大差不差,一定要是unityjit文件夹 。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43687127/article/details/125633917