コード例 #1
0
        static void Main(string[] args)
        {
            //http://commandline.codeplex.com/
            var options = new Options();

            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                // Values are available here
                if (options.Verbose)
                {
                    Console.WriteLine("Filename: {0}", options.InputFile);
                }
                //Items?
                if (options.Verbose)
                {
                    Console.WriteLine("Items Count: {0}", options.Items.Count.ToString());
                    options.Items.ToList().ForEach(i => Console.Write("{0}\t", i));
                }
            }
            else
            {
                Console.WriteLine("required options not specified ... quiting.");
                return;
            }

            string txtLocation = Path.GetFullPath(options.InputFile);

            if (options.Verbose)
            {
                Console.WriteLine("Input File Full Path: {0}", txtLocation);
            }
            if (!File.Exists(txtLocation))
            {
                Console.WriteLine("Input File does not exist: {0}", txtLocation);
                return;
            }

            object _missingValue = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Excel.Workbook theWorkbook = excel.Workbooks.Open(txtLocation,
                                                              _missingValue,
                                                              false,
                                                              _missingValue,
                                                              _missingValue,
                                                              _missingValue,
                                                              true,
                                                              _missingValue,
                                                              _missingValue,
                                                              true,
                                                              _missingValue,
                                                              _missingValue,
                                                              _missingValue);


            if (options.Visible)
            {
                excel.Visible = true;
            }

            if (options.All)
            {
                theWorkbook.RefreshAll();
            }
            else
            {
                if (options.Querytables)
                {
                    refreshQueryTables(theWorkbook);
                }
                if (options.Connections)
                {
                    refreshConnection(theWorkbook);
                }
                if (options.Pivottables)
                {
                    refreshPivots(theWorkbook);
                }
            }
            //
            // To test:
            // XLRefreshC.exe -d  -f ..\..\..\Book1.xlsm -m  sheet1.showMessage
            //
            if (options.MacrosToRun != null)
            {
                if (options.Verbose)
                {
                    Console.WriteLine("Macro Count:{0}", options.MacrosToRun.Count().ToString());
                }
                foreach (string macro in options.MacrosToRun)
                {
                    if (options.Verbose)
                    {
                        Console.WriteLine("Macro :{0}", macro);
                    }
                    excel.Run(macro);
                }
            }
            else
            {
                if (options.Verbose)
                {
                    Console.WriteLine("no macro.");
                }
            }


            if (options.Verbose)
            {
                Console.WriteLine("shut it down!");
            }
            excel.Calculate();
            if (options.Verbose)
            {
                Console.WriteLine("Excel calculated");
            }
            theWorkbook.Save();
            if (options.Verbose)
            {
                Console.WriteLine("Workbook saved");
            }
            theWorkbook.Close(true);
            if (options.Verbose)
            {
                Console.WriteLine("Workbook closed");
            }
            excel.Quit();
            if (options.Verbose)
            {
                Console.WriteLine("Excel Quit");
            }
            //Console.WriteLine("Press any key to close...");
            //Console.ReadLine();
            return;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: NinjaWabbit/ExcelRefresh
        static void Main(string[] args)
        {
            //http://commandline.codeplex.com/
            var options = new Options();
            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                // Values are available here
                if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
                //Items?
                if (options.Verbose)
                {
                    Console.WriteLine("Items Count: {0}", options.Items.Count.ToString());
                    options.Items.ToList().ForEach(i => Console.Write("{0}\t", i));
                }
            }
            else
            {
                Console.WriteLine("required options not specified ... quiting.");
                return;
            }

            string txtLocation = Path.GetFullPath(options.InputFile);
            if (options.Verbose) Console.WriteLine("Input File Full Path: {0}", txtLocation);
            if (! File.Exists(txtLocation))
            {
                Console.WriteLine("Input File does not exist: {0}", txtLocation);
                return;
            }

            object _missingValue = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Excel.Workbook theWorkbook = excel.Workbooks.Open(txtLocation,
                                                            _missingValue,
                                                            false,
                                                            _missingValue,
                                                            _missingValue,
                                                            _missingValue,
                                                            true,
                                                            _missingValue,
                                                            _missingValue,
                                                            true,
                                                            _missingValue,
                                                            _missingValue,
                                                            _missingValue);

            if (options.Visible) excel.Visible = true;

            if (options.All)
            {
                theWorkbook.RefreshAll();
            }
            else
            {
                if (options.Querytables) { refreshQueryTables(theWorkbook); }
                if (options.Connections) { refreshConnection(theWorkbook); }
                if (options.Pivottables) { refreshPivots(theWorkbook); }
            }
            //
            // To test:
            // XLRefreshC.exe -d  -f ..\..\..\Book1.xlsm -m  sheet1.showMessage
            //
            if (options.MacrosToRun != null)
            {
                if (options.Verbose) Console.WriteLine("Macro Count:{0}", options.MacrosToRun.Count().ToString());
                foreach (string macro in options.MacrosToRun)
                {
                    if (options.Verbose) Console.WriteLine("Macro :{0}", macro);
                    excel.Run(macro);
                }
            }
            else
            {
                if (options.Verbose) Console.WriteLine("no macro.");
            }

            if (options.Verbose) Console.WriteLine("shut it down!");
            excel.Calculate();
            if (options.Verbose) Console.WriteLine("Excel calculated");
            theWorkbook.Save();
            if (options.Verbose) Console.WriteLine("Workbook saved");
            theWorkbook.Close(true);
            if (options.Verbose) Console.WriteLine("Workbook closed");
            excel.Quit();
            if (options.Verbose) Console.WriteLine("Excel Quit");
            //Console.WriteLine("Press any key to close...");
            //Console.ReadLine();
            return;
        }