/// <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. 普林斯顿大学算法课 Algorithm Part I Week 3 比较器 Comparators

    比较器接口(Comparator interface):用可选顺序(alternate order)进行排序 public interface Comparator<key> int co ...

  2. Android4.2.2的Stagefright维护编解码器的数据流

    这里是他们自己的源代码阅读点滴总结属性,转请注明出处,谢谢. 欢迎和大家分享.qq:1037701636 email:gzzaigcn2012@gmail.com Android源代码版本号Versi ...

  3. C#的Split用法

    1.用字符串分隔: using System.Text.RegularExpressions;string str="aaajsbbbjsccc";string[] sArray= ...

  4. c++中自增(++)和自减(--)操作符

    自增(++)和自减(--)操作符为对象加1 或减1 操作提供了方便简短的实现方式.它们有前置和后置两种使用形式.到目前为止,我们已经使用过前自增操作,该操作使其操作数加1,操作结果是修改后的值.同理, ...

  5. web.xml加载顺序详解

    一. 1.启动tomcat启动web项目,首先读取web.xml文件中<context-param>和<listener> 2.容器创建一个ServletContext(ser ...

  6. js函数定时器,定时读取系统实时连接数

    function GetDeviceInfo() {     setInterval(function()         {         GetDeviceRealtimeConnect();  ...

  7. 纯CSS 贴底部的布局(兼容各个浏览器包括IE6)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  8. php正则的使用[替换,匹配]

    // 隐藏电话的中间四位 echo parseMobile('15836365252'); function parseMobile($mobile, $start = 4, $len = 4){ $ ...

  9. 正选反选JS

    JS <script> window.onload=function(){ var oTher=document.getElementById('other'); var oCheck=d ...

  10. 通过innerHTML简化脚本

    <!doctype html> <html lang="en"> <head>   <meta charset="UTF-8&q ...