void PrintMyExcelFile(string ruta) { Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); // Open the Workbook: Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open( ruta, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // Get the first worksheet. // (Excel uses base 1 indexing, not base 0.) Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]; // Print out 1 copy to the default printer: ws.PrintOut( Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // Cleanup: GC.Collect(); GC.WaitForPendingFinalizers(); Marshal.FinalReleaseComObject(ws); wb.Close(false, Type.Missing, Type.Missing); Marshal.FinalReleaseComObject(wb); excelApp.Quit(); Marshal.FinalReleaseComObject(excelApp); }
public void Print(YellowstonePathology.Business.Search.ReportSearchList caseList, string description, DateTime printDate) { Microsoft.Office.Interop.Excel.Application xlApp; xlApp = new Microsoft.Office.Interop.Excel.Application(); xlApp.Visible = false; Microsoft.Office.Interop.Excel.Workbook wb = xlApp.Workbooks.Add(@"\\CFileServer\documents\ReportTemplates\MolecularTesting\CaseList.xlt"); Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]; ws.Cells[3, 1] = "Batch: " + description + " - " + printDate.ToShortDateString(); int rowPosition = 6; for (int i = caseList.Count - 1; i > -1; i--) { ws.Cells[rowPosition, 1] = caseList[i].ReportNo; ws.Cells[rowPosition, 2] = caseList[i].PanelSetName; ws.Cells[rowPosition, 3] = caseList[i].PatientName; ws.Cells[rowPosition, 4] = caseList[i].PhysicianName + " - " + caseList[i].ClientName; ws.Cells[rowPosition, 5] = caseList[i].OrderedBy; rowPosition++; } Object oMissing = Type.Missing; Object oFalse = false; ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); wb.Close(oFalse, oMissing, oMissing); xlApp.Quit(); }
private static void printExcel(string excelPath) { Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.Application(); xApp.Visible = false; Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks._Open(excelPath); Microsoft.Office.Interop.Excel.Worksheet xSheet = null; try { try { xApp.Run("Workbook_Open"); } catch (Exception) { } xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.ActiveSheet; xSheet.PrintOut(1, 1, 1, false); } catch (Exception e) { throw e; } finally { xBook.Close(false); xApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xApp); xSheet = null; xBook = null; xApp = null; GC.Collect(); } }
public static void PrintOrder(string fileUrl, IWin32Window win32Window, string printer) { KillProcess("EXCEL"); Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook myBook = null; Microsoft.Office.Interop.Excel.Worksheet mySheet1 = null; Object missing = System.Reflection.Missing.Value; Object defaultPrint = missing; //string print = Settings.Default.DefaultPrintName1; if (printer != null && printer != string.Empty) { defaultPrint = printer; } try { myBook = myExcel.Workbooks.Open(fileUrl, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //handle sheets mySheet1 = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Sheets[1]; mySheet1.PrintOut(missing, missing, missing, missing, defaultPrint, missing, missing, missing); } catch (Exception e) { string errorMsg = "打印失败,重打请按CTRL+P!错误信息:" + e.Message; MessageBox.Show(win32Window, errorMsg, "打印失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (myBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook); } if (mySheet1 != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet1); } myBook = null; mySheet1 = null; myExcel.Quit(); GC.Collect(); } }
public void PrintExcel() { //var printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters; Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(newFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]; ws.PageSetup.PrintHeadings = false; ws.PageSetup.BlackAndWhite = false; ws.PageSetup.PrintGridlines = false; ws.PageSetup.Zoom = false; ws.PageSetup.FitToPagesWide = 1; ws.PageSetup.FitToPagesTall = 1; ws.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait; if (this.paper_type == "A4") { ws.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4; } else { ws.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA5; } ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, this.printObj, Type.Missing, Type.Missing, Type.Missing); // Type.Missing app.Visible = false; wb.Save(); wb.Close(false, Type.Missing, Type.Missing); app.Quit(); }
public void PrintExcel() { //var printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters; Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(newFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]; try { ws.PageSetup.PrintHeadings = false; ws.PageSetup.BlackAndWhite = false; ws.PageSetup.PrintGridlines = false; ws.PageSetup.Zoom = false; ws.PageSetup.FitToPagesWide = 1; ws.PageSetup.FitToPagesTall = 1; ws.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait; if (this.paper_type == "A4") { ws.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4; } else if (paper_type == "A5") { ws.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA5; } else if (paper_type == "A3") { ws.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA3; } //else if (paper_type == "A6") //{ // ws.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaper10x14; //} else { ws.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4; } ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, this.printObj, Type.Missing, Type.Missing, Type.Missing); // Type.Missing app.Visible = false; wb.Save(); wb.Close(false, Type.Missing, Type.Missing); app.Quit(); Marshal.FinalReleaseComObject(ws); Marshal.FinalReleaseComObject(wb); Marshal.FinalReleaseComObject(app); } catch (Exception ex) { Marshal.FinalReleaseComObject(ws); Marshal.FinalReleaseComObject(wb); Marshal.FinalReleaseComObject(app); Console.WriteLine(ex.Message); this.OK = false; } }