156 lines
5.8 KiB
C#
156 lines
5.8 KiB
C#
using log4net;
|
|
using Microsoft.Office.Interop.Excel;
|
|
using System.Runtime.InteropServices;
|
|
using System;
|
|
using System.Drawing;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bsmd.ExcelReadService
|
|
{
|
|
/// <summary>
|
|
/// Kapselt die Implementierung / Schreiben des Bestätigungs-Sheets. Bisher wurde das Highlighting direkt in das ursprüngl. Sheet
|
|
/// übernommen. Da die Bestätigung aber anders aussieht und aus mehreren Sheets bestehen kann ist das jetzt abgetrennt in einer
|
|
/// eignenen Klasse. Der Pfad zu den Templates ist in den Settings hinterlegt, die Klasse wird mit der entspr. Collection aufgerufen
|
|
/// Das wiederum ergibt sich aus dem Zielhafen in PoC.
|
|
/// </summary>
|
|
internal class Confirmation : IDisposable
|
|
{
|
|
private ILog _log = LogManager.GetLogger(typeof(Confirmation));
|
|
|
|
private int okColor = ColorTranslator.ToOle(Color.FromArgb(200, 255, 200)); // light green
|
|
private int warnColor = ColorTranslator.ToOle(Color.FromArgb(255, 255, 200)); // yellow
|
|
private int failColor = ColorTranslator.ToOle(Color.FromArgb(255, 150, 150)); // light red
|
|
private int whiteColor = ColorTranslator.ToOle(Color.White);
|
|
|
|
private List<Workbook> workbooks = new List<Workbook>();
|
|
private List<Dictionary<string, Name>> nameDicts = new List<Dictionary<string, Name>>();
|
|
private List<string> templateNames = new List<string>();
|
|
|
|
#region Construction
|
|
|
|
public Confirmation(System.Collections.Specialized.StringCollection sheetCollection, Application excelInstance)
|
|
{
|
|
foreach(string template in sheetCollection)
|
|
{
|
|
try
|
|
{
|
|
// die Templates sollten echte Excel Templates (.xlst) sein
|
|
Workbook aWorkbook = excelInstance.Workbooks.Open(template, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, false, false, false);
|
|
Dictionary<string, Name> nameDict = new Dictionary<string, Name>();
|
|
foreach (Name name in aWorkbook.Names)
|
|
{
|
|
nameDict[name.Name] = name;
|
|
}
|
|
workbooks.Add(aWorkbook);
|
|
nameDicts.Add(nameDict);
|
|
templateNames.Add(Path.GetFileNameWithoutExtension(template));
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
_log.ErrorFormat("Failure creating sheet from template {0}:{1}", template, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region public
|
|
|
|
public void ConfirmText(string lookup, string value, ExcelReader.ReadState state)
|
|
{
|
|
this.ConfirmValue(lookup, value, state);
|
|
}
|
|
|
|
public void ConfirmNumber(string lookup, double? value, ExcelReader.ReadState state)
|
|
{
|
|
this.ConfirmValue(lookup, value, state);
|
|
}
|
|
|
|
public void ConfirmDate(string lookup, DateTime? value, ExcelReader.ReadState state)
|
|
{
|
|
this.ConfirmValue(lookup, value, state);
|
|
}
|
|
|
|
public void ConfirmTime(string lookup, DateTime? value, ExcelReader.ReadState state)
|
|
{
|
|
this.ConfirmValue(lookup, value, state);
|
|
}
|
|
|
|
public List<string> SaveConfirmationSheets(string receivedFileName)
|
|
{
|
|
|
|
List<string> result = new List<string>();
|
|
|
|
for(int i=0;i<this.workbooks.Count;i++)
|
|
{
|
|
// construct file path
|
|
string fileNameWithPath = Path.Combine(Path.GetDirectoryName(receivedFileName),
|
|
string.Format("{0}_{1}.xlsx", this.templateNames[i], Path.GetFileNameWithoutExtension(receivedFileName)));
|
|
this.workbooks[i].SaveAs(fileNameWithPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
|
|
Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
|
|
Type.Missing, Type.Missing);
|
|
this.workbooks[i].Saved = true;
|
|
result.Add(fileNameWithPath);
|
|
this.workbooks[i].Close();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
{
|
|
for (int i = 0; i < this.workbooks.Count; i++)
|
|
{
|
|
this.workbooks[i].Close();
|
|
this.workbooks[i] = null;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region private
|
|
|
|
private void ConfirmValue(string lookup, object value, ExcelReader.ReadState state)
|
|
{
|
|
for (int i = 0; i < this.nameDicts.Count; i++)
|
|
{
|
|
if (this.nameDicts[i].ContainsKey(lookup))
|
|
{
|
|
Range range = this.nameDicts[i][lookup].RefersToRange;
|
|
if (range != null)
|
|
{
|
|
range.Interior.Color = this.ColorForState(state);
|
|
range.Value = value;
|
|
}
|
|
Marshal.ReleaseComObject(range);
|
|
}
|
|
}
|
|
}
|
|
|
|
private int ColorForState(ExcelReader.ReadState state)
|
|
{
|
|
switch (state)
|
|
{
|
|
case ExcelReader.ReadState.FAIL:
|
|
return this.failColor;
|
|
case ExcelReader.ReadState.WARN:
|
|
return this.warnColor;
|
|
case ExcelReader.ReadState.OK:
|
|
return this.okColor;
|
|
case ExcelReader.ReadState.NONE:
|
|
default:
|
|
break;
|
|
}
|
|
return this.whiteColor;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|