Unity读取Excel文件
一、介绍Excel格式
格式 | 版本 |
---|---|
xxx.xlsx | >=2007以后版本 |
xxx.xls | <=2003以前版本 |
二、导入DLL
新建一个folder取名Plugins 让后将你的DLL放进去就行了
1.这仨是读取Excel数据
2.这些是打包后需要的DLL,否则打包后无法读取
链接:https://pan.baidu.com/s/1viWLwlbGIeFSuC70xIOKlQ
提取码:lala
三、读取Excel
using UnityEngine;
using System.Data;
using System.IO;
using Excel;
using UnityEngine.UI;
public class finsh : MonoBehaviour
{
private void Start()
{
ReadExcel(Path);
}
public string Path;
public Text text;
void ReadExcel(string Path)
{
string path = Application.streamingAssetsPath + Path;
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
//2003 前版本的 .xls
//IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//2007 后版本的 .xslx
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
int rows = result.Tables[0].Rows.Count;
int cols = result.Tables[0].Columns.Count;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
text.text += result.Tables[0].Rows[i][j].ToString() + "|";
}
text.text += "\n";
}
}
}