140 lines
5.6 KiB
C#
140 lines
5.6 KiB
C#
using ClosedXML.Excel;
|
|
using NLog;
|
|
|
|
namespace Server.Git
|
|
{
|
|
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
|
|
{
|
|
private static readonly NLog.ILogger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
List<Sheet> _sheets;
|
|
|
|
public List<Sheet> sheets { get { return _sheets; } }
|
|
|
|
string _pathFile;
|
|
public ExcelManager(string path, string file = "")
|
|
{
|
|
if (file == "")
|
|
_pathFile = path;
|
|
else
|
|
_pathFile = path + "\\" + file;
|
|
}
|
|
|
|
public bool Play()
|
|
{
|
|
return ExcelLoad(_pathFile);
|
|
}
|
|
public bool ExcelLoad(string pathFile)
|
|
{
|
|
// 엑셀 파일을 엽니다.
|
|
try
|
|
{
|
|
using (var workbook = new XLWorkbook(pathFile))
|
|
{
|
|
_sheets = new List<Sheet>();
|
|
|
|
// 모든 워크시트를 반복합니다.
|
|
foreach (var worksheet in workbook.Worksheets)
|
|
{
|
|
// 변수 이름, 데이터 타입, 데이터 열거형, 딕셔너리 초기화
|
|
var variable = new List<string>();
|
|
var dataEnum = new List<string>();
|
|
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((long)(isIndex ? dataList["index"] : n - 4), dataList);
|
|
}
|
|
|
|
var sheet = new Sheet(worksheet.Name, variable, dataEnum, type, dicViewer);
|
|
_sheets.Add(sheet);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 로그 또는 예외 처리
|
|
Console.WriteLine($"Error loading Excel file: {ex.Message}");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
} |