C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码

打印,是做开发的人的经久不变的话题。

今天,用实例代码,说明.NET是如何打印WORD、EXCEL等OFFICE文件,以及PDF文件的。

采用指定的打印机打印OFFICE文件

此方法又分为 “显示相应的程序窗口” 和 “不显示相应的程序窗口”两种方式。

(1) 显示WORD、EXCEL等程序窗口

采用操作系统自身的自动识别模式的打印,此方法实际适用于N多种文件,并不限于WORD,EXCEL,PDF之类的,但是这种方法,有一个缺陷,就是:对于某些类型的文档,如WORD,EXCEL,PDF等,打印时,会有相应的程序窗口一闪而过

实现代码如下:

System.Diagnostics.Process p = new System.Diagnostics.Process();
//不现实调用程序窗口,但是对于某些应用无效
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

//采用操作系统自动识别的模式
p.StartInfo.UseShellExecute = true;

//要打印的文件路径,可以是WORD,EXCEL,PDF,TXT等等
p.StartInfo.FileName = @"d:\a.doc";

//指定执行的动作,是打印,即print,打开是 open
p.StartInfo.Verb = "print";

//开始
p.Start();

此种方法,代码简单,性能好,可靠稳定。此种方式,如果要指定打印机,则只能利用设置默认打印机的方式来实现。

C#设置系统默认打印机的实现方法,见 “C#获取和设置系统的默认打印机” 一文

添加了指定打印机功能的代码如下:

System.Diagnostics.Process p = new System.Diagnostics.Process();
//不现实调用程序窗口,但是对于某些应用无效
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

//采用操作系统自动识别的模式
p.StartInfo.UseShellExecute = true;

//要打印的文件路径
p.StartInfo.FileName = @"d:\a.doc";

//指定执行的动作,是打印,即print,打开是 open
p.StartInfo.Verb = "print";

//获取当前默认打印机

//string defaultPrinter = GetDefaultPrinter();

//将指定的打印机设为默认打印机
SetDefaultPrinter("指定的打印机");

//开始打印
p.Start();

//等待十秒
p.WaitForExit(10000);

//将默认打印机还原
SetDefaultPrinter(defaultPrinter);

(2) 不显示WORD、EXCEL等程序窗口

此种方式,使用.NET调用COM的方式来实现,利用COM对象本身的特性来设置可见性和打印机

使用此方法前,需要先添加Office的COM引用,这里略过。

//要打印的文件路径
object wordFile = @"d:\a.doc";

object oMissing = Missing.Value;

//自定义object类型的布尔值
object oTrue = true;
object oFalse = false;

object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;

//定义WORD Application相关
Word.Application appWord = new Word.Application();

//WORD程序不可见
appWord.Visible = false;
//不弹出警告框
appWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;            

//先保存默认的打印机
string defaultPrinter = appWord.ActivePrinter;

//打开要打印的文件
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.ActivePrinter = "指定打印机的名字";

//打印
doc.PrintOut(
    ref oTrue, //此处为true,表示后台打印
    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,
    ref oMissing,
    ref oMissing,
    ref oMissing,
    ref oMissing
    );

//打印完关闭WORD文件
doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);

//还原原来的默认打印机
appWord.ActivePrinter = defaultPrinter;

//退出WORD程序
appWord.Quit(ref oMissing, ref oMissing, ref oMissing);

doc = null;
appWord = null;

此方法,于COM交互,性能有些损失,要注意COM对象的释放,以及异常控制,此处忽略。

转载于:https://www.cnblogs.com/xiachufeng/archive/2010/07/31/1789136.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先机子上安装有office,在COM中添加引用Microsoft.Word.11.0.Object.Library(或11.0以上) Microsoft.Office.Interop.Word.Application myWordApp = null; Microsoft.Office.Interop.Word.Document doc = null; object Filename = path + "\\" + TaskID + ".docx";//目的文件 object templateFile = System.Windows.Forms.Application.StartupPath + @"\Template.docx";//模板文件,有一个五列一行的表 System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), true);//模板WORD中有一个五列的表头,分别是卡号,串口号,发送指令条数,接收指令条数,收发成功率 myWordApp = new Microsoft.Office.Interop.Word.Application(); doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /////显示页码 object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; object oFirstPage = true; oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignment, ref oFirstPage); myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; for (int i = 2; i < 102; i++)//举例100台 { doc.Tables[1].Rows.Add(ref Nothing);//表格增加一行 doc.Tables[1].Cell(i, 1).Range.Text = "250297";//卡号 doc.Tables[1].Cell(i, 2).Range.Text = "COM12";//串口号 doc.Tables[1].Cell(i, 3).Range.Text = "100";//发送指令条数 doc.Tables[1].Cell(i, 4).Range.Text = "99";//接收指令条数 doc.Tables[1].Cell(i, 5).Range.Text = "99%";//收发成功率 } doc.SaveAs(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;//不保存挂起的更改 ////下面是直接打印,文档不显示 //doc.PrintOut(ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object readOnly=false; object isVisable = true;////文档打开状态为可视 doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothing, ref Nothing); doc.PrintPreview();//打印预览
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值