CsServer/Server/Git/ExcelManager.cs

115 lines
4.1 KiB
C#

using Aspose.Cells;
using NLog;
namespace Server.Git
{
class ExcelManager
{
private static readonly NLog.ILogger logger = LogManager.GetCurrentClassLogger();
public class sheet
{
string _name;
public string name { get { return _name; } }
List<string> _variable;
public List<string> variable { get { return _variable; } }
List<string> _type;
public List<string> type { get { return _type; } }
Dictionary<int, List<object>> _dicViewer;
public Dictionary<int, List<object>> dicViewer { get { return _dicViewer; } }
public sheet(string name, List<string> variable, List<string> type, Dictionary<int, List<object>> dicViewer)
{
this._name = name;
this._variable = variable;
this._type = type;
this._dicViewer = dicViewer;
}
}
List<sheet> _sheets;
public List<sheet> sheets { get { return _sheets; } }
string _pathFile;
public ExcelManager(string path, string file)
{
_pathFile = path + "\\" + file;
}
public bool Play()
{
return ExcelLoad(_pathFile);
}
public bool ExcelLoad(string path)//엑셀 로드
{
// 엑셀 파일 불러오기
Workbook wb = new Workbook(_pathFile);
// 모든 워크시트 가져오기
WorksheetCollection collection = wb.Worksheets;
_sheets = new List<sheet>();
// 모든 워크시트 반복
try
{
for (int worksheetIndex = 0; worksheetIndex < collection.Count; worksheetIndex++)
{
//변수이름
List<string> variable = new List<string>();
//변수 타입
List<string> type = new List<string>();
Dictionary<int, List<object>> dicViewer = new Dictionary<int, List<object>>();
// 인덱스를 사용하여 워크시트 가져오기
Worksheet worksheet = collection[worksheetIndex];
// 행과 열의 수 얻기
int vertical = worksheet.Cells.MaxDataRow;
int horizontal = worksheet.Cells.MaxDataColumn;
//변수 이름과 타입 삽입
for (int n = 0; n <= horizontal; n++)
{
// Console.Write(worksheet.Cells[i, j].Value + " | ");
variable.Add((string)worksheet.Cells[0, n].Value);
type.Add(((string)worksheet.Cells[2, n].Value).ToLower());
}
for (int n = 3; n <= vertical; n++)
{
List<object> dataList = new List<object>();
for (int m = 0; m <= horizontal; m++)
{
switch (type[m])
{
case "enum":
case "int":
dataList.Add(worksheet.Cells[n, m].Value);
break;
case "string":
case "text":
case "time":
dataList.Add(worksheet.Cells[n, m].Value);
break;
default:
Console.WriteLine("알수없는 타입의 메세지 입니다.");
break;
}
}
dicViewer.Add(n, dataList);
}
sheet sheet = new sheet(worksheet.Name, variable, type, dicViewer);
_sheets.Add(sheet);
}
}
catch (Exception ex)
{
logger.Error(ex);
return false;
}
return true;
}
}
}