惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
Apple Machine Learning Research
Apple Machine Learning Research
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
Security Latest
Security Latest
Cloudbric
Cloudbric
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Know Your Adversary
Know Your Adversary
A
Arctic Wolf
L
LangChain Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
小众软件
小众软件
NISL@THU
NISL@THU
云风的 BLOG
云风的 BLOG
P
Privacy & Cybersecurity Law Blog
S
Security @ Cisco Blogs
博客园 - 【当耐特】
I
InfoQ
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
O
OpenAI News
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
量子位
宝玉的分享
宝玉的分享

博客园 - David

TFS Server 不存在时,如何去除对应Server的Workspaces 更改已有程序集中的资源 记录:如何删除TFS Project SQL – TRUNCATE vs DELETE SQL – Character data (varchar) 又好久没写东西了! Powershell: Send mail with attachment 【Update】 - David SSIS: Execute SSIS Package in command prompt 【Update】 随感 Windows Workflow Foundation 了解 通过代码将Word 2007 template (dotx)文档转换Word 2007 (docx)文档 创建Word2007文档而无需安装Word2007 Visual WebGUI - 如何处理MessageBox的DialogResult值 Visual WebGUI - 如何将Dialog的结果传回主窗体 Visual WebGUI Report - 导出到PDF Visual WebGUI 在 Vista IIS 7 中的配置 项目管理片语 - Schedule 在IIS 7设置ASP.NET 1.1(在Vista中使用VS2003) 给儿子的歌!
通过代码将Word 2007 template (dotx)文档转换Word 2007 (docx)文档(续)
David · 2008-06-28 · via 博客园 - David

在转换中间,需要把template中的Mail Merge Field去掉。Mail Merge Field的字符格式是:'MERGEFIELD  MyFieldName  \\* MERGEFORMAT',而对应的XML Node为:

   1: <w:fldSimple w:instr="MERGEFIELD FIELD_SYSTEM_DATE \m \* MERGEFORMAT">
   2:     <w:r>
   3:         <w:rPr>
   4:             <w:noProof /> 
   5:         </w:rPr>
   6:         <w:t>«FIELD_SYSTEM_DATE»</w:t> 
   7:     </w:r>
   8: </w:fldSimple>

我们要想办法把这个Node去掉,并且用

   1: <w:r>
   2:   <w:t>System Date</w:t> 
   3: </w:r>

来代替。

因为在之前的文章,我们可以知道dotx中的document.xml已经得到并且处理为一个XmlDocument Object了。我们可以通过这个XmlDocument来处理相关的节点。

当然是先设置一个XmlNamespaceManager:

   1: const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
   2:  
   3: //  Manage namespaces to perform Xml XPath queries.
   4: NameTable nt = new NameTable();
   5: XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
   6: nsManager.AddNamespace("w", wordmlNamespace);

然后,将之前的XmlDocument Object处理一下:

   1: // parse the document.xml
   2: XmlDocument xdoc = new XmlDocument(nt);
   3: xdoc.Load(documentPart.GetStream());

接着就该把Mail Merge Field分离了:

   1: int iTotalFields = 0;
   2:  
   3: XmlNodeList nodeList = xdoc.SelectNodes("//w:fldSimple", nsManager);
   4: foreach (XmlNode node in nodeList)
   5: {
   6:     // Create text node
   7:     XmlNode newNode = xdoc.CreateNode(XmlNodeType.Element, "w", "r", nsManager.LookupNamespace("w"));
   8:     XmlNode newChildNode = xdoc.CreateNode(XmlNodeType.Element, "w", "t", nsManager.LookupNamespace("w"));
   9:  
  10:     String fieldText = node.Attributes[0].Value;
  11:  
  12:     // only getting the mailmerge fields
  13:     if (fieldText.StartsWith(" MERGEFIELD"))
  14:     {
  15:         // The text comes in the format of 'MERGEFIELD  MyFieldName  \\* MERGEFORMAT'
  16:         // This has to be edited to get only the fieldname "myfieldname"
  17:         Int32 endMerge = fieldText.IndexOf("\\");
  18:         Int32 fieldNameLength = fieldText.Length - endMerge;
  19:         String fieldName = fieldText.Substring(11, endMerge - 11);
  20:  
  21:         // Gives the fieldnames as the user had entered in .dot file
  22:         fieldName = fieldName.Trim();
  23:  
  24:         // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//
  25:         switch (fieldName)
  26:         {
  27:             case "FIELD_SYSTEM_DATE":
  28:                 newChildNode.InnerText = DateTime.Now.ToString("dd/MM/yyyy");
  29:                 break;
  30:         }
  31:     }
  32:     newNode.AppendChild(newChildNode);
  33:  
  34:     node.ParentNode.ReplaceChild(newNode, node);
  35:  
  36:     iTotalFields++;
  37: }

这样就处理好了有Mail merge field的Word template文档了。