zoukankan      html  css  js  c++  java
  • 按模板打印word防止并发操作

    /// <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打印
    ///创建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将临时对象保存起来,这就相当于,在
    编辑完word之后按了保存按钮一样,只是暂时的保存。
    ///此时通过以下合并文档方法将临时保存的数据合并到一个文档中,也就相当于加入下一页,此时删除临时数据,当前的文档就是需要导出的文件, #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 }
    //////将数据写到标签上
     private string PrintText(string str)
            {
              
                if (str == "")
                {
                    return " ";
                   
                }
                else
                {
                    return str;
                }
            }
    ///复制文件方法
    private void myCopy(string sourceFile, string targetFile)    

         {          

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

             {              

       System.IO.File.Delete(targetFile);         

        }      

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

          }

  • 相关阅读:
    idea控制台乱码问题解决
    部署springboot项目到云服务器的两种方式(jar+war)
    case...when...then....(else...)....end的使用小案例
    多线程学习(二)
    多线程学习(一)
    Swagger学习笔记
    Docker进阶篇
    Docker入门基础篇
    Markdown语法学习(推荐使用Typora编辑器)
    git(六)——IDEA中使用git
  • 原文地址:https://www.cnblogs.com/zlqblog/p/3754422.html
Copyright © 2011-2022 走看看