Search code examples
c#htmlasp.netoffice-automation

convert HTML to word format


I use word automation to create a word document. I want to embed some html code in that file. how should I cnvert html tags to word document format? ( I want to keep font, bold, table and other styles in html)


Solution

  •  HttpContext.Current.Response.Clear();  
     HttpContext.Current.Response.Charset = "";  
     HttpContext.Current.Response.ContentType = "application/msword";  
     string strFileName = "docName" + ".doc";  
     HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);  
    
     StringBuilder strHTMLContent = new StringBuilder();  
     strHTMLContent.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:word\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head></head><body>");
     strHTMLContent.Append(htmlContent); 
     strHTMLContent.Append("</body></html>");    
    
     HttpContext.Current.Response.Write(strHTMLContent);  
     HttpContext.Current.Response.End();  
     HttpContext.Current.Response.Flush();