C # generate WORD in batches using WORD Templates and tags (bookmark,

Source: Internet
Author: User

C # generate WORD in batches using WORD Templates and tags (bookmark,

Preface:

Because I am not familiar with the C # operation WORD, I will leave such a hydrology, so don't vomit... = _ = |

Microsoft. Office. Interop. Word (Version 2003 is also version 11) -- because some clients still use Office2003, a lower version should be introduced for the sake of Insurance

My requirement is very simple. From the DataTable, the data is cyclically retrieved to generate Word Documents in batches. The solution is to use the Word template. dot to insert bookmarks and save them as templates used by the program,

Use C # To open the word template, modify the value of bookmarks, and generate Word in batches.

Problems:

My idea is to open the template, and then modify the book content in a cycle, and generate a .doc every cycle. In fact, the problem is that when the second value is assigned to the bookmarks (such as bookMark. range. text = "2013-4-24") is found to be a + = operation, that is, the second round of the loop bookmarks value is "2013-4-242013-4-24 ", my understanding is that the template is not closed, so the bookmarks are actually inserted twice instead of being assigned a value ], one way to achieve this is to disable dot first and then enable it to re-assign a value. However, because it takes a long time, I do not hesitate to abandon this approach. On average, a WORD is generated almost one second, of course, Win764bit is less efficient. -- Later, I found that the generated software runs much more efficiently on 32bit xp than on 62bit WIN7, and the efficiency of running on a virtual machine is faster than that on 32bit host xp, it may be because the virtual machine is only installed with office2003 and framework2.0. Even so, the efficiency in xp is still very low. The problem is how to insert and generate a WORD cyclically without closing the template file.

Solution:

[Csharp]View plaincopy
  1. Object [] oBookMark = new object [8];
  2. OBookMark [0] = "Date"; // The value is omitted to record the bookmarkname.
  3. Word. Range [] rng = new Word. Range [8];
  4. String [] bookMarkName = new string [8];
  5. For (int I = 0; I <8; I ++)
  6. {
  7. Rng [I] = oDoc. Bookmarks. get_Item (ref oBookMark [I]). Range; // obtain the bookmarkobject and assign a value to the defined array for inserting the following loop body
  8. BookMarkName [I] = oDoc. Bookmarks. get_Item (ref oBookMark [I]). Name; // obtain the bookMarkName. the bookmark object is assigned to the defined rng and oBookMark.
  9. }
  10. // Loop body
  11. For (int I = dt. Rows. Count-1; I> = 0; I --)
  12. {
  13. Rng [0]. Text = dt. Rows [I] ["date"]. ToString (); // assign multiple bookmarks 0 ~ 8
  14. For (int j = 0; j <8; j ++)
  15. {
  16. ODoc. Bookmarks. Add (bookMarkName [j], rng [j]); // you can directly insert it here to replace the original Bookmarks, and the position remains unchanged.
  17. // That is to say, the Add operation does not insert bookmarks at will, but replaces the original bookmarks, so that you can re-assign values each time.
  18. }
  19. // Save oDoc and use the Save method.
  20. }


Obtain the bookmarked object and the bookmarked name and assign it to the defined oBook and rng, and then perform an insert operation instead of modifying the bookmarked value of the document.

 

 

This method is described in the article "how to use bookmarks to read or write data to Word documents [C #]". "Because the bookmarks are automatically deleted after the value is attached, it will be easy to modify later, you need to automatically generate another"

When the program is running in the background, it also hurts, and it makes the program more time-consuming), but multiple pages of a file are more appealing than those of multiple single pages. Let's take a look .. ===| |


In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

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.