58 lines
2.1 KiB
C#
58 lines
2.1 KiB
C#
// Copyright (c) 2017- Informatikbüro Daniel Schick
|
|
// Description: Controller class for reading excel sheets (label-based)
|
|
|
|
using bsmd.database;
|
|
using log4net;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace ENI2.Import
|
|
{
|
|
public class ExcelManager
|
|
{
|
|
private readonly ILog _log = LogManager.GetLogger(typeof(ExcelManager));
|
|
|
|
public ExcelManager()
|
|
{
|
|
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
|
|
}
|
|
|
|
public bool Import(string filePath, MessageCore core, List<Message.NotificationClass> classes, out string readMessage)
|
|
{
|
|
bool result = false;
|
|
try
|
|
{
|
|
using (ExcelReader reader = new ExcelReader(filePath))
|
|
{
|
|
ImportHeader importHeader = new ImportHeader
|
|
{
|
|
ImportDate = DateTime.Now
|
|
};
|
|
result = ExcelUtil.ProcessSheet(reader, out readMessage, core, classes); // actual import
|
|
if (result)
|
|
{
|
|
importHeader.MessageCoreId = core.Id.Value;
|
|
importHeader.Filename = Path.GetFileName(filePath);
|
|
ReportingParty thisUser = ReportingParty.CurrentReportingParty;
|
|
importHeader.SenderEmail = thisUser.EMail;
|
|
importHeader.ReportingPartyId = thisUser.Id;
|
|
|
|
DBManager.Instance.Save(importHeader);
|
|
// Saving plaintext data (ImportHeader + ImportValues) for each reading
|
|
List<ImportValue> valueList = importHeader.CreateUpdateList(reader.ImportValues);
|
|
// Bulk save recommended here..
|
|
ImportValue.BulkSave(valueList);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
readMessage = ex.Message;
|
|
_log.Error(ex);
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|