所需dll文件:在unity中创建Plugis文件夹并导入Excel.dll到文件夹下
命名空间:
using System.IO; using Excel; using System.Data;
方法调用:
DataRowCollection dataRowCollection = ReadExcel(“路径”+“Excel文件名”); for (int i = 1; i < dataRowCollection.Count; i++) { “第一行”.Add(dataRowCollection[i][0].ToString()); “第二行”.Add(dataRowCollection[i][1].ToString()); “第三行”.Add(dataRowCollection[i][2].ToString()); ...... }
方法:
/// <summary> /// 读取 Excel /// </summary> /// <param name="_path">Excel 表路径</param> /// <param name="_sheetIndex">读取的 Sheet 索引</param> /// <returns></returns> private DataRowCollection ReadExcel(string _path, int _sheetIndex = 0) { FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read); //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本 DataSet result = excelReader.AsDataSet(); return result.Tables[_sheetIndex].Rows; }