zoukankan      html  css  js  c++  java
  • C#调用word打印

    开发指南:

    1.  必须安装office.net开发包。

    2.  在工程的reference中添加com组件:Microsoft Word 11.0 Object Library

     1//打印的代码如下:
     2 Microsoft.Office.Interop.Word.Application app = null;
     3            Microsoft.Office.Interop.Word.Document doc = null;
     4            object missing = System.Reflection.Missing.Value;
     5            object templateFile = Application.StartupPath + @"\表单模版.doc";
     6            try
     7            {
     8                app = new Microsoft.Office.Interop.Word.ApplicationClass();
     9                doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);
    10                try
    11                {
    12                    foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
    13                    {
    14                        bm.Select();
    15                        string item = bm.Name;
    16                        if (item.Equals("in_time"))
    17                        {
    18                            bm.Range.Text = table.inTime == null ? "" : table.inTime.ToString();
    19                        }

    20                        else if (item.Equals("car_id"))
    21                        {
    22                            bm.Range.Text = table.carID == null ? "" : table.carID.ToString();
    23                        }

    24                                       }

    25                }

    26                catch
    27                {
    28                }

    29                //打印
    30               doc.PrintOut(ref missing, ref missing, ref missing, ref missing, 
    31                    ref missing, ref missing, ref missing, ref missing, ref missing, 
    32                    ref missing, ref missing, ref missing, ref missing, ref missing, 
    33                    ref missing, ref missing, ref missing, ref missing);
    34                     }

    35            catch (Exception exp)
    36            {
    37                MessageBox.Show(exp.Message, this.Text);
    38            }

    39//销毁word进程
    40            finally
    41            {
    42               object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
    43                if (doc != null)
    44                    doc.Close(ref saveChange, ref missing, ref missing);
    45                if (app != null)
    46                    app.Quit(ref missing, ref missing, ref missing);
    47            }

    48 
    49

  • 相关阅读:
    wpf.xaml.behavior
    为你的硬件自动化统一构建root和firmware
    Dsm as deepin mate(2):在阿里云上真正实现单盘安装运行skynas
    Boot界的”开源os“ : coreboot,及再谈云OS和本地OS统一装机的融合
    0pe单文件夹,grub菜单全外置版
    阿里云上利用virtiope+colinux实现linux系统盘动态无损多分区
    比WEB更自然,jupyter用于通用软件开发的创新意义:使任何传统程序秒变WEB
    编程实践选型通史:*坦无架构APP开发支持与充分batteryincluded的微实践设施
    除了LINUX,我们真的有可选的第二开源操作系统吗?
    聪明的Mac osx本地云:同一生态的云硬件,云装机,云应用,云开发的完美集
  • 原文地址:https://www.cnblogs.com/conquer/p/1111142.html
Copyright © 2011-2022 走看看