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

192 lines
6.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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; } }
#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(List<string> fileNames)
{
ExcelManager excel = new ExcelManager();
excel.Add(fileNames);
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(List<string> files)
{
foreach (string file in files)
{
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;
}
}
}