ASP. NET generates word notes based on templates

Source: Internet
Author: User

Recently encountered a problem, the customer asked for a new demand, the customer wants to display the data displayed on the page into Word to set up a dozen, because have not touched the content of this piece, their own system has not used this function, now learn from the start.

Specific ideas:

1. First make Word template, use text box + bookmark way to design template;

2. After the template is finished, generate a new file according to the template, using the File.Copy method, generate a new file in. doc format;

3. The background obtains the data, the reference page renders the way to mark the data stream, facilitates the subsequent writing operation;

4. Edit the new file, according to the corresponding relationship between tags and bookmarks in the program, use the method in the class library to modify the contents of the bookmark, and finally save the file.

Advantages:

The ability to customize the position of an element is also straightforward, and it can be useful if it's just a simple format such as a table.

Disadvantages:

Flexibility is not good, if you want to hit the object is a Web page, and is not in tabular form, the degree of restoration is not good, and if the text content too much will appear outside the text box range of circumstances.

Operation Procedure & Code:

1) First step, introduce the corresponding assembly

If you are not using a third-party class library, you do not need to download it, as long as your system is fully installed with Office, your system assembly will have an assembly that operates on word.

First, add the reference Microsoft.Office.Interop.Word, and then add the corresponding reference in the class:

1 using MSWord = Microsoft.Office.Interop.Word;//rename here to MSWord, then use it to abbreviate 2 using system.io;//to manipulate the file, refer to the system IO3using system.reflection;

2) Preparatory work

1Msword.application WordApp =NewMsword.application ();//Word Application Variables2Msword.document WordDoc;//Word Document Variables3 stringTemplateFile =@"D:\Model.doc";//the path to the template file,4 stringFileName =@"D:\"+ DateTime.Now.ToString ("yyyymmddhhmmssffffff") +". doc";//the path to the new file

3) Copy template to create a new file

1 file.copy (templatefile, FileName); // first, we need to copy a file . 2 object obj_filename = FileName; // Create a new obj variable for operation

4) Set the operation type, there are many types of operation, here to open a new file, modify the contents of it

1 worddoc = WordApp.Documents.Open (ref obj_filename); // open a new file for action 2 3 worddoc.activate (); // set up files for now operation

5) Modify Word content based on bookmarks

1  //loop bookmarks, determine bookmark names, and assign values to them2   foreach(Msword.bookmark BMinchworddoc.bookmarks)3   {4       if(BM. Name = ="Billno")5           {6 BM. Select ();7Bm. Range.Text =" -";8          }9           if(BM. Name = ="dated")Ten              { One BM. Select (); ABm. Range.Text =DateTime.Now.ToString (); -              } -}

6) Close the file

1   WORDDOC.SAVEAS2 (Obj_filename); // Save File 2   Worddoc.close (); // Close Document Object 3   Wordapp.quit (); // Close Application Object

7) Kill process, in some cases, close Word file will not succeed, will remain many word processes

1System.diagnostics.process[] processes = System.Diagnostics.Process.GetProcessesByName ("Winword");2 foreach(System.Diagnostics.Process Pinchprocesses)3    {4        BOOLb = P.mainwindowtitle = ="";5        if(P.mainwindowtitle = ="")6             {7 P.kill ();8             }9}

ASP. NET generates word notes based on templates

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.