js2word/html2word的简单实现

js2word/html2word的简单实现

以C#描述如下:
            StringBuilder sb = new StringBuilder();
            sb.Append("<html ");
            sb.Append("xmlns:o='urn:schemas-microsoft-com:office:office' ");
            sb.Append("xmlns:w='urn:schemas-microsoft-com:office:word'");
            sb.Append("xmlns='http://www.w3.org/TR/REC-html40'>");
            sb.Append("<head><title>Time</title>");

sb.Append("<!--[if gte mso 9]>");
            sb.Append("<xml>"); 
            sb.Append("<w:WordDocument>");
            sb.Append("<w:View>Print</w:View>");
            sb.Append("<w:Zoom>90</w:Zoom>"); 
            sb.Append("<w:DoNotOptimizeForBrowser/>");
            sb.Append("</w:WordDocument>");
            sb.Append("</xml>"); 
            sb.Append("<![endif]-->");

sb.Append("<style>");
            sb.Append("<!-- /* Style Definitions */");
            sb.Append("@page Section1");
            sb.Append("   {size:8.5in 11.0in; ");
            sb.Append("   margin:1.0in 1.25in 1.0in 1.25in ; ");
            sb.Append("   mso-header-margin:.5in; ");
            sb.Append("   mso-footer-margin:.5in; mso-paper-source:0;}");
            sb.Append(" div.Section1");
            sb.Append("   {page:Section1;}");
            sb.Append("-->");
            sb.Append("</style></head>");

sb.Append("<body lang=EN-US style='tab-interval:.5in'>");
            sb.Append("<div class="Section1">");
            sb.Append("<h1>Time and tide wait for none</h1>"); 
            sb.Append("<p style='color:red'><I>");
            sb.Append(DateTime.Now + "</I></p>");
            sb.Append( "<img src='http://dj9okeyxktdvd.cloudfront.net/App_Themes/CodeProject/Img/logo250x135.gif'>" );
            sb.Append("</div></body></html>");

Response.AppendHeader("Content-Type", "application/msword");
            Response.AppendHeader( "Content-disposition", "attachment; filename=myword.doc" );
            Response.Write(sb.ToString());

 
Javascript版本请参考如下例子,自行修改:
上一篇:执行命令行, 并获取输出字符(比如OSQL)


下一篇:mongodb分组排序