QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 1992|回复: 0

[C#] C# 打印Word文档

[复制链接]

等级头衔

积分成就    金币 : 2802
   泡泡 : 1516
   精华 : 6
   在线时间 : 1242 小时
   最后登录 : 2024-4-18

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2022-4-18 17:34:54 | 显示全部楼层 |阅读模式
       在日常工作中,我们可能常常需要打印各种文件资料,比如word文档。对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作。可以通过调用打印对话框(PrintDialog)来进行相关打印设置,也可以通过静默打印方式直接打印Word文档。一般如果要想选择非默认打印机或者说想显示打印设置对话框时,我们也需要对代码进行一定的设置。
; w% c" b" ~; b4 s/ y/ t实现方法:5 Y7 v, B# M5 C; W7 x
1、系统安装 Office,可以是微软的Office,也可以是wps。+ C+ a4 E1 e2 x/ I) F
2、实现方法代码 ! Y; s" [( L9 @2 N
  1.   public bool PrintDoc()
  2.         {
  3.          
  4.             try
  5.             {
  6.                 //要打印的文件路径
  7.                 object wordFile = Directory.GetCurrentDirectory() + "/0.doc";
  8.                 object oMissing = Missing.Value;
  9.                 //自定义object类型的布尔值
  10.                 object oTrue = true;
  11.                 object oFalse = false;
  12.                 object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
  13.                 //定义WORD Application相关
  14.                 Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
  15.                 //WORD程序不可见
  16.                 appWord.Visible = false;
  17.                 //不弹出警告框
  18.                 appWord.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
  19.                 //先保存默认的打印机
  20.                 string defaultPrinter = appWord.ActivePrinter;
  21.                 //打开要打印的文件
  22.                 Microsoft.Office.Interop.Word.Document doc = appWord.Documents.Open(
  23.                     ref wordFile,
  24.                     ref oMissing,
  25.                     ref oTrue,
  26.                     ref oFalse,
  27.                     ref oMissing,
  28.                     ref oMissing,
  29.                     ref oMissing,
  30.                     ref oMissing,
  31.                     ref oMissing,
  32.                     ref oMissing,
  33.                     ref oMissing,
  34.                     ref oMissing,
  35.                     ref oMissing,
  36.                     ref oMissing,
  37.                     ref oMissing,
  38.                     ref oMissing);
  39.                 PrintDocument print = new PrintDocument();
  40.                 string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
  41.                 //List<string> printList = new List<string>();
  42.                 //foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  43.                 //{
  44.                 //    printList.Add(sPrint);
  45.                 //}
  46.                 //设置指定的打印机
  47.                 appWord.ActivePrinter = sDefault;
  48.                 //打印
  49.                 doc.PrintOut(
  50.                     ref oTrue, //此处为true,表示后台打印
  51.                     ref oFalse,
  52.                     ref oMissing,
  53.                     ref oMissing,
  54.                     ref oMissing,
  55.                     ref oMissing,
  56.                     ref oMissing,
  57.                     ref oMissing,
  58.                     ref oMissing,
  59.                     ref oMissing,
  60.                     ref oMissing,
  61.                     ref oMissing,
  62.                     ref oMissing,
  63.                     ref oMissing,
  64.                     ref oMissing,
  65.                     ref oMissing,
  66.                     ref oMissing,
  67.                     ref oMissing
  68.                     );
  69.                 //打印完关闭WORD文件
  70.                 doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
  71.                 //还原原来的默认打印机
  72.                 appWord.ActivePrinter = defaultPrinter;
  73.                 //退出WORD程序
  74.                 appWord.Quit(ref oMissing, ref oMissing, ref oMissing);
  75.                 doc = null;
  76.                 appWord = null;
  77.                 return true;
  78.             }
  79.             catch (Exception e)
  80.             {
  81.                // MessageBox.Show(e.ToString());
  82.                 return false;
  83.             }
  84.         }
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-4-27 20:20

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表