{"id":2949,"date":"2016-03-19T07:31:08","date_gmt":"2016-03-19T07:31:08","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=2949"},"modified":"2016-03-19T07:33:09","modified_gmt":"2016-03-19T07:33:09","slug":"generating-open-xml-documents-using-t4-text-templates","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/generating-open-xml-documents-using-t4-text-templates\/","title":{"rendered":"Generating Open XML Documents using T4 Text Templates"},"content":{"rendered":"<div>\n<p>This is a fun project that I&#8217;ve been meaning to do for some time &#8211; and I finally found the time in the last week. &nbsp;T4 Text Templates are a very cool technology in Visual Studio 2008 and 2010 that make it super-easy to generate C# code, XML, or other text in an automated fashion as part of the editing experience in Visual Studio. &nbsp;The way they work is that you write a text template that consists of a mixture of text and code. &nbsp;The text can be C# code, XML, HTML, or just about any kind of text that you can imagine. &nbsp;The code can be C# or VB.NET &#8211; the code is embedded in special code blocks that are delimited by &lt;# and #&gt;. &nbsp;Then, when you save the template in Visual Studio, at that point in time, right as you save it, Visual Studio transforms the template into a complete program, compiles it, runs it, and then saves the output in a file in your project.<\/p>\n<p>This is a natural technology to apply to Open XML document generation. In this video, I create a text template from some LINQ to XML code, and a word-processing document that I saved in the <a href=\"http:\/\/blogs.msdn.com\/b\/ericwhite\/archive\/2008\/09\/29\/the-flat-opc-format.aspx\" class=\"broken_link\">Flat OPC format<\/a>. &nbsp;When you save the template, it generates automatically a word-processing document from the template. &nbsp;You can open the word-processing document immediately after saving the template.<\/p>\n<p>While it is great to edit and develop T4 templates in Visual Studio 2010, you may want to generate a document in a different context &#8211; perhaps as part of a larger process. &nbsp;You can write your own T4 template processor by writing a &#8216;Custom Host&#8217;. &nbsp;The video walks through this process and demonstrates generating a word-processing document from a command line program. &nbsp;Note that before you can build a Custom Host, you need to install the <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=2680\" class=\"broken_link\">Visual Studio 2010 SDK<\/a>.<\/p>\n<p>I kept the actual document that I generate as simple as possible, as I was more concerned about demonstrating the mechanics of using T4 templates to generate Open XML documents. &nbsp;You can take these basic ideas that I present in this video, and generate some really complicated documents with a minimum of effort.<\/p>\n<p>I&#8217;ve attached the code to this post.<\/p>\n<p><iframe loading=\"lazy\" title=\"GeneratingDocxUsingT4\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/-ezcvEa6pvo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>One last point &#8211; as I mentioned earlier in this post, in the video, I show generating a Flat OPC document. &nbsp;It is pretty simple to convert that Flat OPC document to an ordinary DOCX. &nbsp;The blog post,&nbsp;<a href=\"http:\/\/blogs.msdn.com\/b\/ericwhite\/archive\/2008\/09\/29\/transforming-flat-opc-format-to-open-xml-documents.aspx\" class=\"broken_link\">Transforming Flat OPC Format to Open XML Documents<\/a>, shows how.<\/p>\n<p>T4 templates are cool. &nbsp;I hope you enjoy watching this video as much as I enjoyed making it.<\/p>\n<p>Download &#8211; <a href=\"https:\/\/www.ericwhite.com\/blog\/wp-content\/uploads\/2016\/03\/GenerateDocxUsingT4.zip\">Example Code<\/a><\/p>\n<p>-Eric<\/p>\n<div style=\"clear:both;\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is a fun project that I&#8217;ve been meaning to do for some time &#8211; and I finally found the time in the last week. &nbsp;T4 Text Templates are a very cool technology in Visual Studio 2008 and 2010 that make it super-easy to generate C# code, XML, or other text in an automated fashion [&hellip;]<\/p>\n","protected":false},"author":10567,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"class_list":["post-2949","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/10567"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=2949"}],"version-history":[{"count":2,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2949\/revisions"}],"predecessor-version":[{"id":2952,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2949\/revisions\/2952"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=2949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}