Пример #1
0
 /// <summary>
 /// 打印当前工作表
 /// </summary>
 public void PrintOut()
 {
     xSheet.PrintOut(System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                     System.Reflection.Missing.Value, false, System.Reflection.Missing.Value,
                     false, false, System.Reflection.Missing.Value);
     //Excel.Application.DoEvents();
 }
Пример #2
0
        void PrintMyExcelFile(string sFileName)
        {
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

            // Open the Workbook:
            Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(sFileName, 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];

            var _with1 = ws.PageSetup;

            // A4 papersize
            _with1.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4;
            // Landscape orientation
            _with1.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
            // Fit Sheet on One Page
            _with1.FitToPagesWide = 1;
            _with1.FitToPagesTall = 1;
            // Normal Margins
            _with1.LeftMargin   = excelApp.InchesToPoints(0.7);
            _with1.RightMargin  = excelApp.InchesToPoints(0.7);
            _with1.TopMargin    = excelApp.InchesToPoints(0.75);
            _with1.BottomMargin = excelApp.InchesToPoints(0.75);
            _with1.HeaderMargin = excelApp.InchesToPoints(0.3);
            _with1.FooterMargin = excelApp.InchesToPoints(0.3);
            object misValue = System.Reflection.Missing.Value;

            // 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);
        }