.net下实现Word动态填加数据打印

今天研究了一下.net下实现Word动态填加数据打印的做法,觉得颇有收获~
      以前做过Excel相关的东西,所以对OFFICE的COM有一些了解,很顺利的找到了需要引用的COM和其帮助文档~具体做法是在引用里添加 COM --------Microsoft word 11.0 object library, 然后引入命名空间:

using WordApplication =  Microsoft.Office.Interop.Word.Application;
using Document =
 Microsoft.Office.Interop.Word.Document;
using Bookmark = Microsoft.Office.Interop.Word.Bookmark;

具体实现代码:

WordApplication word = null ;
 
/// <summary>
 
/// 实现动态添加数据打印预览
 
/// </summary>

/// <param name="path">word模板路径</param>
/// <param name="adStu">调剂学生相关信息对象</param>

private void PrintPreview(string  path, AdStuInfo adStu)
        
{
            
if (word == null
)
            
{
                word 
= new
 WordApplication();
            }

            
object oMissing = System.Reflection.Missing.Value;   //这个东西找了好久,不像操作Excel,用Type.mising还不行.
            object path1 = (object)path;
            Document doc 
= null
;
            doc 
= word.Documents.OpenOld(ref path1, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
 oMissing);
            
#region 替换书签
                
object markName = "报考单位";
                Bookmark bm 
= doc.Bookmarks.get_Item(ref
 markName);
                bm.Range.Text 
=
 adStu.SiSch;
                markName 
= "报考专业"
;
                bm 
= doc.Bookmarks.get_Item(ref
 markName);
                bm.Range.Text 
=
 adStu.SiMa;
                markName 
= "考生姓名1"
;
                bm 
= doc.Bookmarks.get_Item(ref
 markName);
                bm.Range.Text 
=
 adStu.Name;
                markName 
= "考生编号"
;
                bm 
= doc.Bookmarks.get_Item(ref
 markName);
                bm.Range.Text 
=
 adStu.No;
                markName 
= "调剂专业"
;
                bm 
= doc.Bookmarks.get_Item(ref
 markName);
                bm.Range.Text 
=
 adStu.AdMa;
                markName 
= "打印时间"
;
                bm 
= doc.Bookmarks.get_Item(ref
 markName);
                bm.Range.Text 
=
 GetDate().ToString();
                markName 
= "考生姓名2"
;
                bm 
= doc.Bookmarks.get_Item(ref
 markName);
                bm.Range.Text 
=
 adStu.Name;
            
#endregion

                word.Visible 
= true;
                doc.PrintPreview();
        }

具体解释:     首先有一个模板.doc文件,在该模板内需要添加数据的地方设置成书签,.net程序所要的事情就是打开该模板,用具体需要添加的值去替换先设定好的书签,然后返回Word的打印预览页面,思路很简单~但是问题也是有滴~~

这个程序是做打印我们学院研究生办发复试生调卷函用的,但是表格是从学校研究生部那弄来的,主要还是在那边盖的章~所以就只能用那些表格来打,但是给的表格留的空实在太小,没法用同样的字体给打上去,怪就只能怪研究生部根本没想到我们这会有这个高级的东西~~哈哈~估计一般都是手工填的~~~明天过去不知道具体要怎么弄,估计这东西在打印调卷函是发挥不出作用了~~~留着以后肯定有用!

4月16日修改:

       几天前做的仔细想想还是有需要改进的地方.我前面做的只是一个个文档的填,一个个的打印,考虑资料上百个以后这样做打印机的打印的效率不高,因为每次只给打印机发送一个打印任务,它接受下一个任务是需要时间的,所以我的想法是把需要打印的文档生成成一个文档,这样不但可以保存文档,还可以只要向打印机发送一个任务就可以打印资料了,NB的打印机打印速度是很快的~~

       把具体的操作写下来吧~思路是打开两个word文档(当然,生成过程中全不可见),一个还是按照前面说的替换书签,但是不做打印处理,而是复制到另外一个文档(该文档存储所有已经生成好的表格,最后得到的就是这个word文档,打印或者保存随便他们).具体的代码如下:

 

先在using里引如:
using  Range  =  Microsoft.Office.Interop.Word.Range;

doc.Content.Copy();    
// 复制doc里的内容,doc是每次做替换书签的文档.
object  save  =   false ;               
doc.Close(
ref  save,  ref  oMissing,  ref  oMissing);    // 关闭doc.
Range range  =  printDoc.Content;    // 全中printDoc里的全部内容,printDoc是目标保存文档.
range.Collapse( ref  oMissing);    // 确定粘贴的位置,其实这个地方我想放一个有效的参数,把粘贴的位置设为文档的末尾,而不是用默认的文档的最前面,但是帮助文档是VB写的,传了一个Direction:=wdCollapseEnd的东东,我弄半天没弄出来,亏我还是懂VB的~~伤心~以至于我都忘了传个空值过去,卡了老半天!谢谢某人和我发了几条短信,就一下把我打通了~
range.Paste();     // 粘贴到目标位置,一次填写就完成了.

 

        虽然是这样,但是在生成目标文档的时候也是很耗资源的,我看了一下那程序竟然占了100多M的内存!!而且~~而且~~我的机器在前两次实验都没顶住!死机了~~当时真是在抓狂了~谁知道后来几次竟然无声无息的成功!可是想想学院那机器能扛住么~~哎继续优化吧!这次把界面也改了一下,上面的生成在一的单独线程里,以至于主界面不会像死了样的,而且在主界面上还做了个显示生成状态的Lable,这东西还真管用~后面两次的实验,看着已生成数目超过前两次死机的数目~~紧张的心情缓解了~再看着数目超过100~~高兴了~最后慢慢接近完成数目时~~兴奋得抓狂!!!!!哈~~

      哎~废话多了点~有用的就那么几行代码~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先机子上安装有office,在COM中添加引用Microsoft.Word.11.0.Object.Library(或11.0以上) Microsoft.Office.Interop.Word.Application myWordApp = null; Microsoft.Office.Interop.Word.Document doc = null; object Filename = path + "\\" + TaskID + ".docx";//目的文件 object templateFile = System.Windows.Forms.Application.StartupPath + @"\Template.docx";//模板文件,有一个五列一行的表 System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), true);//模板WORD中有一个五列的表头,分别是卡号,串口号,发送指令条数,接收指令条数,收发成功率 myWordApp = new Microsoft.Office.Interop.Word.Application(); doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /////显示页码 object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; object oFirstPage = true; oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignment, ref oFirstPage); myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; for (int i = 2; i < 102; i++)//举例100台 { doc.Tables[1].Rows.Add(ref Nothing);//表格增加一行 doc.Tables[1].Cell(i, 1).Range.Text = "250297";//卡号 doc.Tables[1].Cell(i, 2).Range.Text = "COM12";//串口号 doc.Tables[1].Cell(i, 3).Range.Text = "100";//发送指令条数 doc.Tables[1].Cell(i, 4).Range.Text = "99";//接收指令条数 doc.Tables[1].Cell(i, 5).Range.Text = "99%";//收发成功率 } doc.SaveAs(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;//不保存挂起的更改 ////下面是直接打印,文档不显示 //doc.PrintOut(ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object readOnly=false; object isVisable = true;////文档打开状态为可视 doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothing, ref Nothing); doc.PrintPreview();//打印预览

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值