zoukankan      html  css  js  c++  java
  • 通过Word实现表单套打

    1.在Word中做好表单模板,在对应的地方设置好对应的关键字(Key);

    2.添加扩展Microsoft.Office.Interop.Word.dll(添加引用->程序集->扩展);

    3.替换代码:

     1 public partial class _Default : Page
     2     {
     3         private  Dictionary<string, string> dic = new Dictionary<string, string>();
     4         protected void Page_Load(object sender, EventArgs e)
     5         {
     6             dic.Add("[文别]", "测试数据源");
     7             dic.Add("[密级]", "测试数据源");
     8             dic.Add("[缓急]", "测试数据源");
     9             dic.Add("[时限]", "测试数据源");
    10             dic.Add("[文件标题]", "测试数据源");
    11             dic.Add("[附件]", "测试数据源");
    12             dic.Add("[主送]", "测试数据源");
    13             dic.Add("[抄送]", "测试数据源");
    14             dic.Add("[签发]", "测试数据源");
    15             dic.Add("[分管领导意见]", "测试数据源");
    16             dic.Add("[办公室审核]", "测试数据源");
    17             dic.Add("[会办处室意见]", "测试数据源");
    18             dic.Add("[主板处室负责人意见]", "测试数据源");
    19             dic.Add("[主板处室拟稿人]", "测试数据源");
    20             dic.Add("[联系电话]", "测试数据源");
    21             WordReplace(@"E:上海市农业委员会办文单-新版.doc", "[文别]", "[新文别]");
    22         }
    23 
    24         /// <summary>
    25         /// 替换word中的文字
    26         /// </summary>
    27         /// <param name="filePath">文件的路径</param>
    28         /// <param name="strOld">查找的文字</param>
    29         /// <param name="strNew">替换的文字</param>
    30         private void WordReplace(string filePath, string strOld, string strNew)
    31         {
    32             Microsoft.Office.Interop.Word._Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
    33             object nullobj = System.Reflection.Missing.Value;
    34             object file = filePath;
    35             Microsoft.Office.Interop.Word._Document doc = app.Documents.Open(
    36             ref file, ref nullobj, ref nullobj,
    37             ref nullobj, ref nullobj, ref nullobj,
    38             ref nullobj, ref nullobj, ref nullobj,
    39             ref nullobj, ref nullobj, ref nullobj,
    40             ref nullobj, ref nullobj, ref nullobj, ref nullobj) as Microsoft.Office.Interop.Word._Document;
    41 
    42             foreach (var item in dic)
    43             {
    44                 app.Selection.Find.ClearFormatting();
    45                 app.Selection.Find.Replacement.ClearFormatting();
    46                 app.Selection.Find.Text = item.Key;
    47                 app.Selection.Find.Replacement.Text = item.Value;
    48 
    49                 object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    50                 app.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
    51                                            ref nullobj, ref nullobj, ref nullobj,
    52                                            ref nullobj, ref nullobj, ref nullobj,
    53                                            ref nullobj, ref objReplace, ref nullobj,
    54                                            ref nullobj, ref nullobj, ref nullobj);
    55             }
    56 
    57             //app.Selection.Find.ClearFormatting();
    58             //app.Selection.Find.Replacement.ClearFormatting();
    59             //app.Selection.Find.Text = strOld;
    60             //app.Selection.Find.Replacement.Text = strNew;
    61 
    62             //object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    63             //app.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
    64             //                           ref nullobj, ref nullobj, ref nullobj,
    65             //                           ref nullobj, ref nullobj, ref nullobj,
    66             //                           ref nullobj, ref objReplace, ref nullobj,
    67             //                           ref nullobj, ref nullobj, ref nullobj);
    68 
    69             //格式化
    70             //doc.Content.AutoFormat();
    71             //清空Range对象
    72             //Microsoft.Office.Interop.Word.Range range = null;
    73 
    74             //保存
    75             doc.SaveAs(@"E:	est.doc");//.Save();
    76 
    77             //Microsoft.Office.Interop.Word.Range range = null;
    78             doc.Close(ref nullobj, ref nullobj, ref nullobj);
    79 
    80             app.Quit(ref nullobj, ref nullobj, ref nullobj);
    81         }
    82     }
  • 相关阅读:
    CodeForces 659F Polycarp and Hay
    CodeForces 713C Sonya and Problem Wihtout a Legend
    CodeForces 712D Memory and Scores
    CodeForces 689E Mike and Geometry Problem
    CodeForces 675D Tree Construction
    CodeForces 671A Recycling Bottles
    CodeForces 667C Reberland Linguistics
    CodeForces 672D Robin Hood
    CodeForces 675E Trains and Statistic
    CodeForces 676D Theseus and labyrinth
  • 原文地址:https://www.cnblogs.com/yanjc/p/5439065.html
Copyright © 2011-2022 走看看