Friday 19 July 2013

Display the Content of Word File in Browser


Display the Content of Word File in Browser


We do most of the our Document related activities using Microsoft Office.. Most of the time we need to export the details from these files and display it our browsers. Most of them are related to excel file like importing excel to database or importing the excel content to gridview. Today I will be showing how to display the content of Word File(*.doc) to be rendered in your browser using ASP.Net.

First of all you need to reference this Microsoft.Office.Interop.Word to your Web Application.

Then include the namespace:

using Microsoft.Office.Interop.Word;

Then on the Page_Load Event write the following code:[Here I am using a word file named demo.docx which is in the folder WordFile inside my Web Application's Root Folder.]




        Microsoft.Office.Interop.Word.Application objWordApp = new Microsoft.Office.Interop.Word.Application();

        //ACCESSING THE WORD FILE PATH

        object objWordFile = Server.MapPath("WordFile\\demo.docx");

        object objNull = System.Reflection.Missing.Value;

        Document WordDoc = objWordApp.Documents.Open(

        ref objWordFile, ref objNull, ref objNull,

        ref objNull, ref objNull, ref objNull,

        ref objNull, ref objNull, ref objNull,

        ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);

        WordDoc.ActiveWindow.Selection.WholeStory();

        WordDoc.ActiveWindow.Selection.Copy();

        string strWordText = WordDoc.Content.Text;

        WordDoc.Close(ref objNull, ref objNull, ref objNull);

        objWordApp.Quit(ref objNull, ref objNull, ref objNull);

        // Asssigning word file value to bind the div

        DisplayWord.InnerHtml = strWordText;

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top