In this article we will learn how to create word through asp.net and write the data into that word and display in a grid simultaneously.
We can create the of word and write the content into it by asp.net.
1. Add Reference of “ Microsoft.Office.Interop.Word;” into your project
2. Add a button with name Button1
3. Add a div with divTxt id and runat=server and write code behind the following code;
MicrosoftWord.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MicrosoftWord.aspx.cs" Inherits="WebApplication1.MicrosoftWord" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />
<div id="divTxt" runat="server">
</div>
</form>
</body>
</html>
MicrosoftWord.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
public partial class MicrosoftWord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
object nullobj = System.Reflection.Missing.Value;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "<b> Creating , writing and reading a word document </b> <br/>";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
//Insert a paragraph at the end of the document.
Word.Paragraph oPara2;
object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara2.Range.Text = "<font color='red'> all this are from word doc";
oPara2.Format.SpaceAfter = 6;
oPara2.Range.InsertParagraphAfter();
//Insert another paragraph.
Word.Paragraph oPara3;
oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara3.Range.Text = "<br>Hi Friends. <br /> How are you? This File Path is D:";
oPara3.Range.Font.Bold = 0;
oPara3.Format.SpaceAfter = 24;
oPara3.Range.InsertParagraphAfter();
object fileName1 = @"d:\a.doc";
oDoc.SaveAs(ref fileName1, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
oDoc.Close(ref nullobj, ref nullobj, ref nullobj);
oWord.Quit(ref nullobj, ref nullobj, ref nullobj);
//wordwin.Attributes["src"] = fileName1.ToString();
readFileContent(fileName1.ToString());
}
public void readFileContent(string path)
{
ApplicationClass wordApp = new ApplicationClass();
object file = path;
object nullobj = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
ref file, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
string sFileText = doc.Content.Text;
doc.Close(ref nullobj, ref nullobj, ref nullobj);
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
divTxt.InnerHtml = sFileText;
//Response.Write(sFileText);
}
}