/// <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);

}

按模板打印word防止并发操作的更多相关文章

  1. JAVA Asponse.Word Office 操作神器,借助 word 模板生成 word 文档,并转化为 pdf,png 等多种格式的文件

    一,由于该 jar 包不是免费的, maven 仓库一般不会有,需要我们去官网下载并安装到本地 maven 仓库 1,用地址   https://www-evget-com/product/564  ...

  2. JAVA Freemarker + Word 模板 生成 Word 文档 (普通的变量替换,数据的循环,表格数据的循环,以及图片的东替换)

    1,最近有个需求,动态生成 Word 文当并供前端下载,网上找了一下,发现基本都是用 word 生成 xml 然后用模板替换变量的方式 1.1,这种方式虽然可行,但是生成的 xml 是在是太乱了,整理 ...

  3. 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)

    https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...

  4. poi根据模板导出word文档

    POI结构与常用类 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI ...

  5. 按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式

    按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式 之前写过一篇文章<按照已有的模板输出一(如发票)>,是关于如何给已有的模板赋值.在项目的实践过程 ...

  6. Python 使用 win32com 模块对 word 文件进行操作

    what's the win32com 模块 win32com 模块主要为 Python 提供调用 windows 底层组件对 word .Excel.PPT 等进行操作的功能,只能在 Windows ...

  7. vue-element-admin实现模板打印

    一.简介 模板打印也叫”套打“,是业务系统和后台管理系统中的常用功能,B/S系统中实现”套打“比较繁琐,所以很多的B/S系统中的打印功能一直使用的是浏览器打印,很少实现模板打印.本篇将介绍在Vue E ...

  8. C#通过模板导出Word的两种方法(超简单)

    方法一:使用Office的组件 使用该方法必须要安装Office 1.制作Word模板 在需要填充内容的地方增加标识符号,方便之后替换使用,例如 [项目名称],其中[]符号和中间的文字可根据个人情况进 ...

  9. 利用COM组件实现对WORD书签各种操作大全,看这一篇就够了

    有个需求是,程序导出一份word报告,报告中有各种各样的表格,导出时还需要插入图片. 脑海中迅速闪过好几种组件,openxml组件,com组件,npoi.为了减少程序画复杂表格,我们选用了com组件+ ...

随机推荐

  1. Android 为应用添加数字角标

    今天在论坛上看到了一个帖子,终于搞清了我很久以来的一个困惑,android到底能不能实现ios的角标效果,QQ是怎么实现的.看了这个帖子顿时终于解除了我的困惑. 先说一个下大概的思路: 大家都知道an ...

  2. rhel6.4 配置本地yum的源

    1 创建rhel-debuginfo.repo,如果有则先备份再删除       cd  /etc/yum.repos.d    rm  rhel-debuginfo.repo    vi  rhel ...

  3. Windows下使用XManager访问Ubuntu 11.04的设置方法

    1.Download the attached file and upload it to the server. And, enter the following command to update ...

  4. Struts2 + Spring + hibernate 框架搭成实例

    1.准备Jar包: struts2.hibernate.spring所需jar包   struts-core-2.x.x.jar  ----struts核心包 xwork-core-2.x.x.jar ...

  5. wx.button

    wx.Button A button is a control that contains a text string, and is one of the most common elements ...

  6. 使用Transaction访问数据库(C#,TransactionScope,.NET 2.0)

    针对SQL2005和.NET 2.0的事物机制有了新的突破传统数据库事物访问机制,代码如下:                   或者这种,其实都差不多                         ...

  7. CSS权威指南学习笔记 —— HTML元素分类

    HTML文档由各种元素组成.比如,p.table.span等等.每个元素都会对文档的表现有所影响.CSS中,每个元素都会生成一个框(传说中的盒子),其中包含元素内容. 元素可以根据它的创建方式分为两种 ...

  8. 自学XML DOM的几个例子

    XML DOM定义了如何获取.修改.添加和删除XML文件中结点的接口,极大方便了开发者对XML文件的使用.XML DOM教程和手册请转:http://www.w3school.com.cn/xmldo ...

  9. Struts 2.x 与Spring4.x整合出现:No mapping found for dependency [type=java.lang.String, name='actionPackages...

    Struts2.16与Spring4.x整合出错: Caused by: java.lang.RuntimeException: com.opensymphony.xwork2.inject.Depe ...

  10. 20160115--Hibernate

    package com.hanqi.dao; import static org.junit.Assert.*; import java.util.*; import org.hibernate.se ...