zoukankan      html  css  js  c++  java
  • 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的路径方法,将

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

    改成

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

     

  • 相关阅读:
    -lpopt is not found while cross compiling for aarch64
    设置进程的cpu亲和性
    在ARM64位开发板上兼容ARM32位的可执行程序
    ARM开发板上查看动态库或者可执行程序的依赖关系
    交叉编译tmux
    使用PSCI机制的SMP启动分析
    将qemu使用的设备树dump出来
    故障review的一些总结
    理解Compressed Sparse Column Format (CSC)
    统计分析工程的依赖项
  • 原文地址:https://www.cnblogs.com/nanyang0310/p/9068189.html
Copyright © 2011-2022 走看看