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:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to Read Word File Content in Asp.Net</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>How to Read Word File Content in Asp.Net</h2> 
<asp:FileUpload ID="wordfileupload" runat="server" />
<br /><br />
<asp:Button ID="submit" Text="Upload File" runat="server" OnClick="submit_Click" />  
<br /><br />
<asp:TextBox ID="wordcontent" runat="server" TextMode="MultiLine" Height="250px" Width="100%"></asp:TextBox>

<br /><br />
All 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>
</div>
</form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected Boolean CheckFileType(string FileName)
    {
        string Ext = System.IO.Path.GetExtension(FileName);

        switch (Ext.ToLower())
        {
            case (".doc"):
                { return true; }
            case (".docx"):
                { return true; }
            default:
                { return false; }
        }
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        Boolean Check = CheckFileType(wordfileupload.FileName);
        if (Check == false)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "StartUpScript2", "alert('Invalid File Extension')", true);
        }
        else
        {
            wordfileupload.SaveAs(Server.MapPath(wordfileupload.FileName));
            object filename = Server.MapPath(wordfileupload.FileName);
            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);
                wordcontent.Text = doc.Content.Text;
            }
            catch (Exception ex)
            {

            }
        }
    }
}