Example #1
0
        static int Main(string[] args)
        {
            string file;
            string sheet;
            string rangestart;
            string rangeend;
            string printer           = new PrinterSettings( ).PrinterName;   // Default Printer
            bool   usedefaultprinter = true;

            switch (args.Length)
            {
            case 2:
                file  = args[0];
                sheet = args[1];
                break;

            case 3:
                file              = args[0];
                sheet             = args[1];
                printer           = args[2];
                usedefaultprinter = false;
                break;

            case 4:
                file       = args[0];
                sheet      = args[1];
                rangestart = args[2];
                rangeend   = args[3];
                break;

            case 5:
                file              = args[0];
                sheet             = args[1];
                rangestart        = args[2];
                rangeend          = args[3];
                printer           = args[4];
                usedefaultprinter = false;
                break;

            default:
                return(ShowHelp( ));
            }

            // Check if Excel file exists
            if (!File.Exists(file))
            {
                return(ShowHelp("File not found: \"{0}\"", file));
            }

            // Check if printer exists
            bool validprinter = false;

            foreach (string prn in PrinterSettings.InstalledPrinters)
            {
                if (prn.ToUpper( ) == printer.ToUpper( ))
                {
                    validprinter = true;
                }
            }
            if (!validprinter)
            {
                return(ShowHelp("Printer not found: \"{0}\"", printer));
            }

            // Open Excel
            excelapp         = new Excel.Application( );
            excelapp.Visible = false;
            excelapp.Visible = true;
            excelworkbook    = excelapp.Workbooks.Open(file);

            // Change printer - must be done AFTER opening the excel workbook
            if (!usedefaultprinter)
            {
                // Format printer string for Excel, e.g. "HPLaserJet on Ne01:"
                string prnspl = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices", printer, null).ToString( );
                if (!String.IsNullOrEmpty(prnspl))
                {
                    if (prnspl.IndexOf(',') > 0)
                    {
                        prnspl = prnspl.Substring(prnspl.IndexOf(',') + 1);
                        if (!String.IsNullOrEmpty(prnspl))
                        {
                            printer = String.Format("{0} on {1}", printer, prnspl);
                        }
                    }
                }
                try
                {
                    excelapp.ActivePrinter = printer;
                }
                catch (Exception e)
                {
                    try
                    {
                        excelworkbook.Close( );
                    }
                    catch { }
                    try
                    {
                        excelapp.Quit( );
                    }
                    catch { }
                    return(ShowHelp(e.Message));
                }
            }

            if (Regex.IsMatch(sheet, @"^\d+$"))
            {
                int index = int.Parse(sheet);
                if (index <= excelworkbook.Sheets.Count)
                {
                    ExcelPrint(index);
                }
                else
                {
                    excelworkbook.Close( );
                    excelapp.Quit( );
                    return(ShowHelp("Sheet index out of bound: {0} (maximum for this workbook: {1})", index.ToString( ), excelworkbook.Sheets.Count.ToString( )));
                }
            }
            else
            {
                bool validsheetname = true;
                for (int i = 1; i <= excelworkbook.Sheets.Count; i++)
                {
                    Excel.Worksheet ws = excelworkbook.Worksheets[i];
                    if (ws.Name.ToUpper( ) == sheet.ToUpper( ))
                    {
                        validsheetname = true;
                    }
                }
                if (validsheetname)
                {
                    ExcelPrint(sheet);
                }
                else
                {
                    excelworkbook.Close( );
                    excelapp.Quit( );
                    return(ShowHelp("No matching worksheet found: \"{0}\"", sheet));
                }
            }

            excelworkbook.Close( );
            excelapp.Quit( );

            return(0);
        }