Wizard_Of_Wak/Assets/1_Script/System/Excel.cs

200 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using ClosedXML.Excel;
public class Excel
{
private List<Sheet> datas;
public List<Sheet> 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<Data> datas;
public float selectVelue(string name)
{
return datas.Find(n => n.name.Equals(name)).velue;
}
public Dynamic(Sheet sheet)
{
datas = new List<Data>();
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<string> _variable;
public List<string> variable { get { return _variable; } }
List<string> _dataEnum;
public List<string> dataEnum { get { return _dataEnum; } }
List<string> _type;
public List<string> type { get { return _type; } }
Dictionary<long, Dictionary<string, object>> _dicViewer;
public Dictionary<long, Dictionary<string, object>> dicViewer { get { return _dicViewer; } }
public Sheet(string name, List<string> variable, List<string> dataEnum, List<string> type, Dictionary<long, Dictionary<string, object>> 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<string>(sheet.variable);
this._dataEnum = new List<string>(sheet.dataEnum);
this._type = new List<string>(type);
this._dicViewer = new Dictionary<long, Dictionary<string, object>>(dicViewer);
}
}
class ExcelManager
{
readonly string path = Application.dataPath + "/07_Excel/";
List<Sheet> _sheets;
List<string> readList;
public ExcelManager()
{
_sheets = new List<Sheet>();
readList = new List<string>();
}
public void Add(string file)
{
readList.Add(path + file);
}
public List<Sheet> 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<Sheet>();
// 엑셀 파일을 엽니다.
using (var workbook = new XLWorkbook(pathFile))
{
// 모든 워크시트를 반복합니다.
foreach (var worksheet in workbook.Worksheets)
{
// 변수 이름, 데이터 타입, 데이터 열거형, 딕셔너리 초기화
var variable = new List<string>(); // 변수명
var dataEnum = new List<string>(); // server와 client를 나눌 기준
var type = new List<string>(); // 변수 타입
var dicViewer = new Dictionary<long, Dictionary<string, object>>();
// 행과 열의 수 얻기
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<string>());
type.Add(worksheet.Cell(4, n + 1).GetValue<string>().ToLower());
dataEnum.Add(worksheet.Cell(3, n + 1).GetValue<string>().ToLower());
}
bool isIndex = variable[0] == "index";
for (int n = 5; n <= vertical + 1; n++)
{
var dataList = new Dictionary<string, object>();
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;
}
}
}