{"id":17,"date":"2011-04-04T15:57:06","date_gmt":"2011-04-04T05:57:06","guid":{"rendered":"http:\/\/www.reenadu.com\/?p=17"},"modified":"2011-04-04T15:57:06","modified_gmt":"2011-04-04T05:57:06","slug":"asp-net-dynamically-generate-ms-word-document","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=17","title":{"rendered":"ASP.net dynamically generate MS Word document (1)"},"content":{"rendered":"<p>Currently I am using ASP.net and working on a web project, which requires dynamically generate Microsoft Word submission document. The generated document must be specific format, and include page number, last print date, company logo, etc.<\/p>\n<p>At first, I was thinking of using <a href=\"http:\/\/en.wikipedia.org\/wiki\/Component_Object_Model\" target=\"_blank\">COM<\/a> (<strong>Component Object Model)<\/strong>. Because MS Word can comfortably work with .Net framework, .Net provide easy runtime callable wrapper. But it requires MS Word to be installed at server side, and it may slow down web server running. I have to give up.<\/p>\n<p>And then I was looking for 3<sup>rd<\/sup> party .Net library generate MS Word. It\u2019s not a cheap solution either.<\/p>\n<p>After google, finally I found out that MS Word is compatible with HTML file format. As long as you rename .htm to .doc. MS Word can easily open. What I can do is to create a html template and then filled out the dynamic sections and response back to end user.<\/p>\n<p>Here is the code snap shot:<\/p>\n<pre lang=\"csharp\">protected void Page_Load(object sender, EventArgs e)\n{\n  if (this.Request[\"projectID\"] != null)\n  {\n    \/\/fill dynamic section\n    this.catlist.DataBind();\n    Response.Clear();\n    \/\/add word document header to response\n    Response.AddHeader(\"content-disposition\", \"attachment;filename=Submission.doc\");\n    Response.Charset = \"utf-8\";\n    Response.ContentType = \"application\/ms-word\";\n    \/\/flush out whole page to word document\n    System.IO.StringWriter stringWrite = new System.IO.StringWriter();\n    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);\n    this.Page.RenderControl(htmlWrite);\n    Response.Write(stringWrite.ToString());\n    Response.Flush();\n    Response.End();\n  }\n}<\/pre>\n<p>I will keep posting the problem that I met and the solution in the next few posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently I am using ASP.net and working on a web project, which requires dynamically generate Microsoft Word submission document. The generated document must be specific format, and include page number, last print date, company logo, etc. At first, I was thinking of using COM (Component Object Model). Because MS Word can comfortably work with .Net &hellip; <a href=\"https:\/\/nickdu.com\/?p=17\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;ASP.net dynamically generate MS Word document (1)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}