
老规矩,先介绍一下 Unity 的科普小知识:
在Unity中读取Excel文件首先要导入两个DLL文件:Excel.dll和 ICSharpCode.SharpZipLib库文件
其实还要倒导入一个System.Data.dll ,但是新版的Unity中自带这个所以就不需要导入了
但是上面两个Dll文件是必须要导入的!上述DLL文件的下载链接在这
我们将DLL文件导入Unity中的Plugins文件夹下

然后在代码中调用即可,示例如下:
using Excel;
using System.Data;
using System.IO;
using UnityEngine;
public class ExcelDemo : MonoBehaviour
{
void Start()
{
//这里设置需要读取的文件的路径
string FilePath = Application.dataPath + "/StudentName.xlsx";
//读取该文件
FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
//读取Excel文件
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.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();
//打印Excel文件中的内容
Debug.Log(nvalue);
}
}
}
}该脚本读取的是Assets文件夹下的StudentName.xlsx

将该脚本挂在场景中效果如下:
