CsServer/Server/Git/ExcelManager.cs

106 lines
3.6 KiB
C#

using Aspose.Cells;
using Microsoft.AspNetCore.Http;
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> _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> type, Dictionary<long, Dictionary<string, object>> dicViewer)
{
this._name = name;
this._variable = variable;
this._type = type;
this._dicViewer = 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 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<long, Dictionary<string, object>> dicViewer = new Dictionary<long, Dictionary<string, object>>();
// 인덱스를 사용하여 워크시트 가져오기
Worksheet worksheet = collection[worksheetIndex];
// 행과 열의 수 얻기
int vertical = worksheet.Cells.MaxDataRow;
int horizontal = worksheet.Cells.MaxDataColumn;
//변수 이름과 타입 삽입
for (int n = 0; n <= horizontal; n++)
{
variable.Add((string)worksheet.Cells[0, n].Value);
type.Add(((string)worksheet.Cells[2, n].Value).ToLower());
}
bool isIndex = variable[0] == "index";
for (int n = 3; n <= vertical; n++)
{
Dictionary<string, object> dataList = new Dictionary<string, object>();
for (int m = 0; m <= horizontal; m++)
{
dataList.Add(variable[m], worksheet.Cells[n, m].Value);
}
dicViewer.Add((int)(isIndex ? worksheet.Cells[n, 0].Value : n - 3), dataList);
}
sheet sheet = new sheet(worksheet.Name, variable, type, dicViewer);
_sheets.Add(sheet);
}
}
catch (Exception ex)
{
logger.Error(ex);
return false;
}
return true;
}
}
}