/// <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. Unicode其实是Latin1的扩展。只有一个低字节的Uncode字符其实就是Latin1字符——附各种字符编码表及转换表

    一.概念 1,ASCII             ASCII(American Standard Code for Information Interchange),中文名称为美国信息交换标准代码.是 ...

  2. docker 网络4种模式

    1.host 模式,使用docker run 时 使用--net=host 指定 docker 使用的网络和宿主机一样,在容器上看到的网卡ip就是宿主机上的ip 2.container 模式,使用-- ...

  3. django中怎样生成非HTML格式的内容。

    某些时候可能有这种需求.在网页中点击一个链接或者一个button希望返回一张图片.一个pdf文档.一个csv文档等而非HTML. 在diango中非常easy做到这些.django中的view用来接收 ...

  4. python单/双下划线使用

    在Python编程中经常会遇到函数(function),方法(method)及属性(attribute)以下划线'_'作为前缀,这里做个总结. 主要存在四种情形: 1. object # public ...

  5. VB.NET数据库编程基础教程

    关键词:作者罗姗   众所周知,VB.NET自身并不具备对数据库进行操作的功能,它对数据库的处理是通过.NET FrameWork SDK中面向数据库编程的类库和微软的MDAC来实现的.其中,ADO. ...

  6. MySQL报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:NO)

    1.关闭mysql   # service mysqld stop2.屏蔽权限   # mysqld_safe --skip-grant-table   屏幕出现: Starting demo fro ...

  7. Mantis 1.1.0 报告问题中设置必填项或取消必填项[Z]

    打开/mantis/core/bug_api.php,其中有类似下面的语句 if ( is_blank( $c_summary ) ) {    error_parameters( lang_get( ...

  8. oracle tablespace

    oracle tablespace 1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_fil ...

  9. 20151204--JDBC

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. [MUD]MUDLIB详解/MUDOS运行流程/最小MUDLIB/mud文件结构

    现在大部分中文MUD都是在东方故事(esII)基础上发展起来的,其目录结构基本一样, 也有个别MUD为了标新立异对个别目录换了个名字以示不同,但其实质没有什么变化. 这个做的最可恶的是xkx,把一个好 ...