Beispiel #1
0
        protected override void Execute(CodeActivityContext context)
        {
            object xlApp      = null;
            string targetFile = TargetExcelFile.Get(context);

            if (!System.IO.Path.IsPathRooted(targetFile))
            {
                targetFile = System.IO.Path.Combine(Environment.CurrentDirectory, targetFile);
            }

            try
            {
                xlApp = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));
                COMUtil.InvokeSetProperty(ref xlApp, "DisplayAlerts", new object[] { false });

                object xlWorkBooks = null;
                try
                {
                    xlWorkBooks = COMUtil.InvokeGetProperty(ref xlApp, "Workbooks");

                    object xlWorkBookObject = null;

                    try
                    {
                        xlWorkBookObject = COMUtil.InvokeByMethod(ref xlWorkBooks, "Open", new object[] {
                            targetFile                                                                                              // FileName
                            , Type.Missing                                                                                          // UpdateLinks
                            , false                                                                                                 // ReadOnly
                            , Type.Missing                                                                                          // Format
                            , !String.IsNullOrEmpty(ExcelBookPassword.Get(context)) ? ExcelBookPassword.Get(context) : Type.Missing // Password
                            , !String.IsNullOrEmpty(ExcelBookPassword.Get(context)) ? ExcelBookPassword.Get(context) : Type.Missing // WriteResPassword
                            , true                                                                                                  // Ignorereadonlyrecommended
                        });

                        COMUtil.InvokeSetProperty(ref xlWorkBookObject, "ReadOnlyRecommended", new object[] { SetReadOnlyRecommended.Get(context) });

                        COMUtil.InvokeByMethod(ref xlWorkBookObject, "Save", null);
                    }
                    finally
                    {
                        COMUtil.COMRelease(ref xlWorkBookObject, false);
                    }
                }
                finally
                {
                    COMUtil.COMRelease(ref xlWorkBooks, false);
                }

                COMUtil.InvokeSetProperty(ref xlApp, "DisplayAlerts", new object[] { true });

                COMUtil.InvokeByMethod(ref xlApp, "Quit", null);
            }
            finally
            {
                COMUtil.COMRelease(ref xlApp, true);
            }
        }
Beispiel #2
0
        protected override void Execute(CodeActivityContext context)
        {
            // Excelの、プロセスがあるか、確認します
            try
            {
                if (System.Diagnostics.Process.GetProcessesByName("excel").Length == 0)
                {
                    throw new Exception();
                }
            }
            catch
            {
                throw new ApplicationException(Properties.Resources.Exception_Excel_Not_Started);
            }

            object xlApp = null;

            try
            {
                xlApp = Marshal.GetActiveObject("Excel.Application");


                object xlActiveSheet = null;

                try
                {
                    xlActiveSheet = COMUtil.InvokeGetProperty(ref xlApp, "ActiveSheet");

                    ActiveSheetName.Set(context, COMUtil.InvokeGetProperty(ref xlActiveSheet, "Name").ToString());
                }
                finally
                {
                    COMUtil.COMRelease(ref xlActiveSheet, false);
                }
            }
            finally
            {
                COMUtil.COMRelease(ref xlApp, true);
            }
        }