unity读excel

第一步:获取ExcelDataReader插件

打开vs2022 

创建新项目 模板选新的控制台应用程序项目

注意项目起名的的时候不要和插件重名

解决方案管理器中右键点击你新建的项目

找到管理NuGet程序包并点击

浏览中搜索ExcelDataReader

下载 ExcelDataReaderExcelDataReader.DataSet

安装成功之后回到解决方案中会发现我们的项目下面依赖项包中就会有我们刚刚安装好的两个

我们可以直接去访问他们的目录 在其子目录lib下会有不同netstandard的版本

我的unity版本是2022所以我选择netstandard2.1

打开后我们找到需要的dll文件 另一个同理

另一种方式是回到vs 在解决方案中选择我们的项目

在顶上菜单中找到生成 点开后选择第一个生成解决方案

生成完毕后 打开我们的项目目录 在bin\Debug\net8.0\下 也可以找到两个dll

我们把两个dll复制到我们的unity项目中

放到Plugins文件夹

接下来就直接上代码

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

public class ExcelReader : MonoBehaviour
{
    IEnumerator ReadFromStreamingAssets()
    {
        // 构建 StreamingAssets 文件夹中的 Excel 文件路径
        string filePath = Path.Combine(Application.streamingAssetsPath, "YourExcelFile.xlsx");

        // 使用 UnityWebRequest 下载 Excel 文件
        UnityWebRequest www = UnityWebRequest.Get(filePath);
        yield return www.SendWebRequest();

        // 检查请求是否成功
        if (www.result == UnityWebRequest.Result.Success)
        {
            // 读取文件数据到 MemoryStream
            MemoryStream stream = new MemoryStream(www.downloadHandler.data);

            // 创建 ExcelDataReader 读取器
            IExcelDataReader reader = null;
            if (Path.GetExtension(filePath) == ".xlsx")
            {
                reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }
            else if (Path.GetExtension(filePath) == ".xls")
            {
                reader = ExcelReaderFactory.CreateBinaryReader(stream);
            }

            if (reader != null)
            {
                // 读取 Excel 数据
                var result = reader.AsDataSet();
                DataTable table = result.Tables[0]; // 假设文件只有一个工作表

                foreach (DataRow row in table.Rows)
                {
                    foreach (var item in row.ItemArray)
                    {
                        Debug.Log(item); // 输出每个单元格的内容
                    }
                }

                reader.Close(); // 关闭读取器
            }
            else
            {
                Debug.LogError("Unsupported file format.");
            }
        }
        else
        {
            Debug.LogError("Error loading file: " + www.error);
        }
    }


    public void ReadFromAssets()
    {
        // 在编辑器中有效,但打包后无效
        string filePath = Application.dataPath + "/YourExcelFile.xlsx";

        if (File.Exists(filePath))
        {
            using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    var result = reader.AsDataSet();
                    DataTable table = result.Tables[0];

                    foreach (DataRow row in table.Rows)
                    {
                        foreach (var item in row.ItemArray)
                        {
                            Debug.Log(item);
                        }
                    }
                }
            }
        }
        else
        {
            Debug.LogError("File not found: " + filePath);
        }
    }


    IEnumerator ReadFromResources() 
    {
        // 加载 Resources 文件夹中的 Excel 文件
        TextAsset excelFile = Resources.Load<TextAsset>("YourExcelFile"); // 不需要扩展名

        if (excelFile != null)
        {
            // 将加载的文件数据转换为 MemoryStream
            MemoryStream stream = new MemoryStream(excelFile.bytes);

            // 创建 ExcelDataReader 读取器
            IExcelDataReader reader = null;
            if (Path.GetExtension("YourExcelFile.xlsx") == ".xlsx")
            {
                reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }
            else if (Path.GetExtension("YourExcelFile.xlsx") == ".xls")
            {
                reader = ExcelReaderFactory.CreateBinaryReader(stream);
            }

            if (reader != null)
            {
                // 读取 Excel 数据
                var result = reader.AsDataSet();
                DataTable table = result.Tables[0]; // 假设文件只有一个工作表

                foreach (DataRow row in table.Rows)
                {
                    foreach (var item in row.ItemArray)
                    {
                        Debug.Log(item); // 输出每个单元格的内容
                    }
                }

                reader.Close(); // 关闭读取器
            }
            else
            {
                Debug.LogError("Unsupported file format.");
            }
        }
        else
        {
            Debug.LogError("Error loading file from Resources.");
        }
        yield return null;
    }
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一分之三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值