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

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - 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-27 · via 博客园 - David

Code Project中闲逛时,让我发现了这样一篇文章Create simple Word 2007 documents without needing Word 2007 installed,里面就有提到Office 2007的文档都可以用压缩工具来查看里面的文件,也就是Office 2007的文档是用打包工具把XMLs打包在一起的。因此可以通过.net framework 3.0的System.IO.Packaging来打开docx之类的文档,处理好相关的XMLs,再打包。下面就是这样来处理一个dotx文档,转换成docx文档。

以下代码来自 http://openxmldeveloper.org/forums/post/2816.aspx

   1: const string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
   2: const string wordDOCXContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
   3: string docName = @"c:\test9\1nameis.dotx";
   4: Package wdPackage = Package.Open(docName, FileMode.Open, FileAccess.ReadWrite);
   5: PackagePart documentPart = null;
   6: Uri documentUri = null;
   7:  
   8: //  Get the main document part (document.xml).
   9: foreach (System.IO.Packaging.PackageRelationship relationship in wdPackage.GetRelationshipsByType(documentRelationshipType))
  10: {
  11:     documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
  12:     documentPart = wdPackage.GetPart(documentUri);
  13:     //  There is only one document.
  14:     break;
  15: }
  16:  
  17: XmlDocument xdoc = new XmlDocument();
  18: xdoc.Load(documentPart.GetStream());
  19:  
  20: //  Delete the document part and its relationship part.
  21: wdPackage.DeletePart(documentPart.Uri);
  22:  
  23: //  Create new document and relationship parts using the correct content types.
  24:           
  25: documentPart = wdPackage.CreatePart(documentUri, wordDOCXContentType);
  26: xdoc.Save(documentPart.GetStream(FileMode.Create, FileAccess.Write));
  27: wdPackage.Close();
  28:  
  29: string newFileName = Path.GetDirectoryName(docName) + @"\" + Path.GetFileNameWithoutExtension(docName) + ".docx";
  30: //  If the new file exists, delete it. You might
  31: //  want to make this code less destructive.
  32: if (File.Exists(newFileName))
  33: {
  34:     File.Delete(newFileName);
  35: }
  36: File.Move(docName, newFileName);
  37:  

System.IO.Packaging 相关文章:

http://msdn.microsoft.com/msdnmag/issues/06/11/BasicInstincts/default.aspx

http://blogs.msdn.com/erikaehrli/archive/2006/08/16/word2007DataDocumentGenerationPart2.aspx