由于项目需要,需要实现单据的套打,总共四联,请教过朋友,说是用word模板比较容易实现,根据网络资源及自我实践
关键代码如下:(利用word书签实现套打)

Code
 1        private void button1_Click(object sender, EventArgs e)
 2        {
 3            Microsoft.Office.Interop.Word.Application app = null;
 4            Microsoft.Office.Interop.Word.Document doc = null;
 5
 6            object missing = System.Reflection.Missing.Value;
 7            object templateFile = Application.StartupPath + @”\表单模板.doc;
 8
 9            try
10            {
11                app = new Microsoft.Office.Interop.Word.ApplicationClass();
12                doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);
13
14                try
15                {
16                    foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
17                    {
18                        bm.Select();
19
20                        string item = bm.Name;
21
22                        if (item.Equals(Num))//流水号
23                        {
24                            bm.Range.Text = 2009-000001;
25                        }

26                        else if (item.Equals(ProName))//产品名称
27                        {
28                            bm.Range.Text = 甲醇;
29                        }

30                        else if (item.Equals(Unitname))//收货单位
31                        {
32                            bm.Range.Text = 中国石化;
33                        }

34                        else if (item.Equals(Carid))//车号
35                        {
36                            bm.Range.Text = 苏G888888;
37                        }

38                        else if (item.Equals(Driver))//收货人
39                        {
40                            bm.Range.Text = 小英;
41                        }

42                        else if (item.Equals(Datetime))//开票时间
43                        {
44                            bm.Range.Text = 2009.9.7;
45                        }

46                    }

47                }

48                catch
49                {
50            //自己的异常处理代码
51            ……
52                }

53
54                //打印
55                doc.PrintOut(ref missing, ref missing, ref missing, ref missing,
56                     ref missing, ref missing, ref missing, ref missing, ref missing,
57                     ref missing, ref missing, ref missing, ref missing, ref missing,
58                     ref missing, ref missing, ref missing, ref missing);
59            }

60            catch (Exception exp)
61            {
62                MessageBox.Show(exp.Message, this.Text);
63            }

64
65            //销毁word进程(切记,否则会无端占用系统资源)
66            finally
67            {
68                object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
69                if (doc != null)
70                    doc.Close(ref saveChange, ref missing, ref missing);
71
72                if (app != null)
73                    app.Quit(ref missing, ref missing, ref missing);
74            }

75        }

但是上面的代码有一些缺点,那就是标签内容更改了之后标签的记号自动清除,于是有了下边的方法:
在赋值后,在对应位置上给文档再新建一个BookMark。这样每次填写修改完毕后BookMark依然存在,问题就解决了。

Code
 1                        #region
 2                        //bookMark 为书签名称Name
 3                        Bookmark curBookmark = wApp.ActiveDocument.Bookmarks.get_Item(ref bookMark);
 4                        string strOldName = curBookmark.Name;
 5                        Range oldrange = curBookmark.Range;
 6                        //replaceString 为需要在书签位置填写的信息字符串
 7                        oldrange.Text = replaceString;
 8                        object newBookmarkRange = (object)oldrange;
 9                        wApp.ActiveDocument.Bookmarks.Add(strOldName,ref newBookmarkRange);
10                        #endregion

11

代码参考http://www.cnblogs.com/michaellu/archive/2007/08/29/874798.html

版权声明:本文为lingxinyun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lingxinyun/archive/2009/09/08/1562521.html