// Copyright (c) 2017 Informatikbüro Daniel Schick // using log4net; using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Runtime.InteropServices; namespace bsmd.ExcelReadService { /// /// 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. /// internal class Confirmation : IDisposable { private static 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 workbooks = new List(); private List>> nameDicts = new List>>(); private List templateNames = new List(); #region Construction public Confirmation(System.Collections.Specialized.StringCollection sheetCollection, Application excelInstance) { string nameDictKey = ""; foreach (string template in sheetCollection) { try { nameDictKey += template; // 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); /* if (aWorkbook == excelInstance.ActiveWorkbook) _log.Info("aWorkbook is active workbook"); else aWorkbook.Activate(); */ Dictionary> nameDict = new Dictionary>(); // hier müsste etwas hin das nur aus der eben geladenen Vorlage die Namen rauszieht.. // eben nicht aus *allen* Vorlagen. Das funktioniert auch nur liefern einige der Names keine Ranges.. foreach(Name workbookName in aWorkbook.Names) { // _log.Info(workbookName.Name); if (workbookName.Name.Contains("!")) { continue; } else { if (!nameDict.ContainsKey(workbookName.Name)) nameDict[workbookName.Name] = new List(); nameDict[workbookName.Name].Add(workbookName); } } 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 void HighlightCellWithState(string lookup, ExcelReader.ReadState state) { for (int i = 0; i < this.workbooks.Count; i++) { Workbook workbook = this.workbooks[i]; Dictionary> nameDict = this.nameDicts[i]; if (nameDict.ContainsKey(lookup)) { try { foreach (Name someName in nameDict[lookup]) { Range range = someName.RefersToRange; if (range != null) { range.Interior.Color = this.ColorForState(state); } Marshal.ReleaseComObject(range); } } catch (Exception ex) { _log.WarnFormat("cannot set highlight {0} for lookup {1}: {2}", state, lookup, ex.Message); } } } } public List SaveConfirmationSheets(string receivedFileName) { List result = new List(); for(int i=0;i> nameDict = this.nameDicts[i]; if(nameDict.ContainsKey(lookup)) { try { foreach (Name someName in nameDict[lookup]) { Range range = someName.RefersToRange; if (range != null) { range.Interior.Color = this.ColorForState(state); range.Value = value; } Marshal.ReleaseComObject(range); // _log.InfoFormat("Value {0} for lookup {1} OK", value, lookup); } } catch(Exception ex) { _log.WarnFormat("cannot set value {0} for lookup {1}: {2}", value, lookup, ex.Message); } } } } 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 } }