Create Word File using ASP.NET C#


Create word File using ASP.NET C# , by writing only some code of lines ,this is very easy code for generating Reporting using without third party control.

Create word File



Hi, Friends

this code is help to Creating a MS word file using ASP.NET C#
//I have not using any third party control , only use of some ASp.NET Controls
//basically this report is using basic HTML code

first Step



Create a ASP.NET C# website Add a webform and place two command buttons,one Literal Control
first command button for showing report and another for exporting into word File



//in .aspx.cs file add assembly file

using System.Text;

//




public partial class Print_Word : System.Web.UI.Page
{

//create object of StringBuilder

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

//create a simple HTML Table using basic HTML Tags
//append all tags into string variable

protected void Page_Load(object sender, EventArgs e)
{
str.Append("<table align='center' border='1' bordercolor='#00aeef' width='99%' class='reporttable1' cellspacing='0' cellpadding='0' style='font-size:10;'>");
str.Append("<tr>");
str.Append("<td>");
str.Append("<b>S.No</b>");
str.Append("</td>");
str.Append("<td>");
str.Append("<b>Name</b>");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td>");
str.Append("1");
str.Append("</td>");
str.Append("<td>");
str.Append("Shubhang Mathur");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td>");
str.Append("2");
str.Append("</td>");
str.Append("<td>");
str.Append("Shubhang Sahai Mathur");
str.Append("</td>");
str.Append("</tr>");
str.Append("</table>".ToString());
}

//for exporting word file
//this code indicates the report will export into word file

protected void Button2_Click(object sender, EventArgs e)
{
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 class=Section1>");
str1.Append("<DIV style='font-size:12px;'>");
str1.Append(str.ToString());
str1.Append("</div></body></html>");
string strFile = "Text_word.doc";
string strcontentType = "application/word";
Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = strcontentType;

//file open mode with file name

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

}




//for printing report on page


protected void Button1_Click(object sender, EventArgs e)
{
Literal1.Text = str.ToString();
}
}



//run the code


Attachments

  • source code (38219-7018-Create-Word.rar)
  • Related Articles

    Export to excel

    this is a code snippet for downloading gridview contents to excel

    Create Excel File using ASP.NET C#

    Create Excel File using ASP.NET C# , by writing only some code of lines ,this is very easy code for generating Reporting using without third party control.

    More articles: Export to word Create Word file Create doc file String Builder

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: