private static IEnumerable <IExternalExcelApplicationLoader> GetAssemblyLoaders()
        {
            ModuleProc PROC = new ModuleProc("", "GetAssemblyLoaders");
            IList <IExternalExcelApplicationLoader> loaders = new List <IExternalExcelApplicationLoader>();

            try
            {
                string assemblyNames = Extensions.GetAppSettingValue("ExternalExcelApplicationLoaders", "BMC.ExcelLibrary.dll;ClosedXML.dll");
                if (!assemblyNames.IsEmpty())
                {
                    string[] assemblies = assemblyNames.Split(';');
                    if (assemblies != null)
                    {
                        foreach (var assembly in assemblies)
                        {
                            string asmPath = Path.Combine(Extensions.GetStartupDirectory(), assembly);
                            Log.Info(PROC, "Excel Assembly Loader : " + asmPath);
                            if (File.Exists(asmPath))
                            {
                                try
                                {
                                    Assembly asm = Assembly.LoadFrom(asmPath);
                                    if (asm != null)
                                    {
                                        var loaderType = (from t in asm.GetTypes()
                                                          where typeof(IExternalExcelApplicationLoader).IsAssignableFrom(t)
                                                          select t).FirstOrDefault();
                                        if (loaderType != null)
                                        {
                                            IExternalExcelApplicationLoader loader = Activator.CreateInstance(loaderType) as IExternalExcelApplicationLoader;
                                            if (loader != null)
                                            {
                                                loaders.Add(loader);
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log.Exception(PROC, ex);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }

            return(loaders);
        }
        public static IExternalExcelApplication Create()
        {
            ModuleProc PROC = new ModuleProc("ExternalExcelLibraryFactory", "Create");
            IExternalExcelApplication result = default(IExternalExcelApplication);

            try
            {
                if (_activeLibraryLoader == null)
                {
                    lock (_librariesLock)
                    {
                        if (_activeLibraryLoader == null)
                        {
                            try
                            {
                                IEnumerable <IExternalExcelApplicationLoader> libraryLoaders = null;

                                try
                                {
                                    libraryLoaders = MEFHelper.GetExportedValues <IExternalExcelApplicationLoader>();
                                }
                                catch
                                {
                                    libraryLoaders = GetAssemblyLoaders();
                                }
                                if (libraryLoaders != null)
                                {
                                    _libraryLoaders = (from l in libraryLoaders
                                                       orderby l.LibraryOrder
                                                       select l).ToArray();

                                    if (_libraryLoaders != null)
                                    {
                                        // load the first matched library until find valid one
                                        foreach (IExternalExcelApplicationLoader libraryLoader in _libraryLoaders)
                                        {
                                            if (libraryLoader != null)
                                            {
                                                if (libraryLoader.IsLoadedProperly)
                                                {
                                                    Log.Info(PROC, "Excel Library : " + libraryLoader.LibraryName + " (Loaded)");
                                                    _activeLibraryLoader = libraryLoader;
                                                    break;
                                                }
                                                else
                                                {
                                                    Log.Info(PROC, "Excel Library : " + libraryLoader.LibraryName + " (Unable to load)");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Exception(PROC, ex);
                            }
                        }
                    }
                }

                // still no external loaders found (then take ExcelPackage as a rescuer)
                if (_activeLibraryLoader == null)
                {
                    _activeLibraryLoader = new OpenXmlExcelApplicationLoader();
                }

                if (_activeLibraryLoader != null)
                {
                    IExternalExcelApplication library = null;

                    try
                    {
                        library = _activeLibraryLoader.Create();
                    }
                    catch { library = null; }

                    if (library != null)
                    {
                        result = library;
                        Log.Info(PROC, "Excel Library : " + _activeLibraryLoader.LibraryName + " (Created)");
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }

            return(result);
        }