unity读excel
·
第一步:获取ExcelDataReader插件
打开vs2022
创建新项目 模板选新的控制台应用程序项目
注意项目起名的的时候不要和插件重名
在解决方案管理器中右键点击你新建的项目
找到管理NuGet程序包并点击
浏览中搜索ExcelDataReader
下载 ExcelDataReader 和 ExcelDataReader.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;
}
}
更多推荐



所有评论(0)