Step 1- Click on Print button
private void
button1_Click(object sender, EventArgs e)
{
StringBuilder
strBuilder = new StringBuilder();
strBuilder.Append("<h1 title='Header' align='Center'>Writing To
Word File using windows form in c#</h1> ".ToString());
strBuilder.Append("<br>".ToString());
strBuilder.Append("<table border='1' align='Center'>".ToString());
strBuilder.Append("<tr>".ToString());
strBuilder.Append("<td
style='width:30%;color:green'><b>Krishna Kumar
Chaturvedi</b></td>".ToString());
strBuilder.Append("<td
style='width:30%;color:red'>India</td>".ToString());
strBuilder.Append("<td style='width:30%;color:red'>Software
developer</td>".ToString());
strBuilder.Append("</tr>".ToString());
strBuilder.Append("</table>".ToString());
string
datestyle = @"<style>
.nf{mso-number-format:\@;}</style>";
HttpResponse Response =
HttpContext.Current.Response;
string
filename = "Test" + "_" + System.DateTime.Now.ToString("MMddyy_hhmmss") + ".doc";
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.Charset = "";
Response.ContentType = "application/xls";
System.IO.StringWriter
WriteItem = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter
htmlText = new HtmlTextWriter(WriteItem);
Response.Write(datestyle);
Response.Write(strBuilder.ToString());
Response.End();
}
//Result
No comments:
Post a Comment