Click here to Skip to main content
14,880,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I was trying to export mt aspx page to word format using the below code,

pageObject.Response.Clear();
pageObject.Response.AddHeader("content-disposition", "attachment; filename=FileName.doc");
pageObject.Response.Charset = "";
pageObject.Response.Cache.SetCacheability(HttpCacheability.NoCache);
pageObject.Response.ContentType = "application/doc";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
myDiv.RenderControl(htmlWrite);
pageObject.Response.Write(stringWrite.ToString());
pageObject.Response.End();



Code was produced the output but whatever CSS has been applied to the page which are not applied to the word document(output doc).

Kindly help me.

Thanks .
Posted

C#
StringBuilder strHTMLContent = new StringBuilder();
            //appending the style sheet 
            strHTMLContent.Append("<style>.tableHeader{background-color:#CCCCCC;font-weight:bold; }.style1 { width: 100%;border-collapse:collapse;}.firstHeader { background-color: #3366CC; font-weight: bold;color: #FFFFFF;}</style>".ToString());
            
             //strHTMLContent.Append("<style>.firstHeader { background-color: #3366CC; font-weight: bold;color: #FFFFFF;}</style>".ToString());
        
            pageObject.Response.Clear();
            pageObject.Response.AddHeader("content-disposition", "attachment; filename=FileName.doc");
            pageObject.Response.Charset = "";
            pageObject.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            pageObject.Response.ContentType = "application/doc";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            myDiv.RenderControl(htmlWrite);

            pageObject.Response.Write(strHTMLContent.Append(stringWrite).ToString());   // send the stringWrite          
            pageObject.Response.End();
   

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