Read Write Edit Word Document In ASP.NET

In this article i am explaining how to Read Write Edit Word Document With FileStream StreamWriter In ASP.NET. For this i've added a textbox for input text to be written in word document and a button to write, same for reading text from word file.

The word file created is saved in a folder named document in root of application. which can be changed to desired location.

Read Write Edit Word Document In ASP.NET


html source of the page look like
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:Button ID="Button1" runat="server" 
                         onclick="Button1_Click" 
                         Text="Click to write to word" />
<br />
<asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox>
<asp:Button ID="Button2" runat="server" 
                         onclick="Button2_Click" 
                         Text="Read Word Document" />
</div>
</form>

To use Filestream and stringBuilder we need to add these namespace
using System.IO;
using System.Text;

C# code behind
protected void Button1_Click(object sender, EventArgs e)
{
    //Create stringBuilder to write formatted 
    //Text to word file
 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>amiT</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\\Test.doc";

 //string strTextToWrite = TextBox1.Text;
 FileStream fStream = File.Create(strPath);
 fStream.Close();
 StreamWriter sWriter = new StreamWriter(strPath);
 Writer.Write(strBuilder);
 sWriter.Close(); 

}
protected void Button2_Click(object sender, EventArgs e)
{
 string strPath = Request.PhysicalApplicationPath 
                  + "\\document\\Test.doc";
 FileStream fStream = new FileStream
            (strPath, FileMode.Open, FileAccess.Read);
 StreamReader sReader = new StreamReader(fStream);
 TextBox2.Text = sReader.ReadToEnd();
 sReader.Close();
Response.Write(TextBox2.Text);
}

VB.NET code
Protected Sub Button1_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim strBuilder As 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>amiT</b></td>".ToString())
strBuilder.Append("<td style='width:100px;color:red'>
                               India</td>".ToString())
strBuilder.Append("</tr>".ToString())
strBuilder.Append("</table>".ToString())
'string path = @"C:\Test.doc";

Dim strPath As String = 
Request.PhysicalApplicationPath & "\document\Test.doc"
    
'string strTextToWrite = TextBox1.Text;
Dim fStream As FileStream = File.Create(strPath)
fStream.Close()
Dim sWriter As New StreamWriter(strPath)
sWriter.Write(strBuilder)
      
sWriter.Close()
End Sub

Protected Sub Button2_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim strPath As String = 
Request.PhysicalApplicationPath & "\document\Test.doc"

Dim fStream As New FileStream
(strPath, FileMode.Open, FileAccess.Read)
Dim sReader As New StreamReader(fStream)
TextBox2.Text = sReader.ReadToEnd()
sReader.Close()
Response.Write(TextBox2.Text)
End Sub

hope this helps

Download the sample code attached


If you like this post than join us or share

20 comments:

James said...

This is no different from reading a .txt file. What about writing to .doc file with formatting of?


Rahul Mahajan said...

Thanks Man ITs usefullll


Rahul Mahajan said...

Hello dear i asking one thing i use aspose component to build the word doc. can you please help me how to copy content the word file.

Actually my aspx page contain the textbox,richtextbox,datalist control i need to copy all content in word file how to acheive this dear can you please help using VB.net.


Mikael said...

This comment has been removed by a blog administrator.


Anonymous said...

Nice Article.
Some nice tutorials on this blog. I liked them.

http://aspnetcsharp4.blogspot.com/


Unknown said...

@Anonymous:

Thanks and welcome to my site :)


Unknown said...

@James & Mikael:

I've changed the code to write formatted text to word file.

Hope this is not plain text now ;)


Anonymous said...

This comment has been removed by a blog administrator.


Anonymous said...

Dear web-master ! I looked your site and I want to say that yor very wellmade it .All information on this site is represented for users. A site ismade professionally. So to holdhttp://web.zone.ee/aceba/hospiceh43/index.html hospice home care


Diwakar said...

Thanks I'm searching this from two days......
Thanks Ones again....


Anonymous said...

Hi,
While getting output it is not coming in text form.
It comes in a form like this
ࡱ > " $ !q`.
Any help would be appreciable.

Thanks


MeBerserk said...

This has bad written all over it!


Unknown said...

how to read ms word header in asp.net


Anonymous said...

hi my name is tausif meman.i would like to say thank u very much because of this help.if do u have any more tips for vb.net or asp.net so please send me "tosif_memon2000@yahoo.com".


Anonymous said...

hi
it is giving writer does not exist in this context error on line Writer.Write(strBuilder);

plzzz help.


Unknown said...

@Above :

There seems to be a typing error in my post, it should be

sWriter.Write(strBuilder);


wdwtest said...

hello these is not useful for reading doc file when i tried to read doc file it it shows garbage data


Anonymous said...

I dont usual comment, but that was an excellent post. cheers!


Anonymous said...

If its word doc file creation code then how do i generate the plain old text file.... ??????????????


Dylan said...

You can try .NET Library for Word from Aspose and check out their code samples for this purpose i got alot of help from their documentation section in creating my own application in .net.


Find More Articles