Unity(C#)Excel 操作

Excel

  • 读取
	private void GetExcelData()
    {
    
    
        DataRowCollection collect = ReadExcel("D/user.xlsx", "userSheet");
        for (int i = 0; i < collect.Count; i++) {
    
    
            Debug.Log(collect[i]);
        }
    }

    /// <summary>
    /// 读取 Excel ; 需要添加 Excel.dll; System.Data.dll;
    /// </summary>
    /// <param name="excelPath">excel路径</param>
    /// <param name="sheetName">sheet名称</param>
    private DataRowCollection ReadExcel(string excelPath, string sheetName)
    {
    
    
        FileStream stream = File.Open(excelPath, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        //tables可以按照sheet名获取,也可以按照sheet索引获取
        return result.Tables[sheetName].Rows;
    }

猜你喜欢

转载自blog.csdn.net/a0_67/article/details/117150650