155 lines
5.0 KiB
C#
155 lines
5.0 KiB
C#
// Copyright (c) 2017- schick Informatik
|
|
// Description:
|
|
//
|
|
|
|
using Microsoft.Office.Interop.Excel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
using bsmd.database;
|
|
|
|
namespace ENI2.Excel
|
|
{
|
|
internal class ExcelWriter : ExcelBase
|
|
{
|
|
|
|
#region Fields
|
|
|
|
private readonly string _saveFilePath;
|
|
|
|
#endregion
|
|
|
|
#region Construction
|
|
|
|
public ExcelWriter(string filePath) : base(filePath)
|
|
{
|
|
string refFilePath = @"Excel\Reference_Sheet_DE.xlsx";
|
|
this._workBook = _excelWorkbooks.Open(refFilePath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", false, false, 0, false, false, false);
|
|
|
|
this.InitNameFields();
|
|
_saveFilePath = filePath;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region public methods
|
|
|
|
public void WriteData(List<Message> messages, MessageCore core, out string resultMessage)
|
|
{
|
|
resultMessage = "";
|
|
|
|
foreach (Message message in messages)
|
|
{
|
|
try
|
|
{
|
|
this.WriteMessage(message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
resultMessage += string.Format("{2}:{0}{1}", ex.Message, Environment.NewLine, message.MessageNotificationClassDisplay);
|
|
}
|
|
}
|
|
|
|
WriteCore(core);
|
|
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
this._workBook.SaveAs(_saveFilePath, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
|
|
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
|
|
this._workBook.Saved = true;
|
|
this._workBook.Close();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region private excel field writing
|
|
|
|
private void WriteMessage(DatabaseEntity dbEntity)
|
|
{
|
|
Type objType = dbEntity.GetType();
|
|
List<PropertyInfo> props = new List<PropertyInfo>();
|
|
|
|
// add lookup properties to scan list
|
|
props.AddRange(objType.GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(LookupNameAttribute))));
|
|
|
|
foreach (PropertyInfo property in props)
|
|
{
|
|
object propValue = property.GetValue(dbEntity, null);
|
|
string value = (propValue == null) ? string.Empty : propValue.ToString();
|
|
LookupNameAttribute lookupNameAttribute = Attribute.GetCustomAttribute(property, typeof(LookupNameAttribute)) as LookupNameAttribute;
|
|
bool success = true;
|
|
|
|
if (property.PropertyType == typeof(DateTime?))
|
|
{
|
|
success = this.WriteDate(lookupNameAttribute.LookupName, property.GetValue(dbEntity));
|
|
}
|
|
else if (property.PropertyType == typeof(double?))
|
|
{
|
|
success = this.WriteNumber(lookupNameAttribute.LookupName, property.GetValue(dbEntity));
|
|
}
|
|
else if (property.PropertyType == typeof(string))
|
|
{
|
|
success = this.WriteText(lookupNameAttribute.LookupName, property.GetValue(dbEntity));
|
|
}
|
|
else if (property.PropertyType == typeof(int?))
|
|
{
|
|
success = this.WriteNumber(lookupNameAttribute.LookupName, property.GetValue(dbEntity));
|
|
}
|
|
else if (property.PropertyType == typeof(byte?))
|
|
{
|
|
success = this.WriteNumber(lookupNameAttribute.LookupName, property.GetValue(dbEntity));
|
|
}
|
|
else if (property.PropertyType == typeof(Boolean?))
|
|
{
|
|
success = this.WriteBoolean(lookupNameAttribute.LookupName, property.GetValue(dbEntity));
|
|
}
|
|
else
|
|
{
|
|
string message = string.Format("unhandled property type: {0} for lookup {1}", property.PropertyType, lookupNameAttribute.LookupName);
|
|
_log.Warn(message);
|
|
}
|
|
|
|
if(!success)
|
|
{
|
|
string message = string.Format("Sheet does not contain lookup field {0}", lookupNameAttribute.LookupName);
|
|
_log.Error(message);
|
|
throw new FormatException(message);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void WriteCore(MessageCore core)
|
|
{
|
|
|
|
}
|
|
|
|
private bool WriteBoolean(string lookupName, object v)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private bool WriteText(string lookupName, object v)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private bool WriteNumber(string lookupName, object v)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private bool WriteDate(string lookupName, object v)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|