locked
Code - Export To MSWord (Print Layout) RRS feed

  • Question

  • User-1832868798 posted

    Below is the code to use to Export the ASP.Net Page to MS-Word.
    The below code has a feature that it exports in 'Print Layout' mode and not in Web-Layout.
    For this feature to work you need to remove the <header> tag from ASPX.

    string fileNameWord = "PrintView.doc";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindPage(); // Your codt e to bind the page
            ExportToMSWord(fileNameWord);
        }
    }

    public void ExportToMSWord(string fileNameWord)
    {
        string strBody = string.Empty;
        strBody = @"<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
        "xmlns:w='urn:schemas-microsoft-com:office:word'" +
        "xmlns='http://www.w3.org/TR/REC-html40'>";

        strBody = strBody + "<!--[if gte mso 9]>" +
        "<xml>" +
        "<w:WordDocument>" +
        "<w:View>Print</w:View>" +
        "<w:Zoom>100</w:Zoom>" +
        "</w:WordDocument>" +
        "</xml>" +
        "<![endif]-->";

        string attachment = "attachment; filename= " + fileNameWord;
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.RenderControl(htw);
        Response.Write(strBody + sw.ToString());
        Response.End();
    }


    Regards,
    Arun Manglick

    Wednesday, October 13, 2010 5:10 AM

All replies