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 the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

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.