-2

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)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
mjyazdani
  • 2,110
  • 6
  • 33
  • 64

1 Answers1

6
 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();  
mjyazdani
  • 2,110
  • 6
  • 33
  • 64
Akshay Randive
  • 384
  • 2
  • 10
  • Your code just saves some text in word! or I misunderstood it? I want to change the html style to word style... not to save the html text with it's tags in word... – mjyazdani Nov 27 '14 at 07:26
  • The code will save the HTML formatted text to word you need to apply in-line CSS to html and then call above code... if you need to format your content in Word as per MS-Word styles then you can consider using somethisng like [OpenXML](http://openxmldeveloper.org/) – Akshay Randive Nov 27 '14 at 09:58
  • I used `sdgdbfg nf
    gnfgn` as htmlContent and it saved `
    ` in the word! is it something wrong with my html?
    – mjyazdani Nov 29 '14 at 03:50
  • you can try replacing '
    ' with '' in your html
    – Akshay Randive Nov 29 '14 at 08:36
  • I have this html : `

    hi every body


    ` and it get me `

    hi every body

    ` in word document. it do'nt change any thing!
    – mjyazdani Nov 29 '14 at 09:27
  • 1
    this is working now `strHTMLContent.Append(""); strHTMLContent.Append(htmlContent); strHTMLContent.Append("");` add these lines to your code – Akshay Randive Nov 29 '14 at 10:21
  • 1
    thanks a lot ... it worked! I have another question now... Can I append an external css file to it? (I have a css file for my editor that handles the appearance of my editor content and I should make that changes in word file too) – mjyazdani Nov 29 '14 at 10:47
  • noyou cannot append external css but you can define css classes in html style tag and apply classes to your html – Akshay Randive Nov 29 '14 at 10:53