unity 读取excel

本文参考链接:http://www.xuanyusong.com/archives/2429

1.工程文件里加入

下载链接http://exceldatareader.codeplex.com/

2.加入红色引用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Excel;
using System.Data;

3.代码

 public static List<float> tt = new List<float>();
    public static List<float> ss = new List<float>();
    public static List<float> vv = new List<float>();
    public static List<float> aa = new List<float>();

    string shuzi;//截取的字符串
    float number;//转换成数字

    void Start()
    {

        XLSX();
        onxie();
    }


    void Update()
    {

    }

    //读取xlsx,注意不要用unity直接打开,否则会报错。如果报错,请重新放入一个表格。
    private void XLSX()
    {

        FileStream stream = File.Open(Application.dataPath + "/Excel/jianchuang.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;

        //print("列数" + columns);
        //print("行数" + rows);

        //从第二行开始输出。
        for (int i = 1; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                string nvalue = result.Tables[0].Rows[i][j].ToString();

                //判断截取字符串的长度。
                if (nvalue.Length == 1)
                {
                    //如果字符串的长度为1;
                    shuzi = nvalue.Substring(0, 1);

                }
                else if (nvalue.Length == 2)
                {
                    //如果字符串的长度为2;
                    shuzi = nvalue.Substring(0, 2);
                }
                else if (nvalue.Length == 3)
                {
                    //如果字符串的长度为3;
                    shuzi = nvalue.Substring(0, 3);
                }
                else if (nvalue.Length == 4)
                {
                    //如果字符串的长度为大于4;
                    shuzi = nvalue.Substring(0, 4);
                }
                else if (nvalue.Length >= 5)
                {
                    //如果字符串的长度为大于4;
                    shuzi = nvalue.Substring(0, 5);
                }
                else
                {
                    Debug.Log("存在空的字符串!!!");
                }

                //字符串转化成字符串。
                try
                {
                    number = float.Parse(shuzi);
                }
                catch
                {
                    Debug.Log("字符串没有转化成功!!!");
                }

                //Debug.Log("行数" + i + "列数" + j + ":" + number);

                //把excel按照列存到列表里面
                switch (j)
                {
                    case 0:
                        tt.Add(number);
                        break;
                    case 1:
                        ss.Add(number);
                        break;
                    case 2:
                        vv.Add(number);
                        break;
                    case 3:
                        aa.Add(number);
                        break;
                }

            }
        }

    }


    public void onxie()
    {
        print("输出一列的数据!");
        for (int i = 0; i < tt.Count; i++)
        {
            print(tt[i]);
        }
    }
 

4.加入excel。

注意:要是修改excel表格里面的数据不要再unity里面双击,否则会报错。

InvalidCastException: Cannot cast from source type to destination type.
System.Data.Common.DoubleDataContainer.DoCopyValue (System.Data.Common.DataContainer from, Int32 from_index, Int32 to_index)
System.Data.Common.DataContainer.CopyValue (System.Data.Common.DataContainer from, Int32 from_index, Int32 to_index)
System.Data.Common.RecordCache.CopyRecord (System.Data.DataTable fromTable, Int32 fromRecordIndex, Int32 toRecordIndex)

在外面改过之后,重新替换就行,虽然麻烦,但是不会报错。要是忘记了,还是报错了,就把里面的excel表格删掉,重新放一个就不会报错了。具体原因不知道。

5.发布出来的是这样的。

猜你喜欢

转载自blog.csdn.net/weixin_42399500/article/details/84989004