public static void Print(string fileName, string printerName, PageSetup p) { Excel.Application excelApp = null; Excel.Workbook workBook = null; try { object objOpt = System.Reflection.Missing.Value; excelApp = ExcelInit(); workBook = excelApp.Workbooks.Open(fileName, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt); if (p != null) { //if (p.TopMargin != 0 || p.LeftMargin != 0 || p.RightMargin != 0 || p.BottomMargin != 0) { int sheetCount = workBook.Sheets.Count; Excel.Worksheet workSheet = null; int i = 0; while (i <= sheetCount) { try { workSheet = workBook.Sheets[i]; break; } catch { i++; } } workSheet.PageSetup.LeftMargin = p.GetLeftMargin(); workSheet.PageSetup.RightMargin = p.GetRightMargin(); workSheet.PageSetup.TopMargin = p.GetTopMargin(); workSheet.PageSetup.BottomMargin = p.GetBottomMargin(); //workSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4; } } excelApp.Visible = false; excelApp.ActivePrinter = ConvertToExcelPrinterFriendlyName(printerName); workBook.PrintOut(objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt); } catch (Exception ex) { throw ex; } finally { if (workBook != null) { workBook.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); workBook = null; } ExcelTerminal(excelApp); } }