日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

unity中解析excel表

發(fā)布時(shí)間:2024/4/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity中解析excel表 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上代碼

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; }} }

?



?

打包后,發(fā)現(xiàn)Excel沒有被讀取,原因如下:在Asste文件下Excel發(fā)布時(shí)沒有打包到發(fā)布文件中(也就是.EXE),修改方法如下
在Project視圖中--創(chuàng)建StreamingAssets文件夾,將Excel文件放在該文件夾下,(具體關(guān)于StreamingAssets文件夾的一些內(nèi)容網(wǎng)上可以搜)---修改讀取Excel的路徑方法,將

File.Open(Application.dataPath + "/Excel" + XlsPath, FileMode.Open, FileAccess.Read);

改成

File.Open(Application.streamingAssetsPath+ "/Excel" + XlsPath, FileMode.Open, FileAccess.Read); 即可

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/nanyang0310/p/9068189.html

總結(jié)

以上是生活随笔為你收集整理的unity中解析excel表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。