unity读取Excel表格配置

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

使用Excel.dll

示例代码

代码如下(示例):

/// 
///  从excel配置表加载
/// 
///  要读取的excel文件路径 
/// 
public static GameInfo[] GetGlist(string path)
{
    // 行                列
    int columnNum = 0, rowNum = 0;

    FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);

    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

    DataSet result = excelReader.AsDataSet();

    //Tables[0] 下标0表示excel文件中第一张表的数据
    columnNum = result.Tables[0].Columns.Count;
    rowNum = result.Tables[0].Rows.Count;

    DataRowCollection collect = result.Tables[0].Rows;

    GameInfo[] excels = new GameInfo[rowNum - 1];

    // collect[i][0].ToString()   读取第I行第0个数据
    for (int i = 1; i < rowNum; i++)
    {
        GameInfo item = new GameInfo();
        item.ID = int.Parse(collect[i][0].ToString());
        item.Type = int.Parse(collect[i][1].ToString());
        item.GameName = collect[i][2].ToString();
        item.IsDoublePeople = int.Parse(collect[i][3].ToString());

        item.GameImagePath = collect[i][6].ToString();

        item.GameExePath = collect[i][5].ToString();

        item.IsContinue = int.Parse(collect[i][4].ToString());

        item.VideoPath = collect[i][7].ToString();
     //   item.GameText = collect[i][8].ToString();

        excels[i - 1] = item;
    }
    return excels;
}

你可能感兴趣的:(unity,c#)