在項目開發過程當中,常常會用到大量的可編輯的數據,而這些數據使用Json,XML等形式存儲又比較麻煩 PS:對於不懂電腦的客戶來講徹底就是看天書,後期編輯也比較費事。因此就有了使用Excel表格進行數據的存儲和讀取。好比:人員名單(姓名,班級,學號等信息)。因此本篇文章就分享一下如何使用Unity讀寫Excel表格。markdown
此篇文章中使用的是Unity2019.4.17版本,VS2017。須要引入EPPlus.dll,Excel.dll 和ICSharpCode.SharpZipLib庫文件。測試
下圖是咱們要讀取的數據this
讀取Excel須要用到Excel.dll 和ICSharpCode.SharpZipLib庫文件。將其放到Plugins文件夾下。 首先須要引入命名空間spa
使用以下腳本,首先加載Excel文件excel
FileStream fileStream = File.Open(Application.streamingAssetsPath + "/"+ 表格名, FileMode.Open, FileAccess.Read);
code
上一句代碼爲Unity的StreamingAssets目錄下的.xlsx文件的路徑:Application.streamingAssetsPath + "/表格名.xlsx"orm
讀取文件以後要對文件進行相似實例化解析操做,ip
IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
開發
將全部數據所有讀取出來字符串
DataSet result = excelDataReader.AsDataSet();
接下來獲取表格的行數和列數
// 獲取表格有多少列
int columns = result.Tables[0].Columns.Count;
// 獲取表格有多少行
int rows = result.Tables[0].Rows.Count;
複製代碼
接下來咱們要把讀取到的每一行每一列數據進行整合,經過for循環,將每一行的數據進行記錄,首先讀取每一行,而後在讀取這一行中的每一列,從而得到整行數據信息。具體代碼信息以下
// 根據行列依次打印表格中的每一個數據
List<string> excelDta = new List<string>();
//第一行爲表頭,不讀取。沒有表頭從0開始
for (int i = 1; i < rows; i++)
{
value = null;
all = null;
for (int j = 0; j < columns; j++)
{
// 獲取表格中指定行指定列的數據
value = result.Tables[0].Rows[i][j].ToString();
if (value == "")
{
continue;
}
all = all + value + "|";
}
if (all != null)
{
//print(all);
excelDta.Add(all);
}
}
複製代碼
在這裏,每一行中每一列數據是使用「|進行分割的,接下來要作的是將讀取到的每一行數據存到定義好的類中。方便之後好調用。 首先定義數據類型,要有ID(序號),姓名,班級,學號,電話。 代碼以下:
public class PlayerInfo
{
// 序號
public string ID { get; set; }
// 姓名
public string Name { get; set; }
// 學號
public string Number { get; set; }
// 班級
public string Class { get; set; }
/// 手機號
public string Mobile { get; set; }
public PlayerInfo(string id, string name,string class,string number,string mobile)
{
this.ID = id;
this.Name = name;
this.Number = number;
this.Class = class;
this.Mobile = mobile;
}
}
複製代碼
接下來遍歷讀取到的數據進行處理而後存儲到List中,首先使用Split("|"),讀數據進行截取,組成一個是字符串數據,而後在new一個PlayerInfo,傳入參數,最後將new的PlayerInfo存入List中。
string[] item = data_1[i].Split('|');
複製代碼
PlayerInfo playerinfo = new PlayerInfo(item[0], item[1], item[2], mobile_Enc);
複製代碼
List<PlayerInfo> Players = new List<PlayerInfo>();
Players.Add(playerinfo);
複製代碼
到此,全部數據也就所有讀取出來了,能夠再控制檯進行一下打印,輸入結果以下,證實數據讀取成功
最終結果是要在上述表格中添加一列,以下所示
首先,須要引入EPPlus.dll庫文件,其次,須要引用命名空間:using OfficeOpenXml;以便對Excel表格進行寫入 一樣,首先須要打開文件,確認文件是否存在,不存在須要自動建立一個文件
//文件路徑
string path = Application.streamingAssetsPath + "/表格名.xlsx";
FileInfo newFile = new FileInfo(path);
//判斷文件是否中存在
if (!newFile.Exists)
{
//建立一個新的excel文件
newFile = new FileInfo(path);
}
複製代碼
接下來進行文件的寫入,在這以前,咱們須要要寫入的文件存入一個list中,一樣使用"|"對每一列數據進行分割,呀添加的數據爲
"3|王五|20210103|三年一班|11111111111"
經過ExcelPackage打開文件
using (ExcelPackage package = new ExcelPackage(newFile))
而後進行數據分割,存儲,這裏之因此使用一整串字符串是爲了之後存儲數據較多的時候,能夠直接進行遍歷,省的一點點進行添加
string[] messages = newList[i].Split('|');
string itemId = messages[0];
string itemName = messages[1];
string itemNumber = messages[2];
string imageClass = messages[3];
string imageMobile = messages[4];
//添加第四行數據
worksheet.Cells["A4"].Value = itemId;
worksheet.Cells["B4"].Value = itemName;
worksheet.Cells["C4"].Value = itemNumber;
worksheet.Cells["D4"].Value = imageClass;
worksheet.Cells["E4"].Value = imageMobile;
複製代碼
最後,對錶格進行保存
package.Save();
經測試發現以下幾個問題: 一、在打包以後或者沒打包的時候就會出現讀取不到excel數據,須將
Unity\Editor\Data\Mono\lib\mono\unity目錄下的一系列i18n相關dll導入項目Plugins文件夾中。 二、若是xlsx文件的後綴爲.xlsx,讀取的代碼應該爲
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
若使用CreateBinaryReader讀取,則在excelReader.AsDataSet();會報錯NullReferenceException: Object reference not set to an instance of an object
三、若是xlsx文件的後綴爲.xls,讀取的代碼應該爲
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
若使用CreateOpenXmlReader讀取,則在CreateOpenXmlReader處會報錯ArgumentNullException: Value cannot be null.
總體項目案例會在後期整理好以後分享給你們,若有錯誤之處還請多多指出。