asp.net How to import page content into a Word template-practical tips

Source: Internet
Author: User
Tags reflection
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.