unity3d讀寫(xiě)EXCEL文件的方法
對(duì) Excel 表的操作少不了要引入第三方庫(kù),首先我們需要引入 Excel.dll 和 ICSharpCode.SharpZipLib.dll,這兩個(gè)類(lèi)庫(kù)在網(wǎng)上都能找到;然后我們還需要引入 System.Data.dll,這個(gè)類(lèi)庫(kù)在 Unity3D 的安裝路徑下的 Editor\Data\Mono\lib\mono\unity 文件夾下能找到。wiseglove數(shù)據(jù)手套客戶(hù),可以在我們提供的數(shù)據(jù)手套FOR UNITY3D演示項(xiàng)目下找到。
using Excel; using System.Data; using System.IO; using UnityEngine; public class Test : MonoBehaviour { #region -- 變量定義 #endregion #region -- 系統(tǒng)函數(shù) private void Start() { DataRowCollection _dataRowCollection = ReadExcel(Application.streamingAssetsPath + "/手套錄制數(shù)據(jù).xlsx"); //這里從 1 開(kāi)始循環(huán),因?yàn)榈谝恍斜槐眍^占據(jù)了。所以具體解析數(shù)據(jù)的時(shí)候需要根據(jù)具體情況來(lái)定。 for (int i = 1; i < _dataRowCollection.Count; i++) { Debug.Log("拇指" + _dataRowCollection[i][0] + "--" + "食指" + _dataRowCollection[i][1] + "--" + "中指" + _dataRowCollection[i][2])+ "--" + "無(wú)名指" + _dataRowCollection[i][2])+ "--" + "小指" + _dataRowCollection[i][2]); } } #endregion #region -- 自定義函數(shù) ////// 讀取 Excel 表并返回一個(gè) DataRowCollection 對(duì)象 //////手套錄制數(shù)據(jù)的Excel 表路徑///讀取的 Sheet 索引。Excel 表中是有多個(gè) Sheet 的///private static DataRowCollection ReadExcel(string _path, int _sheetIndex = 0) { FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read); //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//讀取 Excel 1997-2003版本 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//讀取 2007及以后的版本 DataSet result = excelReader.AsDataSet(); return result.Tables[_sheetIndex].Rows; } ////// 讀取 Excel 表并返回一個(gè) DataRowCollection 對(duì)象 //////Excel 表路徑///讀取的 Sheet 名稱(chēng)。Excel 表中是有多個(gè) Sheet 的///private static DataRowCollection ReadExcel(string _path, string _sheetName) { FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read); //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//讀取 Excel 1997-2003版本 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//讀取 2007及以后的版本 DataSet result = excelReader.AsDataSet(); return result.Tables[_sheetName].Rows; } #endregion }
這里需要注意的是,根據(jù) Excel 表的版本不同,使用的方法也不一致,我在代碼中也有注釋?zhuān)蠹铱匆幌戮托小_€有就是 Sheet ,在讀取的時(shí)候,我們可以根據(jù)索引去讀取,也可以根據(jù)名稱(chēng)去讀取,我也寫(xiě)了重載方法。
如果這樣寫(xiě),發(fā)布后運(yùn)行,也許會(huì)報(bào)錯(cuò),這時(shí)我們就又需要引入第三方庫(kù)了,去 Unity3D 安裝路徑下的Editor\Data\Mono\lib\mono\unity,找到所有 I18N 開(kāi)頭的類(lèi)庫(kù)導(dǎo)入U(xiǎn)nity中,就不會(huì)報(bào)錯(cuò)了。
OK, 現(xiàn)在Excel 表的讀取功能解決了,那我們?nèi)绾紊梢粡?Excel 表,并寫(xiě)入數(shù)據(jù)呢?這時(shí)我們需要導(dǎo)入一個(gè)叫 EPPlus.dll 的類(lèi)庫(kù),網(wǎng)上也有,大家可以自己下載。
代碼如下:
private void Start() { string _filePath = Application.streamingAssetsPath + "/錄制數(shù)據(jù).xlsx"; string _sheetName = "詳情"; FileInfo _excelName = new FileInfo(_filePath); if (_excelName.Exists) { //刪除舊文件,并創(chuàng)建一個(gè)新的 excel 文件。 _excelName.Delete(); _excelName = new FileInfo(_filePath); } //通過(guò)ExcelPackage打開(kāi)文件 using (ExcelPackage package = new ExcelPackage(_excelName)) { //在 excel 空文件添加新 sheet,并設(shè)置名稱(chēng)。 ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(_sheetName); //添加列名 worksheet.Cells[1, 1].Value = "時(shí)間"; worksheet.Cells[1, 2].Value = "拇指"; worksheet.Cells[1, 3].Value = "中指"; worksheet.Cells[1, 4].Value = "無(wú)名指"; worksheet.Cells[1, 5].Value = "小指"; //添加一行數(shù)據(jù) worksheet.Cells[2, 1].Value = 10; //ms worksheet.Cells[2, 2].Value = 33.0f; worksheet.Cells[2, 3].Value = 34.0f; worksheet.Cells[2, 4].Value = 35.0f; worksheet.Cells[2, 5].Value = 36.0f; //添加一行數(shù)據(jù) worksheet.Cells[3, 1].Value = 20; //ms worksheet.Cells[3, 2].Value = 33.0f; worksheet.Cells[3, 3].Value = 34.0f; worksheet.Cells[3, 4].Value = 35.0f; worksheet.Cells[3, 5].Value = 36.0f; //添加一行數(shù)據(jù) worksheet.Cells[4, 1].Value = 30; //ms worksheet.Cells[4, 2].Value = 33.0f; worksheet.Cells[4, 3].Value = 34.0f; worksheet.Cells[4, 4].Value = 35.0f; worksheet.Cells[4, 5].Value = 36.0f; //保存excel package.Save(); } }
Excel 表的讀寫(xiě)操作大致就是這樣的。因?yàn)?Excel 表 包含太多的格式信息,好是將 Excel 表另存為純文本的 CSV 文件再去讀取,我們另一篇技術(shù)文章討論關(guān)于 CSV 文件的讀取。
- 上一篇:UNITY3D讀寫(xiě)CVS格式文件錄制與存儲(chǔ)數(shù)據(jù)手套數(shù)據(jù) 2019/11/12
- 下一篇:UNITY3D使用C#腳本的幾種讀寫(xiě)TXT文本文件的方法 2019/11/12