Click here to Skip to main content
15,891,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I am trying to convert a HTML to Word, the images are not displaying in the Word file. Please any one provide a solution.
My Code Snippet is:
C#
private void SaveToWord()
		{
                Response.ClearContent();
                Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
                Response.ContentType = "application/vnd.ms-word";
                Response.Charset = string.Empty;
                this.EnableViewState = false;
                Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:word\">");
                Response.Write("\r\n");
                string CSSFileName = System.Web.HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationSettings.AppSettings["SaveCssFile"]));
                if (System.IO.File.Exists(CSSFileName))
                {
                    FileStream file = new FileStream(CSSFileName, FileMode.OpenOrCreate, FileAccess.Read);
                    StreamReader sr = new StreamReader(file);
                    string s = sr.ReadToEnd();
                    Response.Write("<style>" + s + "\r\n" + "</style>");

                    using (StringWriter strWriter = new StringWriter())
                    {
                        using (HtmlTextWriter htmWriter = new HtmlTextWriter(strWriter))
                        {
                            imgCandidate.Src = imgCandidate.Src;
                            imgWork1.Src = imgWork1.Src;
                            imgWork1.Width = 30;
                            imgWork2.Src = imgWork2.Src;
                            imgWork2.Width = 30;
                            imgWork3.Src = imgWork3.Src;
                            imgWork3.Width = 30;
                            trPreview.RenderControl(htmWriter);
                            tblProfileDetails.RenderControl(htmWriter);
                            Response.AppendHeader("content-disposition", "attachment;filename=Profile.doc");
                            Response.Write(Convert.ToString(strWriter));
                            Response.Flush();
                            Response.End();
                        }
                    }
                }
		}

Thank You,
Posted
Updated 23-Apr-12 1:27am
v3
Comments
Sandeep Mewara 23-Apr-12 7:28am    
Is there any error? What exactly is displayed, a small box with cross?

1 solution

Hey,

try following code to convert html to word
C#
HttpContext.Current.Response.Clear();  
     HttpContext.Current.Response.Charset = "";  
     HttpContext.Current.Response.ContentType = "application/msword";  
     string strFileName = "GenerateDocumentfgfgfg" + ".doc";  
     HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);  
     StringBuilder strHTMLContent = new StringBuilder();  
     strHTMLContent.Append(RadEditor1.Content); 
     -- RadEditor1.Content is HTML Text  I m taking it from editor.
 
     HttpContext.Current.Response.Write(strHTMLContent);  
     HttpContext.Current.Response.End();  
     HttpContext.Current.Response.Flush();


Best Luck
Happy Coding :)
 
Share this answer
 
Comments
maajanes 23-Apr-12 7:22am    
Hi,
My code snippet is:

private void SaveToWord()
{
Response.ClearContent();
Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
Response.ContentType = "application/vnd.ms-word";
Response.Charset = string.Empty;
this.EnableViewState = false;
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:word\">");
Response.Write("\r\n");
string CSSFileName = System.Web.HttpContext.Current.Server.MapPath(Convert.ToString(ConfigurationSettings.AppSettings["SaveCssFile"]));
if (System.IO.File.Exists(CSSFileName))
{
FileStream file = new FileStream(CSSFileName, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
Response.Write("<style>" + s + "\r\n" + "</style>");

using (StringWriter strWriter = new StringWriter())
{
using (HtmlTextWriter htmWriter = new HtmlTextWriter(strWriter))
{
//imgSchool.Src = imgSchool.Src;
imgCandidate.Src = imgCandidate.Src;
imgWork1.Src = imgWork1.Src;
imgWork1.Width = 30;
imgWork2.Src = imgWork2.Src;
imgWork2.Width = 30;
imgWork3.Src = imgWork3.Src;
imgWork3.Width = 30;
trPreview.RenderControl(htmWriter);
tblProfileDetails.RenderControl(htmWriter);
Response.AppendHeader("content-disposition", "attachment;filename=Profile.doc");
Response.Write(Convert.ToString(strWriter));
Response.Flush();
Response.End();
}
}
}

}
Can you tell me an exact solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900