WOrd ,excel实现打印功能

本文介绍了一种使用C#代码实现Word文档打印的方法。通过调用Microsoft.Office.Interop.Word库,可以实现选择文件并设置打印参数的功能,包括指定打印机名称及打印份数等。

            PrintDocument print = new PrintDocument();
            string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
            string s = "";
            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                s = sPrint;
            }
            return;
            OpenFileDialog fileDialog = new OpenFileDialog();
            //fileDialog.Title = "请选择文件";
            //fileDialog.Filter = "excel03文件(*.xls)|*.xls";
            //fileDialog.FilterIndex = 1;
            //fileDialog.RestoreDirectory = true;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                #region excel
                //Microsoft.Office.Interop.Excel.ApplicationClass xlsApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                //xlsApp.Visible = true;
                //object oMissing = System.Reflection.Missing.Value;
                //Microsoft.Office.Interop.Excel.Workbook xlsWorkbook = xlsApp.Workbooks.Open(fileDialog.FileName, 0, true, 5, oMissing, oMissing, true, 1, oMissing, false, false, oMissing, false, oMissing, oMissing);
                //Microsoft.Office.Interop.Excel.Worksheet xlsWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlsWorkbook.Worksheets[1];
                ////xlsWorksheet.PrintPreview(null);
                //xlsWorksheet.PrintOut(1, 2, 1, false, oMissing, true, false, oMissing);
                //xlsApp.Visible = false;
                //xlsWorksheet = null;
                #endregion
                #region word
                object wordFile = fileDialog.FileName;
                object oMissing = Missing.Value;
                object copies = "1";
                object oTrue = true;
                object oFalse = false;
                object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                appWord.Visible = false;
                appWord.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
                string defaultPrinter = appWord.ActivePrinter;
                Microsoft.Office.Interop.Word.Document doc = appWord.Documents.Open(
                    ref wordFile ,
                    ref oMissing,
                    ref oTrue ,
                    ref oFalse ,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing);
                //弹出打印设置
                //appWord.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(ref oMissing);
                //appWord.Visible = false;
                appWord.ActivePrinter = "hp3030 ";
                doc.PrintOut(
                    ref oTrue,
                    ref oFalse,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref copies,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing);
                appWord.ActivePrinter = defaultPrinter;
                doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
                appWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                doc = null;
                appWord = null;
                GC.Collect();
                #endregion word

 

            }

转载于:https://www.cnblogs.com/swarb/archive/2012/04/11/9924430.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值