Hi All,
Below example is used to export the entire Page (DivID in our case)to Word. In the below example we are applying the css also. It will export same how it looks in Web.
Below example is used to export the entire Page (DivID in our case)to Word. In the below example we are applying the css also. It will export same how it looks in Web.
public void ExportWord(string fileName) { try { string attachment = "attachment; filename=" + fileName + ".doc"; //Setting the attachment name. HttpContext.Current.Response.ClearContent(); //clears all content output from the buffer stream. HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "application/vnd.ms-word"; using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { using (HtmlForm frm = new HtmlForm()) { DivID.Parent.Controls.Add(frm); frm.Controls.Add(DivID); frm.RenderControl(htw); string style = @"<style> .textmode { mso-number-format:\@; } </style>"; string docPath = SPContext.Current.Site.Url + "/Style%20Library/MyProject/css/Main.css"; using (SPSite oSite = new SPSite(SPContext.Current.Site.Url)) { SPWeb oWeb = oSite.OpenWeb(); SPFile tempFile = oWeb.GetFile(docPath); StreamReader reader = new StreamReader(tempFile.OpenBinaryStream()); string s = reader.ReadToEnd(); HttpContext.Current.Response.Write("<HEAD><STYLE>"); HttpContext.Current.Response.Write(Convert.ToString(s)); HttpContext.Current.Response.Write("</STYLE></HEAD>"); HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Write(Convert.ToString(sw)); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } } } } } catch (Exception ex) { //Exception Handling } }
0 comments:
Post a Comment