unity中解析excel表
生活随笔
收集整理的這篇文章主要介紹了
unity中解析excel表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上代碼
using Excel; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using UnityEngine;public class ExcelLogicManager : MonoBehaviour {public static string ExcelName = "UserLevel.xlsx";public static string LogicExcelName = "邏輯表.xls";public static string[] sheetNames = { "sheet1", "sheet2", "sheet3" };public static List<DeviceData> DeviceDataList = new List<DeviceData>();void Start () {XLS("");//XLS("/邏輯表.xls");// SelectDeviceDataTableXLS("/邏輯表.xls"); }/// <summary>/// 解析Excel表/// </summary>void XLSX(){FileStream stream = File.Open(Application.dataPath + "/Excel" + "/UserLevel.xlsx", FileMode.Open, FileAccess.Read);IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);DataSet result = excelReader.AsDataSet();int columns = result.Tables[0].Columns.Count;int rows = result.Tables[0].Rows.Count;for (int i = 0; i < rows; i++){//for (int j = 0; j < columns; j++)//{// string nvalue = result.Tables[0].Rows[i][j].ToString();// Debug.Log(nvalue);//} }}void XLS(string XlsPath){FileStream stream = File.Open(Application.dataPath + "/Excel" + "/邏輯表.xls", FileMode.Open, FileAccess.Read);// FileStream stream = File.Open(Application.dataPath + "/Excel" + XlsPath, FileMode.Open, FileAccess.Read);IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);DataSet result = excelReader.AsDataSet();int columns = result.Tables[0].Columns.Count;int rows = result.Tables[0].Rows.Count;for (int i = 1; i < rows; i++){DeviceData deviceData = new DeviceData();deviceData.ID = Int32.Parse(result.Tables[0].Rows[i][0].ToString());deviceData.DevName = result.Tables[0].Rows[i][1].ToString();deviceData.OutLogic = result.Tables[0].Rows[i][2].ToString();deviceData.RecvDelay = result.Tables[0].Rows[i][3].ToString();deviceData.LoseDelay = result.Tables[0].Rows[i][4].ToString();deviceData.InValue = result.Tables[0].Rows[i][5].ToString();deviceData.Notes = result.Tables[0].Rows[i][6].ToString();deviceData.NoteDescription = result.Tables[0].Rows[i][8].ToString();DeviceDataList.Add(deviceData);}Debug.Log(DeviceDataList.Count);//for (int i = 0; i < rows; i++)//{// for (int j = 0; j < columns; j++)// {// string nvalue = result.Tables[0].Rows[i][j].ToString();// Debug.Log(nvalue);// }//} }public static List<DeviceData> SelectDeviceDataTable(string xmlPath, int tableIndex){List<DeviceData> DeviceDataList = new List<DeviceData>();FileStream stream = File.Open(xmlPath, FileMode.Open, FileAccess.ReadWrite);IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);DataSet result = excelReader.AsDataSet();int rows = result.Tables[tableIndex].Rows.Count;for (int i = 0; i < rows; i++){DeviceData deviceData = new DeviceData();deviceData.ID = Int32.Parse(result.Tables[0].Rows[i][0].ToString());deviceData.DevName = result.Tables[0].Rows[i][1].ToString();deviceData.OutLogic = result.Tables[0].Rows[i][2].ToString();deviceData.RecvDelay = result.Tables[0].Rows[i][3].ToString();deviceData.LoseDelay = result.Tables[0].Rows[i][4].ToString();deviceData.InValue = result.Tables[0].Rows[i][5].ToString();deviceData.Notes= result.Tables[0].Rows[i][6].ToString();deviceData.NoteDescription = result.Tables[0].Rows[i][8].ToString();DeviceDataList.Add(deviceData);}return DeviceDataList;}public static List<DeviceData> SelectDeviceDataTableXLS(string XlsPath){List<DeviceData> DeviceDataList = new List<DeviceData>();FileStream stream = File.Open(Application.dataPath + "/Excel" + XlsPath, FileMode.Open, FileAccess.Read);IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);DataSet result = excelReader.AsDataSet();int rows = result.Tables[0].Rows.Count;for (int i = 1; i < rows; i++){DeviceData deviceData = new DeviceData();deviceData.ID = Int32.Parse(result.Tables[0].Rows[i][0].ToString());deviceData.DevName = result.Tables[0].Rows[i][1].ToString();deviceData.OutLogic = result.Tables[0].Rows[i][2].ToString();deviceData.RecvDelay = result.Tables[0].Rows[i][3].ToString();deviceData.LoseDelay = result.Tables[0].Rows[i][4].ToString();deviceData.InValue = result.Tables[0].Rows[i][5].ToString();deviceData.Notes = result.Tables[0].Rows[i][6].ToString();deviceData.NoteDescription = result.Tables[0].Rows[i][8].ToString();DeviceDataList.Add(deviceData);}Debug.Log(DeviceDataList.Count);return DeviceDataList;}}public class DeviceData {private int m_ID;public int ID{get { return m_ID; }set { m_ID = value; }}private string m_DevName;public string DevName{get { return m_DevName; }set { m_DevName = value; }}private string m_OutLogic;public string OutLogic{get { return m_OutLogic; }set { m_OutLogic = value; }}private string m_RecvDelay;public string RecvDelay{get { return m_RecvDelay; }set { m_RecvDelay = value; }}private string m_LoseDelay;public string LoseDelay{get { return m_LoseDelay; }set { m_LoseDelay = value; }}private string m_InValue;public string InValue{get { return m_InValue; }set { m_InValue = value; }}private string m_Notes;public string Notes{get { return m_Notes; }set { m_Notes = value; }}private string m_NoteDescription;public string NoteDescription{get { return m_NoteDescription; }set { m_NoteDescription = value; }} }?
?
打包后,發現Excel沒有被讀取,原因如下:在Asste文件下Excel發布時沒有打包到發布文件中(也就是.EXE),修改方法如下
在Project視圖中--創建StreamingAssets文件夾,將Excel文件放在該文件夾下,(具體關于StreamingAssets文件夾的一些內容網上可以搜)---修改讀取Excel的路徑方法,將
改成
?
?
?
轉載于:https://www.cnblogs.com/nanyang0310/p/9068189.html
總結
以上是生活随笔為你收集整理的unity中解析excel表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux虚拟机时间同步
- 下一篇: 蓦然回首,我是如何走上数据库开发这条路的