GQ_Communicate/GQ_URP/GQ/Assets/Adam/Utils/ExcelUtil.cs

75 lines
2.5 KiB
C#

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<DeviceData> GetAllDeviceInfo(string _path, string sheetName)
//public static List<DeviceDataCard> GetAllDeviceInfo(string _path, string sheetName)
{
if (File.Exists(_path))
{
List<DeviceData> devices = new List<DeviceData>();
//List<DeviceDataCard> devices = new List<DeviceDataCard>();
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();
//DeviceDataCard s = new DeviceDataCard();
//s.Num = dataRowCollection[i][0].ToString();
//s.deviceName = dataRowCollection[i][1].ToString();
//s.manufacturer = dataRowCollection[i][2].ToString();
//s.deviceType = dataRowCollection[i][3].ToString();
devices.Add(s);
//Debug.Log(s.ID);
//Debug.Log(s.Num);
}
return devices;
}
else
{
Debug.Log("没有数据");
return null;
}
}
else
{
Debug.Log("没有该文件");
return null;
}
}
/// <summary>
/// 读取excel文件内容获取行数 列数 方便保存
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="tableIndex">sheet index</param>
/// <returns></returns>
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;
}
}