Excel中设置打印格式

在2007中,exclel文档会保存自身定义的打印设置而非读取打印机的设置,所以可以通过以下方法来修改excel的打印设置。

 

  /// <summary>
        ///设置excel文档的页面大小,打印方向
        /// </summary>
        /// <param name="fineName"></param>
        public void SetExcel(string fileName, string paperSize, bool isLandScape)
        {
            try
            {
                Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                excel.Visible = false;
                excel.DisplayAlerts = false;
                _Workbook workbook = excel.Workbooks.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                Sheets sheets = workbook.Worksheets;
                _Worksheet sheet = (_Worksheet)sheets.get_Item(1);
                switch (paperSize)
                {
                    case "A4":
                        sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;
                        break;
                    case "Letter":
                        sheet.PageSetup.PaperSize = XlPaperSize.xlPaperLetter;
                        break;
                    default:
                        break;
                }
                if (isLandScape == true)
                {
                    sheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
                }
                else
                {
                    sheet.PageSetup.Orientation = XlPageOrientation.xlPortrait;
                }
                workbook.Close(true, missing, missing);
                excel.Workbooks.Close();
                excel.Application.Quit();
                excel.Quit();
                Kill(excel);

                #region //此处也可以直接用pdfmaker生成pdf文件,但是经过测试pdf生成速度比直接打印要慢
                //PDFMAKERAPILib.PDFMakerApp pdfMaker = new PDFMAKERAPILib.PDFMakerApp();
                //FileInfo fileInfo = new FileInfo(fileName);
                //string outputFileName = fileName.Replace(fileInfo.Extension, ".pdf");
                //pdfMaker.CreatePDF(fileName, outputFileName, PDFMAKERAPILib.PDFMakerSettings.kConvertAllPages, false, false, false, missing);
                #endregion
            }
            catch (Exception e)
            {
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        /// <summary>
        /// 杀掉当前excel进程
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        public static void Kill(Microsoft.Office.Interop.Excel.ApplicationClass excel)
        {
            IntPtr t = new IntPtr(excel.Hwnd);   //得到这个句柄,具体作用是得到这块内存入口
            int k = 0;
            GetWindowThreadProcessId(t, out k);   //得到本进程唯一标志k
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);   //得到对进程k的引用
            p.Kill();     //关闭进程
        }  
 

posted on 2008-08-21 15:03  正在输入  阅读(2920)  评论(0编辑  收藏  举报

导航