Sometimes we require to export our data to doc file. I am explaining you how we can do this using C# .

I`ll Give you A Simple Example How You Can Do This..

______________________________________________________________________________________

Now i will explain it with 2 approaches :

1. When we want to save automatically(using FileStream)

2.when we want to download file.

______________________________________________________________________________________

1st Method  . You Have To Write this code in your .cs file

private void Export_to_Word()

{

StringBuilder strBuilder = new StringBuilder();
strBuilder.Append(“<h2 title=’Header’ align=’Center’> Writing To Word Using ASP.NET</h2> “.ToString());

strBuilder.Append(“<br>”.ToString());
strBuilder.Append(“<table align=’Center’>”.ToString());
strBuilder.Append(“<tr>”.ToString());

strBuilder.Append(“<td style=’width:100px;color:black’> <b>DotNetGeekBlog</b></td>”.ToString());

strBuilder.Append(“<td style=’width:100px;color:red’>India</td>”.ToString());
strBuilder.Append(“</tr>”.ToString());
strBuilder.Append(“</table>”.ToString());

string strPath = Request.PhysicalApplicationPath + “\\document\\DotNetGeekBlog.doc”;
FileStream fStream = File.Create(strPath);
fStream.Close();
StreamWriter sWriter = new StreamWriter(strPath);
sWriter.Write(strBuilder);
sWriter.Close();

}

_______________________________________________________________________________________

______________________________________________________________________________________

2nd Method  . You Have To Write this code in your .cs file

private void Export_to_Word()

{

StringBuilder strBuilder = new StringBuilder();
strBuilder.Append(“<h1 title=’Header’ align=’Center’> Writing To Word Using ASP.NET</h1> “.ToString());

strBuilder.Append(“<br>”.ToString());
strBuilder.Append(“<table align=’Center’>”.ToString());
strBuilder.Append(“<tr>”.ToString());

strBuilder.Append(“<td style=’width:100px;color:green’> <b>Name</b></td>”.ToString());

strBuilder.Append(“<td style=’width:100px;color:red’>India</td>”.ToString());
strBuilder.Append(“</tr>”.ToString());
strBuilder.Append(“</table>”.ToString());

Response.Clear();
Response.ContentType = “text/doc”;
Response.AddHeader(“Content-Disposition”, “attachment;filename=DotNetGeekBlog.doc”);
Response.Write(strBuilder.ToString());
Response.End();

}

_______________________________________________________________________________________

This will convert your string into doc. You can download it and Save it.

This Is Basic , Now You Can Modify This Code as You Want 🙂

Hope This Post Will Help you Somehow . . :) . Feel Free To Ask Anything You Need.

Waiting For Your Valuable Feedback.

Advertisements