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); 即可

 

 

 

posted @ 2018-05-21 17:08  nanyang0310  阅读(1558)  评论(0编辑  收藏  举报