public static void PrintPreview(string fileName, 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 = true; workBook.PrintPreview(objOpt); excelApp.Visible = false; } catch (Exception ex) { throw ex; } finally { if (workBook != null) { workBook.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); workBook = null; } ExcelTerminal(excelApp); } }
private void butprint_pos_Click(object sender, EventArgs e) { Save(); String ExcelFile = Application.StartupPath + "\\门诊预交金收支情况统计.xls"; //Microsoft.Office.Interop.Excel.ApplicationClass xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); //xlApp.Visible = true; //object oMissing = System.Reflection.Missing.Value; //Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(ExcelFile, 0, true, 5, oMissing, oMissing, true, 1, oMissing, false, false, oMissing, false, oMissing, oMissing); //Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets[1]; //xlWorksheet.PrintPreview(null); //xlApp.Visible = false; //xlWorksheet = null; //xlWorksheet.PrintOut(1, 1, 1, Missing.Value, Missing.Value, Missing.Value, Missing.Value, // Missing.Value); //return; Excel.Application xlsApp = new Excel.Application(); //xlsApp.Visible = true; Excel.Workbooks xlsWbs = xlsApp.Workbooks; Excel.Workbook xlsWb = xlsWbs.Open( ExcelFile, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Excel.Worksheet xlsWs = (Excel.Worksheet)xlsWb.Worksheets[1]; //if (checkBox1.Checked) // xlsWs.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;//页面方向横向 //else xlsWs.PageSetup.Orientation = Excel.XlPageOrientation.xlPortrait; //页面方向横向 //objsheet.PageSetup.Zoom = 75;//打印时页面设置,缩放比例 //objsheet.PageSetup.TopMargin = 0; //上边距为0 //objsheet.PageSetup.BottomMargin = 0; //下边距为0 //objsheet.PageSetup.LeftMargin = 0; //左边距为0 //objsheet.PageSetup.RightMargin = 0; //右边距为0 //objsheet.PageSetup.CenterHorizontally = true;//水平居中 //PaperSize p = null; //PrintDialog pd = new PrintDialog(); //foreach (PaperSize ps in pd.PrinterSettings.PaperSizes) //{ // if (ps.PaperName.Equals("A4 Plus")) // p = ps; //} //xlsWs.PageSetup.PaperSize = (Excel.XlPaperSize)p.RawKind; //使excel可见 xlsApp.Visible = true; //预览 xlsWb.PrintPreview(false); //保存后退出,并释放资源 xlsApp.DisplayAlerts = false; // xlsWb.Save(); // xlsWb.SaveAs(ExcelFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, // Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, // Missing.Value, Missing.Value, Missing.Value, Missing.Value); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWs); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWb); xlsWs = null; xlsWb = null; xlsApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp); xlsApp = null; //xlsApp.Visible = true; //打印 //xlsWb.PrintOut(1, 1, 1, Missing.Value, Missing.Value, Missing.Value, Missing.Value, // Missing.Value); //保存后退出,并释放资源 // xlsApp.DisplayAlerts = false; // xlsWb.Save(); //xlsWb.SaveAs(ExcelFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, // Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, // Missing.Value, Missing.Value, Missing.Value, Missing.Value); xlsWs = null; xlsWb = null; //xlsApp.Quit(); xlsApp = null; GC.Collect(); }