locked
How to Generate word document programatically using C#?? RRS feed

  • Question

  • User-2008110961 posted

    I have a webform designed using ASP.NET in that i have some table data and some information.Now,my problem is to generate a report in MSWORD based on  webform table data and display it to the User.


    Plz help me....

    Friday, June 25, 2010 3:27 AM

Answers

  • User-2008110961 posted

    I got d answer to my problem after googling many sites....here s the answer....

    1st way s using HTML approach....

    StringBuilder str = new StringBuilder();
    StringBuilder str1 = new StringBuilder();


    str.Append("<table border='0' cellpadding='0' cellspacing='0' class='textbold' width='100%' >");

    str.Append("<tr >");

            str.Append("<td style='width:140px'>");
            str.Append("Sl No");
            str.Append("</td>");
            str.Append("<td style='width:20px'>");
            str.Append("Name");
            str.Append("</td>");
            str.Append("<td style='width:200px'>");
            str.Append("Age");
            str.Append("</td>");
            str.Append("<td style='width:500px'>");
            str.Append("</td>");

          str.Append("</tr >");


    str.Append("</table>".ToString());


     str1.Append(@"<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'><head><title>Time</title>");
            str1.Append(@"<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div>");
            str1.Append("<DIV  style='font-size:12px;'>");
            str1.Append(str.ToString());
            str1.Append("</div></body></html>");
            string strFile = "Text.doc";
            string strcontentType = "application/word";
            Response.ClearContent();
            Response.ClearHeaders();
            Response.BufferOutput = true;
            Response.ContentType = strcontentType;

            Response.AddHeader("Content-Disposition", "attachment; filename=" + strFile);
            Response.Write(str1.ToString());
            Response.Flush();
            Response.Close();
            Response.End();



    2nd way is using Microsoft.Office.Interop.Word Namespace functionality...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 26, 2010 8:49 AM