1, create a new Word template by looking for a Word document that needs to be populated with a program, and saving it if you want to enter it with a bookmark (insert--> bookmark--> enter Id-->ok).
2, modify the security add everyone readable on the word template to prevent the file from being opened.
3, add "References" to the project to find the "Microsoft Word 10.0 Object Library" or "Microsoft Word 11.0 Object Library" point determination.
4, new class called WordOp.cs meaning is the class that operates word.
The contents are as follows:
Copy Code code as follows:
Using System;
Using System.Web.Security;
Using Microsoft.Office.Interop.Word;
Using System.IO;
<summary>
Word's summary description
</summary>
public class Wordop
{
Public Wordop ()
{
//
TODO: Add constructor logic here
//
}
Private ApplicationClass WordApp;
Private Document WordDoc;
private static bool isopened=false;//to determine if Word templates are occupied
public void SaveAs (String Strfilename,bool isreplace)
{
if (Isreplace && file.exists (strFileName))
{
File.delete (strFileName);
}
Object missing = Type.Missing;
Object fileName = strFileName;
Worddoc.saveas (ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
Ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
Define a Word.Application Object
public void Activewordapp ()
{
WordApp = new ApplicationClass ();
}
public void Quit ()
{
Object missing = System.Reflection.Missing.Value;
WordApp.Application.Quit (ref missing, ref missing, ref missing);
isopened = false;
}
New Word files based on templates
public void Opentempelte (string strtemppath)
{
Object Missing = Type.Missing;
Object Missing = System.Reflection.Missing.Value;
Activewordapp ();
Wordapp.visible = false;
Object otemplate = (object) Strtemppath;
Try
{
while (isopened)
{
System.Threading.Thread.Sleep (500);
}
WordDoc = WORDAPP.DOCUMENTS.ADD (ref otemplate, ref Missing, ref Missing, ref Missing);
Isopened = true;
Worddoc.activate ();
}
catch (Exception Ex)
{
Quit ();
isopened = false;
throw new Exception (ex.message);
}
}
public void Filllable (String labelid,string Content)
{
Open Word Template
Opentempelte (Tempname); Fill in the contents of the Labelid label, that is, the subject item of the letter
Object BKMC = Labelid;
if (WordApp.ActiveDocument.Bookmarks.Exists (labelid) = = True)
{
WordApp.ActiveDocument.Bookmarks.get_Item (ref BKMC). Select ();
}
WordApp.Selection.TypeText (Content);
SaveAs (Saveasfilename);
Quit ();
}
}
5, in the need to use the page background call can be, for example as follows:
Copy Code code as follows:
String path = Server.MapPath ("Download");
String TemplatePath = path + "file://downloadczql.doc/";
Wordop wop = new Wordop ();
Wop. Opentempelte (TemplatePath);
Wop. Filllable ("id", "1");
Wop. Filllable ("Usr_name", "Test");
Wop. SaveAs (path + "File://savetest.doc", true/);
Wop. Quit ();
Response.Redirect (@ "/download/savetest.doc");//Make a jump for downloading.