Export Html string to word in asp.net

This is a simple blog to demonstrate how to export hmtl string to MS Word.
So here i going to create a function with parameter filename and htmlstring.

public void downloadAsWord(String filename, String htmlstring)
    {
        HttpContext.Current.Response.Clear();

        HttpContext.Current.Response.ContentType = "application/msword";

        HttpContext.Current.Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;

        HttpContext.Current.Response.Charset = "UTF-8";

        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".doc");

        Response.Write("<html>");

        Response.Write("<head>");

        Response.Write("<META HTTP-EQUIV=Content-Type CONTENT=text/html charset=UTF-8>");

        Response.Write("<meta name=ProgId content=Word.Document>");

        Response.Write("<meta name=Generator content=Microsoft Word 9>");

        Response.Write("<meta name=Originator content=Microsoft Word 9>");



        Response.Write("</head>");

        Response.Write("<body>");

         Response.Write(htmlstring); //String come here

        Response.Write("</body>");

        Response.Write("</html>");

        HttpContext.Current.Response.End();

        HttpContext.Current.Response.Flush();
    }


Call the above function here below on button click

    protected void Button1_Click(object sender, EventArgs e)
    {
String Htmlstring="<ul><li>dsfsdaf <strong>sdfas sdfdsaf <em>dsfas</em></strong></li></ul>";
        downloadAsWord("FileName",Htmlstring);
    }

Comments

Popular posts from this blog

Multipart/form-data using basic auth

How to Capture image of HTML5 camera in asp.net

Simple API that receive MultipartData File with parameter in ASP.net Web API