◄ Unity 『功能總結』►——Excel讀取
阿新 • • 發佈:2020-12-23
技術標籤:excel
Unity讀取Excel檔案
一、介紹Excel格式
格式 | 版本 |
---|---|
xxx.xlsx | >=2007以後版本 |
xxx.xls | <=2003以前版本 |
二、匯入DLL
新建一個folder取名Plugins 讓後將你的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";
}
}
}