码迷,mamicode.com
首页 > 其他好文 > 详细

按模板打印word防止并发操作

时间:2014-05-29 00:34:26      阅读:468      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   tar   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/// <summary>
        ///
        /// <summary>
        /// 打印人员备案表
        /// </summary>
        /// <param name="UPT_ID"></param>
        /// <param name="printError"></param>
        /// <returns></returns>
        public bool PrintCGRYBA(string UPT_ID, out string printError)
        {
            printError = "";
            //获得数据
            DBControl.ORMapping.T_UPTeam objUPTeam = new DBControl.ORMapping.T_UPTeam();
            objUPTeam.theDBAgent = CTheDBAgent;
            objUPTeam.Open(UPT_ID);
            objUPTeam.Find(0);
 
            //try
            //{
            //return DownLoadFile(UPT_ID, objUPTeam.UPT_ID + "人员备案表", objUPTeam.UPT_NAME + "人员备案表.doc");
            //}
            //catch (Exception ex)
            //{
            //    printError = ex.ToString();
            //    return false;
            //}
 
 
            //end
 
            #region 直接调用word打印<br>///创建word对象
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            try
            {
 
 
                Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
                object Visible = false;
                object ReadOnly = false;
                object missing = System.Reflection.Missing.Value;
                object WdChatacter = Word.WdUnits.wdCharacter;
                object WdCell = Word.WdUnits.wdCell;
                object WdStory = Word.WdUnits.wdStory;
                object WdPageBreak = Word.WdBreakType.wdPageBreak;
 
 
 
                int page = 1;
///逐个数据循环,保存个子的文件,首先是从doc下复制模板,在saveword中按主键方式另存文档
                for (int i = 0; i < objUPTeam.theUPMembers.Count(); i++)
                {
                    objUPTeam.theUPMembers.Find(i);
 
                    #region 出国人员备案表
                    string TemplateFile = System.Web.HttpContext.Current.Server.MapPath(".") + "\\Doc\\东莞市因公临时出国赴港澳人员备案表.doc";
                    //生成的具有模板样式的新文件
                    string FileName = System.Web.HttpContext.Current.Server.MapPath(".") + "\\SaveWord\\" + UPT_ID + "出国人员备案表" + "(" + page.ToString() + ")" + ".doc";
                    //模板文件拷贝到新文件
 
                    myCopy(TemplateFile, FileName);
                    object Obj_FileName = FileName;
 
                    //打开文件
                    doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref Visible,
                        ref missing, ref missing, ref missing,
                        ref missing);
                    doc.Activate();
 
                    #region 填入变量值
                        object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
 
                        object BookMarkName = "人员姓名";
                        if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
                        {
                            doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                            doc.ActiveWindow.Selection.TypeText(PrintText(objUPTeam.theUPMembers.UPER_NAME));
                        }
                        BookMarkName = "性别";
                        if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
                        {
                            doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                            doc.ActiveWindow.Selection.TypeText(PrintText(objUPTeam.theUPMembers.UPER_SEX));
                        }
                        BookMarkName = "组团单位";
                        if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
                        {
                            doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                            string ztdw = "";
                            if (objUPTeam.UPT_ACOMP!="")
                            {
                                ztdw = objUPTeam.UPT_ACOMP;
                            }
                            else
                            {
                                ztdw = objUPTeam.UPT_UPNAME;
                            }
                            doc.ActiveWindow.Selection.TypeText(PrintText(ztdw));
                        }
                          BookMarkName = "类型备注3";
                        if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
                        {
                            doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                            if (objUPTeam.UPT_TYPE == "出国")
                            {
                                doc.ActiveWindow.Selection.TypeText(PrintText(""));
                            }
                            else
                            {
                                doc.ActiveWindow.Selection.TypeText(PrintText("(港澳事务)"));
                            }
                        }
                        BookMarkName = "出访任务国家";
                        if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
                        {
                            doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                            string cfrwAndCfd = "";
 
                            if (objUPTeam.UPT_TYPE == "出国")
                            {
                                //出国任务、所赴国家(地区)及停留时间
                                cfrwAndCfd = objUPTeam.UPT_TASK;
                                string cfd = "";
                                int stay_days = 0;
                                for (int g = 0; g < objUPTeam.theUPCountry.Count(); g++)
                                {
                                    objUPTeam.theUPCountry.Find(g);
                                    if (objUPTeam.theUPCountry.UPC_STAY!="过境")
                                    {
                                        if (cfd=="")
                                        {
                                            cfd = objUPTeam.theUPCountry.UPC_COUNTRY;
                                        }
                                        else
                                        {
                                            cfd += "," + objUPTeam.theUPCountry.UPC_COUNTRY;
                                        }
                                        stay_days += objUPTeam.theUPCountry.UPC_DAYS;
                                    }
                                }
                                cfrwAndCfd += "、" + cfd + stay_days.ToString() + "天";
                                doc.ActiveWindow.Selection.TypeText(PrintText(cfrwAndCfd));
                            }
                            else
                            {
                                //出国任务、所赴地区及停留时间
                                string rw = "";
                                string cfd = "";
                                int stay_days = 0;
                                for (int g1 = 0; g1 < objUPTeam.theUPCountry.Count(); g1++)
                                {
                                    objUPTeam.theUPCountry.Find(g1);
 
                                    if (rw == "")
                                    {
                                        rw = objUPTeam.theUPCountry.UPC_TASK1;
                                    }
                                    else
                                    {
                                        rw += "," + objUPTeam.theUPCountry.UPC_TASK1;
                                    }
                                    if (cfd == "")
                                    {
                                        cfd = objUPTeam.theUPCountry.UPC_COUNTRY;
                                    }
                                    else
                                    {
                                        cfd += "," + objUPTeam.theUPCountry.UPC_COUNTRY;
                                    }
                                    stay_days += objUPTeam.theUPCountry.UPC_STAYDAYS1;
                                }
 
                                cfrwAndCfd = rw + "、" + cfd + stay_days.ToString() + "天";
                                doc.ActiveWindow.Selection.TypeText(PrintText(cfrwAndCfd));
                            }
                        }
 
 
                        BookMarkName = "最后一次出访记录";
                        if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
                        {
                            doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                            doc.ActiveWindow.Selection.TypeText(PrintText(objUPTeam.theUPMembers.UPM_LATEST_RECORD));
                        }
                        #endregion
 
                    #region 保存临时文件
 
                    object IsSave = true;
                    doc.Close(ref IsSave, ref missing, ref missing);
                    #endregion
                    page += 1;
 
                    #endregion
 
                }
 
///在这之前的循环操作是针对多文档操作的,首先是按循环将模板复制一份,保存起来,然后逐个对复制的文件进行数据写入,写入后的文件通过doc.close将临时对象保存起来,这就相当于,在<br>编辑完word之后按了保存按钮一样,只是暂时的保存。<br>///此时通过以下合并文档方法将临时保存的数据合并到一个文档中,也就相当于加入下一页,此时删除临时数据,当前的文档就是需要导出的文件,
                #region 合并文档
                string TargetName = System.Web.HttpContext.Current.Server.MapPath(".") + "\\SaveWord\\" + UPT_ID + "人员备案表" + ".doc";
                for (int i = 1; i < page; i++)
                {
                    if (i == 1)
                    {
                        string TemplateFile = System.Web.HttpContext.Current.Server.MapPath(".") + "\\SaveWord\\" + UPT_ID + "人员备案表" + "(" + i.ToString() + ")" + ".doc";
                        //模板文件拷贝到新文件
                        myCopy(TemplateFile, TargetName);
                        object Obj_FileName = TargetName;
 
                        doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
                           ref missing, ref missing, ref missing, ref missing,
                           ref missing, ref missing, ref missing, ref Visible,
                           ref missing, ref missing, ref missing,
                           ref missing);
                        doc.Activate();
                    }
                    else
                    {
                        string TemplateFile = System.Web.HttpContext.Current.Server.MapPath(".") + "\\SaveWord\\" + UPT_ID + "人员备案表" + "(" + i.ToString() + ")" + ".doc";
                        doc.Application.Selection.EndKey(ref WdStory, ref missing);
                        doc.Application.Selection.InsertBreak(ref WdPageBreak);
                        doc.Application.Selection.InsertFile(TemplateFile, ref missing, ref missing, ref missing, ref missing);
                    }
                }
 
                try
                {
                    doc.Application.Selection.EndKey(ref WdStory, ref missing);
                    doc.Application.Selection.Delete(ref WdChatacter, ref missing);
                }
                catch { }
 
                try
                {
                    //删除临时数据
                    for (int t = 1; t < page; t++)
                    {
                        System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(".") + "\\SaveWord\\" + UPT_ID + "人员备案表" + "(" + t.ToString() + ")" + ".doc");
                    }
                }
                catch { }
                #endregion
 
                object IsSave1 = true;
 
                doc.Close(ref IsSave1, ref missing, ref missing);
                doc = null;
                app.Quit(ref IsSave1, ref missing, ref missing);
                app = null;
                DownLoadFile(TargetName);
                GC.Collect();
 
 
                return true;
            }
            catch (Exception ex)
            {
                printError = ex.ToString();
                app = null;
                GC.Collect();
                return false;
            }
            #endregion
 
        }<br>//////将数据写到标签上<br> private string PrintText(string str)<br>        {<br>           <br>            if (str == "")<br>            {<br>                return " ";<br>                <br>            }<br>            else<br>            {<br>                return str;<br>            }<br>        }<br>///复制文件方法<br>private void myCopy(string sourceFile, string targetFile)    

     {          

   if (System.IO.File.Exists(targetFile))    

         {              

   System.IO.File.Delete(targetFile);         

    }      

System.IO.File.Copy(sourceFile, targetFile);   

      }

 

按模板打印word防止并发操作,布布扣,bubuko.com

按模板打印word防止并发操作

标签:style   c   class   blog   code   tar   

原文地址:http://www.cnblogs.com/zlqblog/p/3754422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!