unity 读取Excle 表格

链接:https://pan.baidu.com/s/1N3lV6VEKuuXi-a9Db2XmSw?pwd=icmw
提取码:icmw
--来自百度网盘超级会员V5的分享

 

 

打包出来也可使用

using System.Data;
using System.IO;
using Excel;
using UnityEngine;
using UnityEngine.UI;

public class ExcelTool : MonoBehaviour
{

    public Text text;
    void Start()
    {
        ReadExcel("/cook.xlsx");
    }

    public void ReadExcel(string xmlName)
    {
        FileStream stream = File.Open(Application.streamingAssetsPath + xmlName, FileMode.Open, FileAccess.Read, FileShare.Read);
        //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本

        DataSet result = excelReader.AsDataSet();

        if (stream != null)
        {
            stream.Close();
        }

        int[] counts = GetCount(result.Tables[0]);
        int rows = counts[0];
        int columns = counts[1];
        Debug.LogError("row:" + rows + "...col:" + columns);
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                Debug.LogError(result.Tables[0].Rows[i][j].ToString());
                text.text += result.Tables[0].Rows[i][j].ToString();
            }
        }
    }

    private int[] GetCount(DataTable dt)
    {
        int i = dt.Rows.Count;
        for (int m = 0; m < dt.Rows.Count; m++)
        {
            if (string.IsNullOrEmpty(dt.Rows[m][0].ToString()))
            {
                i = m;
                break;
            }
        }

        int j = dt.Columns.Count;
        for (int n = 0; n < dt.Columns.Count; n++)
        {
            if (string.IsNullOrEmpty(dt.Rows[0][n].ToString()))
            {
                j = n;
                break;
            }
        }
        return new int[] { i, j };
    }
}

 

posted @ 2020-11-04 15:34  GP19  阅读(520)  评论(0)    收藏  举报