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

![]() |
||
|
|
![]() |
|
| add to del.icio.us saved by 0 users |











9 comments:
This is no different from reading a .txt file. What about writing to .doc file with formatting of?
Thanks Man ITs usefullll
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.
This post has been removed by a blog administrator.
Nice Article.
Some nice tutorials on this blog. I liked them.
http://aspnetcsharp4.blogspot.com/
@Anonymous:
Thanks and welcome to my site :)
@James & Mikael:
I've changed the code to write formatted text to word file.
Hope this is not plain text now ;)
This post has been removed by a blog administrator.
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
Post a Comment