Unity读取Excel表和修改表数据

 库文件不多说

表结构:

Unity读取Excel表和修改表数据_第1张图片

读取代码:

 DataRowCollection _dataRowCollection = 
ReadExcel(Application.streamingAssetsPath + "/系统配置文件.xlsx");


 Config.speed =float.Parse(_dataRowCollection[1][1].ToString());
      
 Config.picSize = float.Parse(_dataRowCollection[2][1].ToString());
  
 Config.picDictance = float.Parse(_dataRowCollection[3][1].ToString());



  private DataRowCollection ReadExcel(string _path, int _sheetIndex = 0)
    {
        FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();
        return result.Tables[_sheetIndex].Rows;
    }

修改表:

public  static void   UpdateExcel(int c,int l,int id)
    {
        string path = Application.streamingAssetsPath + "/系统配置文件.xlsx";
        FileInfo _excelName = new FileInfo(path);
        using (ExcelPackage package = new ExcelPackage(_excelName))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets["详情"];
            //修改某一行的数据
            worksheet.Cells[c, l].Value = id.ToString();
            //保存excel
            package.Save();
        }
    }

但修改方法打包出桌面应用执行不了。有需要再研究

你可能感兴趣的:(Unity,unity,游戏引擎)