Unity 3D 使用 ExcelDataReader 读取Excel文件

Excel的第三方库https://github.com/ExcelDataReader/ExcelDataReader

打开上面的Github地址。克隆库到本地,然后解压Zip后打开文件夹。

用VS打开Vs解决方案.sln,生成解决方案。

之后在工程目录下找到下面的这个文件夹中选中的三个文件,拖入Assets/Plugins/下。

还有这个,Unity安装目录的System.Data.dll 放入工程里。

如图

using System.Data;
using System.IO;
using ExcelDataReader;
using UnityEngine;

public class ReadExcelMyTools : MonoBehaviour
{
    private string FilePath;

    private void Start()
    {
        FilePath = Application.dataPath + "/../Excels/LevelInfo.xlsx";
        //Debug.Log(FilePath);
        ReadExcelStream();
    }

    private void ReadExcelStream()
    {
        FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelDataReader.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("row :" + i.ToString() + " column :" + j.ToString() + "+++++" + nvalue);
            }
        }

        excelDataReader.Close();
    }
}

excel表放在Assets同级目录,当然也可以放在别处。

有人说报错,把安装目录下面的 I18N 开头的也导入工程。

编辑器模式

using System.Data;
using System.IO;
using ExcelDataReader;
using UnityEditor;
using UnityEngine;

public class ReadExcelEditorMyTools
{
    [MenuItem("MyExcel/ReadExcel", false, 0)]
    static void ReadExcel()
    {
        string FilePath = Application.dataPath + "/../Excels/LevelInfo.xlsx";

        FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelDataReader.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("row :" + i.ToString() + " column :" + j.ToString() + "+++++" + nvalue);
            }
        }

        excelDataReader.Close();
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值