Beispiel #1
0
        public void OpenFile(string filePath, string personalWorkbookPath)
        {
            if (filePath.IsNullOrEmpty())
            {
                throw new ArgumentNullException("filePath");
            }

            Excel.Application excel     = null;
            Excel.Workbooks   workbooks = null;

            try
            {
                excel = new Excel.Application {
                    Visible = true
                };
                workbooks = excel.Workbooks;
                workbooks.Open(filePath);

                foreach (Excel.AddIn addin in excel.AddIns)
                {
                    if (addin.Installed)
                    {
                        addin.Installed = false;
                        addin.Installed = true;
                    }

                    Marshal.FinalReleaseComObject(addin);
                }

                if (personalWorkbookPath.IsNullOrEmpty())
                {
                    return;
                }

                workbooks.Open(personalWorkbookPath);
            }
            finally
            {
                if (excel != null)
                {
                    Marshal.FinalReleaseComObject(excel);
                }

                if (workbooks != null)
                {
                    Marshal.FinalReleaseComObject(workbooks);
                }
            }
        }
        public void OpenFile(string filePath, string personalWorkbookPath)
        {
            if (filePath.IsNullOrEmpty())
                throw new ArgumentNullException("filePath");

            Excel.Application excel = null;
            Excel.Workbooks workbooks = null;

            try
            {
                excel = new Excel.Application { Visible = true };
                workbooks = excel.Workbooks;
                workbooks.Open(filePath);

                foreach (Excel.AddIn addin in excel.AddIns)
                {
                    if (addin.Installed)
                    {
                        addin.Installed = false;
                        addin.Installed = true;
                    }

                    Marshal.FinalReleaseComObject(addin);
                }

                if (personalWorkbookPath.IsNullOrEmpty())
                    return;

                workbooks.Open(personalWorkbookPath);
            }
            finally
            {
                if (excel != null)
                    Marshal.FinalReleaseComObject(excel);

                if (workbooks != null)
                    Marshal.FinalReleaseComObject(workbooks);
            }
        }