Categories
Asp, Asp.net

How to Read Word File Content in Asp.Net

How to Read Word File Content in Asp.Net

In this post we will learn How to Read Word File Content in Asp.Net. For this we have to Add COM reference of Microsoft word.

Design:

01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
02 
03<!DOCTYPE html>
04 
06<head runat="server">
07<title>How to Read Word File Content in Asp.Net</title>
08<style type="text/css">
09body
10 {
11 width: 980px;
12 margin: 0px auto;
13 text-align: center;
14 padding-top: 50px;
15 font-size: 20px;
16 }
17</style>
18</head>
19<body>
20<form id="form1" runat="server">
21<div>
22<h2>How to Read Word File Content in Asp.Net</h2>
23<asp:FileUpload ID="wordfileupload" runat="server" />
24<br /><br />
25<asp:Button ID="submit" Text="Upload File" runat="server" OnClick="submit_Click" /> 
26<br /><br />
27<asp:TextBox ID="wordcontent" runat="server" TextMode="MultiLine" Height="250px" Width="100%"></asp:TextBox>
28 
29<br /><br />
30All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a> | Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>
31</div>
32</form>
33</body>
34</html>

Code:

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Web;
05using System.Web.UI;
06using System.Web.UI.WebControls;
07using System.IO;
08 
09public partial class _Default : System.Web.UI.Page
10{
11    protected void Page_Load(object sender, EventArgs e)
12    {
13 
14    }
15    protected Boolean CheckFileType(string FileName)
16    {
17        string Ext = System.IO.Path.GetExtension(FileName);
18 
19        switch (Ext.ToLower())
20        {
21            case (".doc"):
22                { return true; }
23            case (".docx"):
24                { return true; }
25            default:
26                { return false; }
27        }
28    }
29    protected void submit_Click(object sender, EventArgs e)
30    {
31        Boolean Check = CheckFileType(wordfileupload.FileName);
32        if (Check == false)
33        {
34            ScriptManager.RegisterStartupScript(this, this.GetType(), "StartUpScript2", "alert('Invalid File Extension')", true);
35        }
36        else
37        {
38            wordfileupload.SaveAs(Server.MapPath(wordfileupload.FileName));
39            object filename = Server.MapPath(wordfileupload.FileName);
40            Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();
41            Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
42            object readOnly = false;
43            object isVisible = true;
44            object missing = System.Reflection.Missing.Value;
45            try
46            {
47                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);
48                wordcontent.Text = doc.Content.Text;
49            }
50            catch (Exception ex)
51            {
52 
53            }
54        }
55    }
56}