using Excel;
using System.Collections.Generic;
using System.Data;
using System.IO;
using UnityEngine;
public class FileManager : MonoBehaviour
{
public static FileManager instance;
public List dataList = new List();//
public static bool isTestMode = false;
public static int idleTime = 120;
public static float autoScrollSpeed = 0.3f;
private IExcelDataReader excelReader;
public string value;
private void Awake()
{
instance = this;
ReadConfig();
AnalysisExcel();
}
#region 读excel
public void AnalysisExcel()
{
dataList.Clear();
// 项目根目录下的test.xlsx文件的路径:Application.dataPath + "/test.xlsx"
string path = Application.streamingAssetsPath + "/Excel.xlsx";
if (!File.Exists(path))
{
Debug.LogError("路径不存在: " + path);
return;
}
FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.Read);
IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
// 表格数据全部读取到result里
DataSet result = excelDataReader.AsDataSet();
// 获取表格有多少列
int columns = result.Tables[0].Columns.Count;
// 获取表格有多少行
int rows = result.Tables[0].Rows.Count;
// 根据行列依次打印表格中的每个数据
for (int i = 2; i < rows; i++)
{
HeadImgChildData model = new HeadImgChildData();
model.myIdx = i - 1;
for (int j = 0; j < columns; j++)
{
// 获取表格中指定行指定列的数据
value = result.Tables[0].Rows[i][j].ToString();
Debug.Log(i + "行" + j + "列:" + value);
if (j == 0)//企业名称
{
model.mName = value;
}
else if (j == 1)
{
model.mRawIamge = value;
}
else if (j == 2)
{
model.mRawIamge_1 = value;
}
else if (false)
{
return;
}
}
if (!dataList.Contains(model))
dataList.Add(model);
}
}
#endregion
private void ReadConfig()
{
string[] contents = File.ReadAllLines(Application.streamingAssetsPath + "/config.txt");
if (contents != null && contents.Length > 0)
{
for (int i = 0; i < contents.Length; i++)
{
if (contents[i] == null) continue;
string[] temps = contents[i].Split(':');
if (i == 0)
{
isTestMode = temps[1].Trim() == "1";
}
else if (i == 1)
{
idleTime = int.Parse(temps[1].Trim());
}
else if (i == 2)
{
autoScrollSpeed = float.Parse(temps[1].Trim());
}
}
}
}
List<HeadImgChildData> tempList = new List<HeadImgChildData>();
public List<HeadImgChildData> GetDataByName(string name)
{
tempList.Clear();
for (int i = 0; i < dataList.Count; i++)
{
if (dataList[i].mName == name)
tempList.Add(dataList[i]);
}
return tempList;
}
}