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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Security Latest
Security Latest
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
S
Secure Thoughts
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
罗磊的独立博客
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
V
V2EX
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tenable Blog
T
Threat Research - Cisco Blogs
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
F
Full Disclosure
H
Help Net Security
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
O
OpenAI News
S
Securelist

博客园 - 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文档了。