Read a Word File in ASP.Net C#

This article shows how to read a Word file.
 
The following is my Word file, the contents of which I am showing in a text box.
 
Read Word file in ASP.Net 
 
Image 1.
 
For this, I am using a Microsoft.Office.Interop.Word reference.
 
Add reference 
 
Image 2.
 
Now my aspx code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Read Word File</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div style="height: 700px;">  
  11.         <table cellpadding="10" cellspacing="10" width="85%" align="center" style="background: SkyBlue;">  
  12.             <tr>  
  13.                 <td>  
  14.                     Select Word File To Read #  
  15.                     <asp:FileUpload ID="WordFileToRead" runat="server" Width="500px" />  
  16.                     <asp:Button ID="btnUpload" runat="server" Text="Read File" OnClick="btnUpload_Click" />  
  17.                 </td>  
  18.             </tr>  
  19.             <tr>  
  20.                 <td>  
  21.                     <asp:TextBox ID="WordFileText" runat="server" Height="400px" Width="100%" TextMode="MultiLine"></asp:TextBox>  
  22.                 </td>  
  23.             </tr>  
  24.         </table>  
  25.     </div>  
  26.     </form>  
  27. </body>  
  28. </html> 
My aspx.cs code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8. using Microsoft.Office;  
  9.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.   
  15.     }  
  16.   
  17.     protected void btnUpload_Click(object sender, EventArgs e)  
  18.     {  
  19.   
  20.         WordFileToRead.SaveAs(Server.MapPath(WordFileToRead.FileName));  
  21.         object filename = Server.MapPath(WordFileToRead.FileName);  
  22.         Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();  
  23.         Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();  
  24.         object readOnly = false;  
  25.         object isVisible = true;  
  26.         object missing = System.Reflection.Missing.Value;  
  27.         try  
  28.         {  
  29.             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, ref isVisible, ref missing, ref missing, ref missing);  
  30.             WordFileText.Text = doc.Content.Text;  
  31.         }  
  32.         catch (Exception ex)  
  33.         {  
  34.   
  35.         }  
  36.   
  37.     }  

Now run the application and select your file to read:
 
Read file in ASP.Net 
 
Image 3.


Similar Articles