HOW TO READ .DOC OR .DOCX FILE IN C# THROUGH FILE HANDLING???

Posted by Avdesh under C# on 11/18/2011 | Points: 10 | Views : 6660 | Status : [Member] | Replies : 2
PLEASE TELL ME WITH THE BEST SUITABLE EXAMPLE


Note::ONLY IN WINDOWS APPLICATION I WANNA READ .DOC OR DOCX FILE




Responses

Posted by: perfectchourasia-9163 on: 11/18/2011 [Member] Starter | Points: 25

Up
0
Down
<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>

using System.IO;
using System.Text;

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);
}

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Avdesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neoone on: 9/26/2012 [Member] Starter | Points: 25

Up
0
Down
You can use the free word_reader library. Download here http://sourceforge.net/p/word-reader/wiki/Home/
Add a reference to the dll "Code7248.word_reader.dll"
Also add a using Statement as "using Code7248.word_reader;"
To extract text, use:
TextExtractor extractor = new TextExtractor(PathToWordDocument);
string text = extractor.ExtractText(); //The string 'text' is now loaded with the text from the Word Document


Avdesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response