读取Excel配置文件

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;
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值