Wednesday, June 17, 2009

Read Write Edit Word Document With FileStream StreamWriter ASP.NET


In this article i am going to describe how to read and write to ms word file or document in ASP.NET using FileStream, StreamReader and StreamWriter.

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.



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

www.tips-fb.com
Shout it
Stumble Upon Toolbar
Submit this story to DotNetKicks vote it on WebDevVote.com add to del.icio.us saved by 0 users
Subscribe to Feeds

9 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 post has been removed by a blog administrator.


Anonymous said...

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

http://aspnetcsharp4.blogspot.com/


amiT
amiT jaiN said...

@Anonymous:

Thanks and welcome to my site :)


amiT
amiT jaiN 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 post 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


About Me

My Photo
amiT jaiN
Hi, I am amiT jaiN Software engineer working on C#.NET and ASP.NET technologies
View my complete profile

Comments

.NET Resources

Find More Articles


Subscribe To Feeds

Subscribe by E-mail

Enter your email address:

Delivered by FeedBurner


Subscribe in your favorite reader

This site is best viewed with || You may get errors in proper display of this site if using Internet explorer


C#.NET Articles and tutorials,ASP.NET Articles - blog by amiT jaiN