Excel读取

需要引用 Excel.dll 、 ICSharpCode.SharpZipLib.dll、System.Data.dll(2018版本不用)

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Data;
using Excel;


public class Excel_show : MonoBehaviour
{
   
   
    //数据集合 key 为用户  值为数据集合
    public static Dictionary<string, List<string>> _dic = new Dictionary<string, List<string>>();

    public static List<string> _str = new List<string>();
   
    //excel 路径
    private static string path;
    
  
    //热源s1
    public static string path_s1;
    //热源s2
    public static string path_s2;

  
    void Start()
    {
        
      
    }


    /// <summary>   
    /// 只读Excel方法   
    /// </summary>   
    /// <param name="ExcelPath"></param>   
    public static   void GameReadExcel(string ExcelPath)
    {
         

        FileStream stream = File.Open(Application.streamingAssetsPath + "/" + ExcelPath, FileMode.Open, FileAccess.Read);

        
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();


        int columns = result.Tables[3].Columns.Count;//获取列数   

       // print(columns);
        int rows = result.Tables[3].Rows.Count;//获取行数   
        //print(rows);
        

        //从第二行开始读   
        for (int i = 1; i < rows; i++)
        {
            //print(result.Tables[3].Rows[i][0]);
            _dic.Add(result.Tables[3].Rows[i][0].ToString(), new List<string>());
            //用作更名
            _str.Add(result.Tables[3].Rows[i][0].ToString());
            for (int j = 0; j < columns; j++)
            {
                string nvalue = result.Tables[3].Rows[i][j].ToString();
                _dic[result.Tables[3].Rows[i][0].ToString()].Add(nvalue);
               // Debug.Log(nvalue);
            }
        }
        excelReader.Close();
    }
    //路径 热源  阀门开度  水泵频率
    public static void Set_Path(string reyuan,string str1,string str2 )
    {
        path_s1 = "相关数据/" + "热源S1  阀门+变频调节";
        path_s2 = "相关数据/" + "热源S2  阀门+变频调节";

        switch (reyuan)
        {
            case "热源s1":
                switch (str1)
                {
                    case "0%":
                        path = path_s1 +  "/热源S1 阀门开度" + str1 +"/"+ "热源S1 阀门开度" + str1+ ".xlsx";
                        break;
                    default:
                        path = path_s1  + "/热源S1 阀门开度" + str1 + "+频率" + str2+"/"+"热源S1 阀门开度" + str1 + "+频率" + str2 + ".xlsx";
                        break;
                }
                break;
            case "热源s2":
                switch (str1)
                {
                    case "0%":
                        path = path_s2+"/流量" + str1+"+扬程30" + "/标准工况(流量0%)"+ ".xlsx";
                        break;
                    default:
                        path = path_s2 + "/热源S2 阀门开度" + str1 + "+水泵频率" + str2 + "/" + "热源S2 阀门开度" + str1 + "+频率" + str2 + ".xlsx";
                        break;
                }
                break;
            default:
                break;
        }
       
        GameReadExcel(path);
    }
    
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值