Introduction
This article contains description
about how Ms Word Document can be read and displayed in the screen using
asp.net C#. reading Word document is very easy task , just have to follow some
steps to get your task done. You have to follow only three steps are give
bellow.
Steps
1.
Browse and read word document file from client
machine
2.
Save it on the server
3.
Read file from server and display on the screen
Browse and Read Word
Document from client machine
To browse I did
nothing, just I used the browse or file up loader control available in the
ASP.NET server side controls list, the
up loader controls in build asp.net controls helps to caching file information
on the page .
Save it on the server and Read file from server and display on the
screen
After browsing word document file
don't refresh your page by any of the control, because in the every refresh the
page loses his information and control information. Before the page's post back,
save the browsed word document in the server. To save and view the word
document I have take the help button click event and Microsoft.Office.Interop.Word dll.
FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
object
filename = Server.MapPath(FileUpload1.FileName);
The server control
FileUplod's saveas function helps to save file in the server and Server.MapPath function is used to
map the path of the server.
Microsoft.Office.Interop.Word.ApplicationClass AC = new
Microsoft.Office.Interop.Word.ApplicationClass();
Above code create a object of application
class which helps to open or create a word document. This class belongs
Microsoft.Office.Interop name space.
Microsoft.Office.Interop.Word.Document doc = new
Microsoft.Office.Interop.Word.Document();
Above code create the object of
document class which keeps word content
doc
= AC.Documents.Open(ref filename, ref missing, ref
readOnly, ref missing, ref
missing, ref missing, ref
missing, ref missing, ref
missing, ref missing, ref
missing, ref isVisible);
TextBox1.Text
= doc.Content.Text;
above code opens the document
that you have browsed set the content of the word document in to the textbox
text.
HTML Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1"
runat="server"
Height="722px"
Width="100%"
TextMode="MultiLine"></asp:TextBox>
</div>
<div>
<asp:FileUpload ID="FileUpload1"
runat="server"
BorderStyle="Double"
Width="549px"
/> <asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click"
/>
</div>
</form>
</body>
</html>
C# Code
protected void
Button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.ApplicationClass
AC = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document
doc = new Microsoft.Office.Interop.Word.Document();
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
try
{
doc = AC.Documents.Open(ref filename, ref missing, ref
readOnly, ref missing, ref
missing, ref missing, ref
missing, ref missing, ref
missing, ref missing, ref
missing, ref isVisible);
TextBox1.Text = doc.Content.Text;
}
catch (Exception
ex)
{
MessageBox.Show("ERROR: " + ex.Message);
}
finally
{
doc.Close(ref missing, ref missing, ref
missing);
}
}
Summery
Above discussion we know how to read word document taking
help of Microsoft.Office.Interop.Word