需要引用 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);
}
}