按模板打印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组件+ ...
随机推荐
- Spring 3.x企业实用开发实战(1)
有关Spring的介绍这里就不赘述了,主要是学习了陈雄华版的<Spring 3.x企业应用开发实战>并做了一点笔记,以助于后期的回顾和复习. 废话不多说,直接进入主题,以下所有代码基于&l ...
- ASP 代码当前记录集不支持更新问题的解决办法。
错误类型: ADODB.Recordset (0x800A0CB3) 当前记录集不支持更新.这可能是提供程序的限制,也可能是选定锁定类型的限制. /Model/manage/Admin_Admin.a ...
- 【OpenGL游戏开发之三】OpenGl核心函数库汇总
OpenGl核心函数库 glAccum 操作累加缓冲区 glAddSwapHintRectWIN 定义一组被SwapBuffers拷贝的三角形 glAlphaFunc允许设置alpha检测功能 glA ...
- AndroidUI 视图动画-自定义动画效果 (Animation)
如果Android提供的四种动画 效果 和混合动画效果 不能够 满足需求的话,可以使用自定义动画效果 : 新建一个类CustomAnimation 使其继承自 android.view.animati ...
- HBASE学习笔记--配置信息
hbase的配置信息,在hbase-site.xml里面有详细说明. 可以按照需要查询相关的配置. <?xml version="1.0"?> <?xml-sty ...
- hdu4507
数位dp,终于守得云开见月明了.建议初学者先试试两道比较简单的hdu2089,hdu3555. 鸣谢:http://blog.csdn.net/acm_cxlove/article/details/8 ...
- PHP 提交checkbox表单时 判断复选框是否被选中
function GetTitleImgPath(){ $titleImgPath = ""; if (isset($_POST["titlecheckbox" ...
- shared_ptr智能指针源码剖析
(shared_ptr)的引用计数本身是安全且无锁的,但对象的读写则不是,因为 shared_ptr 有两个数据成员,读写操作不能原子化.根据文档 (http://www.boost.org/doc/ ...
- Programming C#.Interfaces
类实现一个接口时,它必须实现该接口的所有部分(方法和属性等),效果相当于类要表明:“我同意履行这个接口所定义的协定.” 从抽象类继承实现了“is-a(是一种)”关系,实现接口时一种“implement ...
- mysql 数据库热备份
https://www.percona.com/doc/percona-xtrabackup/2.2/index.html