按模板打印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);
}
按模板打印word防止并发操作的更多相关文章
- JAVA Asponse.Word Office 操作神器,借助 word 模板生成 word 文档,并转化为 pdf,png 等多种格式的文件
一,由于该 jar 包不是免费的, maven 仓库一般不会有,需要我们去官网下载并安装到本地 maven 仓库 1,用地址 https://www-evget-com/product/564 ...
- JAVA Freemarker + Word 模板 生成 Word 文档 (普通的变量替换,数据的循环,表格数据的循环,以及图片的东替换)
1,最近有个需求,动态生成 Word 文当并供前端下载,网上找了一下,发现基本都是用 word 生成 xml 然后用模板替换变量的方式 1.1,这种方式虽然可行,但是生成的 xml 是在是太乱了,整理 ...
- 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)
https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...
- poi根据模板导出word文档
POI结构与常用类 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI ...
- 按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式
按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式 之前写过一篇文章<按照已有的模板输出一(如发票)>,是关于如何给已有的模板赋值.在项目的实践过程 ...
- Python 使用 win32com 模块对 word 文件进行操作
what's the win32com 模块 win32com 模块主要为 Python 提供调用 windows 底层组件对 word .Excel.PPT 等进行操作的功能,只能在 Windows ...
- vue-element-admin实现模板打印
一.简介 模板打印也叫”套打“,是业务系统和后台管理系统中的常用功能,B/S系统中实现”套打“比较繁琐,所以很多的B/S系统中的打印功能一直使用的是浏览器打印,很少实现模板打印.本篇将介绍在Vue E ...
- C#通过模板导出Word的两种方法(超简单)
方法一:使用Office的组件 使用该方法必须要安装Office 1.制作Word模板 在需要填充内容的地方增加标识符号,方便之后替换使用,例如 [项目名称],其中[]符号和中间的文字可根据个人情况进 ...
- 利用COM组件实现对WORD书签各种操作大全,看这一篇就够了
有个需求是,程序导出一份word报告,报告中有各种各样的表格,导出时还需要插入图片. 脑海中迅速闪过好几种组件,openxml组件,com组件,npoi.为了减少程序画复杂表格,我们选用了com组件+ ...
随机推荐
- 在共享DLL中使用MFC 和在静态库中使用MFC的区别
使用VS2008,在项目属性中有一项MFC的使用,有三种设置: 1.使用标准Windows库 2.在共享DLL中使用MFC 3.在静态库中使用MFC 第一种顾名思义. ...
- OleVariant的本质
OleVariant的本质 OleVariant,COM的一种数据类型.MIDAS基于COM之上构建的,自然使用OleVariant作为数据序列格式. 延续到现在最新的DATASNAP仍然支持它. T ...
- POI开源项目-PPT2PNG转换测试
第一次学使用软件去读取Office类文件. 今天百度了一下,发现Apache也是有这样的开源项目的,POI提供下列支持: 测试了一下HSLF对PPT的读操作,并将PPT转化为图片保存到指定目录: 使用 ...
- How to change a product dropdown attribute to a multiselect in Magento
First, update the attribute input type to multiselect: UPDATE eav_attribute SET entity_type_id ', at ...
- KVC在定义Model类中的妙用
@我们应用程序使用MVC架构的话,对于处理数据类,我们会单独的定义Model类,在里面为要展示的属性进行初始化赋值,一般採用的方法是通过定义相应的属性,挨个赋值.如今我要介绍的就是通过KVC,key- ...
- java实现xml文件增删改查
java一次删除xml多个节点: 方案1.你直接改动了nodeList,这一般在做循环时是不同意直接这么做的. 你能够尝试在遍历一个list时,在循环体同一时候删除list里的内容,你会得到一个异常. ...
- Android之TextView的样式类Span的使用具体解释
Android中的TextView是个显示文字的的UI类,在现实中的需求中,文字有各式各样的样式.TextView本身没有属性去设置实现,我们能够通过Android提供的 SpannableStrin ...
- servlet上传文件报错(一)
1.详细报错例如以下: org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multip ...
- css3 翻转和旋转的差别
我曾经一直以为旋转跟翻转一样,今日自己旋转了好久都发觉跟翻转差一点点,纠结了十几分钟才明确,仅仅能怪自己的立体感太差了. css3中的transform中有旋转,放缩,倾斜,平移的功能,分别相应的属性 ...
- iOS开发那些事儿(二)热补丁
一.热补丁作用:修复导致崩溃的错误.替换/增加方法.替换原来的界面等等 二.实现手段:JSPatch (使用Objective-C Objective-C和JavaScript jspatch桥.你可 ...