using Excel; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using UnityEngine; public static class ExcelUtil { public static List GetAllDeviceInfo(string _path, string sheetName) { if (File.Exists(_path)) { List devices = new List(); DataRowCollection dataRowCollection = ReadExcel(_path, sheetName); if (dataRowCollection.Count > 1) { for (int i = 1; i < dataRowCollection.Count; i++) { DeviceData s = new DeviceData(); s.ID = dataRowCollection[i][0].ToString(); s.deviceType = dataRowCollection[i][1].ToString(); s.manufacturer = dataRowCollection[i][2].ToString(); s.deviceNum = dataRowCollection[i][3].ToString(); s.isInCabinet = dataRowCollection[i][4].ToString(); s.uPos = dataRowCollection[i][5].ToString(); devices.Add(s); Debug.Log(s.ID); } return devices; } else { Debug.Log("没有数据"); return null; } } else { Debug.Log("没有该文件"); return null; } } /// /// 读取excel文件内容获取行数 列数 方便保存 /// /// 文件路径 /// sheet index /// private static DataRowCollection ReadExcel(string filePath, string tableName) { FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); return result.Tables[tableName].Rows; } }