C# Code to Generate Word Document from a Web Page

protected void Generate_Click(object sender, EventArgs e)
{
    string str = "
http://www.google.co.in/";
    HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(str);
    HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();

    // Make sure the response is valid
    byte[] MyBuffer = null;

    // Make sure the response is valid
    if (HttpStatusCode.OK == MyResponse.StatusCode)
    {
        // Open the response stream
        using (Stream MyResponseStream = MyResponse.GetResponseStream())
        {
            byte[] buffer = new byte[10000];

            using (MemoryStream memoryStream = new MemoryStream())
            {
                int
                count = 0;

                do
                {
                    count = MyResponseStream.Read(buffer, 0, buffer.Length);
                    memoryStream.Write(buffer, 0, count);
                }
                while (count != 0);
                MyBuffer = memoryStream.ToArray();
            }
        }
    }
    Response.ContentType = "application/ms-word";
    Response.AddHeader("content-Disposition", "attachment; filename=GhouseBarqSample.doc");
    Response.BinaryWrite(MyBuffer);
    Response.Flush();
    Response.Close();
    Response.ClearHeaders();
    Response.End();
}

Comments

Popular posts from this blog

Convert XElement to DataTable

Enable mouse scroll-wheel in VB6

C# code to Check IE Proxy Settings