Export Data To Word Using Asp.net C#

Posted: July 22, 2013 in ASP.net
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
Comments
  1. Hari says:

    Word document is not opening once writes the data to word. Getting the File corrupted error.

  2. With havin so much content do you ever run into any problems of plagorism or copyright violation? My site has a lot of completely unique content I’ve either authored myself or outsourced but it looks like a lot of it is popping it up all over the web without my agreement. Do you know any techniques to help reduce content from being stolen? I’d genuinely appreciate it.

  3. Thanks for another informative blog. Where else may just I am getting that type of information written in such an ideal manner? I’ve a challenge that I’m simply now operating on, and I’ve been on the look out for such information.

  4. You made some respectable factors there. I looked on the internet for the issue and located most people will associate with along with your website.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s