using System; using System.Collections.Generic; using UnityEngine; using ClosedXML.Excel; public class Excel { private List datas; public List getDatas { get { return datas; } } // Á»´õ °íµµÈ­°¡ ÇÊ¿äÇÔ. //public Sheet findSheet(string sheetName) { // int index = datas.FindIndex(n => n.name == sheetName); // if(index == -1) // { // Debug.Log("null exception"); // return null; // } // return datas[index]; //} #region Dynamic //ÀÌ·¯ÇÑ ÇüÅ·Π»ç¿ëÇÒ°Í public Dynamic dynamic; public class Dynamic { List datas; public float selectVelue(string name) { return datas.Find(n => n.name.Equals(name)).velue; } public Dynamic(Sheet sheet) { datas = new List(); foreach (var item in sheet.dicViewer) { Data data = new Data((string)item.Value["name"], (float)item.Value["velue"]); datas.Add(data); } } public class Data { public string name; public float velue; public Data(string name, float velue) { this.name = name; this.velue = velue; } } } #endregion public Excel() { ExcelManager excel = new ExcelManager(); //Dynamic, Unit excel.Add("Datas.xlsx"); datas = excel.Play(); dynamic = new Dynamic(datas.Find(n => n.name.Equals("Dynamic"))); } public class Sheet { string _name; public string name { get { return _name; } } List _variable; public List variable { get { return _variable; } } List _dataEnum; public List dataEnum { get { return _dataEnum; } } List _type; public List type { get { return _type; } } Dictionary> _dicViewer; public Dictionary> dicViewer { get { return _dicViewer; } } public Sheet(string name, List variable, List dataEnum, List type, Dictionary> dicViewer) { this._name = name; this._variable = variable; this._dataEnum = dataEnum; this._type = type; this._dicViewer = dicViewer; } public Sheet(Sheet sheet) { this._name = new string(sheet.name); this._variable = new List(sheet.variable); this._dataEnum = new List(sheet.dataEnum); this._type = new List(type); this._dicViewer = new Dictionary>(dicViewer); } } class ExcelManager { readonly string path = Application.dataPath + "/07_Excel/"; List _sheets; List readList; public ExcelManager() { _sheets = new List(); readList = new List(); } public void Add(string file) { readList.Add(path + file); } public List Play() { for (int n = 0; n < readList.Count; n++) if (!ExcelLoad(readList[n])) return null; return _sheets; } public bool ExcelLoad(string pathFile) { _sheets = new List(); // ¿¢¼¿ ÆÄÀÏÀ» ¿±´Ï´Ù. using (var workbook = new XLWorkbook(pathFile)) { // ¸ðµç ¿öÅ©½ÃÆ®¸¦ ¹Ýº¹ÇÕ´Ï´Ù. foreach (var worksheet in workbook.Worksheets) { // º¯¼ö À̸§, µ¥ÀÌÅÍ Å¸ÀÔ, µ¥ÀÌÅÍ ¿­°ÅÇü, µñ¼Å³Ê¸® ÃʱâÈ­ var variable = new List(); // º¯¼ö¸í var dataEnum = new List(); // server¿Í client¸¦ ³ª´­ ±âÁØ var type = new List(); // º¯¼ö ŸÀÔ var dicViewer = new Dictionary>(); // Çà°ú ¿­ÀÇ ¼ö ¾ò±â var vertical = worksheet.RangeUsed().RowCount() - 1; // µ¥ÀÌÅͰ¡ ÀÖ´Â ÇàÀÇ ¼ö var horizontal = worksheet.RangeUsed().ColumnCount() - 1; // µ¥ÀÌÅͰ¡ ÀÖ´Â ¿­ÀÇ ¼ö // º¯¼ö À̸§°ú ŸÀÔÀ» »ðÀÔ for (int n = 0; n <= horizontal; n++) { variable.Add(worksheet.Cell(1, n + 1).GetValue()); type.Add(worksheet.Cell(4, n + 1).GetValue().ToLower()); dataEnum.Add(worksheet.Cell(3, n + 1).GetValue().ToLower()); } bool isIndex = variable[0] == "index"; for (int n = 5; n <= vertical + 1; n++) { var dataList = new Dictionary(); for (int m = 0; m <= horizontal; m++) { object getData; switch (type[m]) { case "bool": getData = (worksheet.Cell(n, m + 1).Value.ToString() == "true"); break; case "int": case "enum": getData = (int)worksheet.Cell(n, m + 1).Value; break; case "long": getData = (long)worksheet.Cell(n, m + 1).Value; break; case "float": getData = (float)worksheet.Cell(n, m + 1).Value; break; case "time": getData = (DateTime)worksheet.Cell(n, m + 1).Value; break; default: getData = worksheet.Cell(n, m + 1).Value.ToString(); break; } dataList.Add(variable[m], getData); } dicViewer.Add((isIndex ? (long)dataList["index"] : n - 4), dataList); } var sheet = new Sheet(worksheet.Name, variable, dataEnum, type, dicViewer); _sheets.Add(sheet); } } return true; } } }