Reprinted from: http://blog.csdn.net/fujie724/article/details/5443322
Fit for template Writing
Today, someone asked me how to generate a report-style Word document.
Is that the style and position of the text is relatively fixed, but the contents of the content are read from the data.
I think that similar to the general use of third-party reports to do relatively simple. But since I asked for word, I had to bite the bullet.
Most of the methods on the Web are getting data from a GridView or table and adding a table to Word.
However, it is not convenient to achieve the following effect (Figure 1). (The following illustration may require CSDN user login to see)
(Fig. 1)
We do this by using the Word template, as follows:
1. First, you need to add a reference to the Word class library to the reference in the project (Figure 2). I am Office 2003. Other versions may be slightly different. Inside the COM.
(Fig. 2)
2. Use Word to design a template document (suffix name *.dot). (Fig. 3)
(Fig. 3)
3. Add bookmarks to the template where the dynamic content needs to be displayed. The specific method is. Cursor falls where you want to insert the content, select "Insert"--〉 "Bookmark" on the menu bar (Figure 4)
(Figure 4) Add a bookmark after "remarks:", called "Beizhu". Bookmark names cannot begin with a number.
4, complete the addition of all bookmarks, in turn, should be:
Position |
Book Signature |
Notes to the right |
Beizhu |
Cell to the right of the name |
Name |
Gender right Cell |
Sex |
Right cell for Birthday |
Birthday |
Birthplace Right Cell |
Hometown |
5. Save this completed template to any path, such as X:/template.dot
6. Add a reference namespace to the class of the project's form
1 using Word;
7, in order to save trouble, directly in the form of the Load event to add the following code.
1 //**********************************************2 //from blog http://blog.csdn.net/fujie7243 //**********************************************4 Objectomissing =System.Reflection.Missing.Value;5 //Create a Word application instance6Word._application oword=NewWord.Application ();7 //set to not visible8oWord.Visible =false; 9 //template file address, here is assumed in the X packing directoryTen ObjectOtemplate ="X://template.dot"; One //generate a document on a template basis AWord._document odoc = OWORD.DOCUMENTS.ADD (refOtemplate,refomissing,refomissing,refomissing); - //declaring an array of bookmarks - Object[] oBookMark =New Object[5]; the //Assignment Book Signature -obookmark[0] ="Beizhu"; -obookmark[1] ="name"; -obookmark[2] ="Sex"; +obookmark[3] ="Birthday"; -obookmark[4] ="Hometown"; + //assigning arbitrary data to the location of a bookmark AODoc.Bookmarks.get_Item (refobookmark[0]). Range.Text ="using templates to implement word generation"; atODoc.Bookmarks.get_Item (refobookmark[1]). Range.Text ="John Doe"; -ODoc.Bookmarks.get_Item (refobookmark[2]). Range.Text ="female"; -ODoc.Bookmarks.get_Item (refobookmark[3]). Range.Text ="1987.06.07"; -ODoc.Bookmarks.get_Item (refobookmark[4]). Range.Text ="Hezhou"; - //The Save File dialog box appears, saving the generated word -SaveFileDialog SFD =NewSaveFileDialog (); inSfD. Filter ="Word Document (*.doc) |*.doc"; -SfD. DEFAULTEXT ="Word Document (*.doc) |*.doc"; to if(SFD. ShowDialog () = =DialogResult.OK) + { - Objectfilename =SFD. FileName; the *Odoc.saveas (refFileNamerefomissing,refomissing,refomissing, $ refomissing,refomissing,refomissing,refomissing,refomissing,Panax Notoginseng refomissing,refomissing,refomissing,refomissing,refomissing, - refomissing,refomissing); theoDoc.Close (refomissing,refomissing,refomissing); + //Close Word AOword.quit (refomissing,refomissing,refomissing); the}
8. After running, pop up the Save File dialog box (because it is written in the Load event). Save As Doc document, open the found effect as follows (Figure 5)
(Fig. 5)
At this point, the content in the document is what we have set. A simple and fast, fixed-format Word document output is complete.
Hope to be helpful to the friends who need it.
The above complete tutorial for individual labor results, reproduced please indicate the source. Thank you.
Manipulating Word documents in C #-one, template-style writing