C # generate and save Word documents with Dynamic Data,

Source: Internet
Author: User

C # generate and save Word documents with Dynamic Data,

Someone asked me how to generate a report-type Word document.

That is, the style and position of the text are relatively fixed, but the content is read from the data.

I think it is easier to use third-party reports. However, since the Word is required, you have to stick your head to it.

Most methods on the Internet are to obtain data from a GridView or table and then add a table to Word.

(Figure 1)

We use the Word template as follows:

1. First, add the Word class library reference to the "Reference" in the project (figure 2 ). I am in Office 2003. Other versions may be slightly different. In COM

Figure 2)

2. Use Word to design a template document (suffix *. dot ). (Figure 3)

Figure 3)

3. Add bookmarks to the places in the template where dynamic content needs to be displayed. The specific method is. Place the cursor to the position where you want to insert the content. Select "insert"> "bookmarks" on the menu bar (figure 4)

Figure 4) Add a bookmarks named "beizhu" after "Remarks ". The bookmark name cannot start with a number.

4. Add all the bookmarks. The values should be:

 

Location Bookmarks
Remarks right Beizhu
Cell right of name Name
Right cell of gender Sex
Cell right of birthday Birthday
Right cell of the nationality Hometown

5. Save the completed template to any path, for example, X:/template. dot.

6. Add the referenced namespace to the class of the project form

7. To save trouble, add the following code directly to the Load event of the form.

 

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 using Microsoft. office. interop. word; 10 11 namespace generate Word Documents 12 {13 public partial class Form1: Form14 {15 public Form1 () 16 {17 InitializeComponent (); 18 Load + = Form1_Load; 19} 20 21 void Form1_Load (object sender, EventArgs e) 22 {23 //*********************************** * 24/from blog http://blog.csdn.net/fujie72425 //********************* * ************************ 26 object oMissing = System. reflection. missing. value; 27 // create a Word application instance 28 Microsoft. office. interop. word. _ Application oWord = new Microsoft. office. interop. word. application (); 29 // set to invisible 30 oWord. visible = false; 31 // template file address, which is assumed to be in the X root directory 32 object oTemplate = "d: // template. dotx "; 33 // template-based document generation 34 Microsoft. office. interop. word. _ Document oDoc = oWord. documents. add (ref oTemplate, ref oMissing); 35 // declare the bookmark array 36 object [] oBookMark = new object [5]; 37 // value the bookmarkname 38 oBookMark [0] = "beizhu"; 39 oBookMark [1] = "name"; 40 oBookMark [2] = "sex "; 41 oBookMark [3] = "birthday"; 42 oBookMark [4] = "hometown"; 43 // assign any value to the position of the bookmark 44 oDoc. bookmarks. get_Item (ref oBookMark [0]). range. text = "use the template to generate Word"; 45 oDoc. bookmarks. get_Item (ref oBookMark [1]). range. text = "Li Si"; 46 oDoc. bookmarks. get_Item (ref oBookMark [2]). range. text = "female"; 47 oDoc. bookmarks. get_Item (ref oBookMark [3]). range. text = "1987.06.07"; 48 oDoc. bookmarks. get_Item (ref oBookMark [4]). range. text = "Hezhou"; 49 // The save file dialog box is displayed. Save the generated Word50 SaveFileDialog sfd = new SaveFileDialog (); 51 sfd. filter = "Word Document (*. doc) | *. doc "; 52 sfd. defaultExt = "Word Document (*. doc) | *. doc "; 53 if (sfd. showDialog () = DialogResult. OK) 54 {55 object filename = sfd. fileName; 56 57 oDoc. saveAs (ref filename, ref oMissing, 58 ref oMissing, 59 ref oMissing, ref oMissing, ref oMissing, 60 ref oMissing, ref oMissing); 61 oDoc. close (ref oMissing, ref oMissing, ref oMissing); 62 // Close word63 oWord. quit (ref oMissing, ref oMissing, ref oMissing); 64} 65} 66} 67}

 

8. The save file dialog box is displayed after running (because it is written in the Load event ). Save it as a Doc file. The following figure shows the effect of opening the file (figure 5)

(Figure 5)

At this point, we have achieved that the content in this document is exactly what we have set. A simple and quick fixed-format word document is output.

 

I hope to help my friends in need.

 

The complete tutorial above is the result of individual labor. For more information, see the source. Thank you.

 

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.