C # Simple implementation of Dynamic Data generation Word document and save

Source: Internet
Author: User
Tags save file

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.

(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

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

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 usingMicrosoft.Office.Interop.Word;Ten  One namespaceBuild a Word document A { -      Public Partial classForm1:form -     { the          PublicForm1 () -         { - InitializeComponent (); -Load + =Form1_Load; +         } -  +         voidForm1_Load (Objectsender, EventArgs e) A         { at             //********************************************** -             //from blog http://blog.csdn.net/fujie724 -             //********************************************** -             Objectomissing =System.Reflection.Missing.Value; -             //Create a Word application instance -Microsoft.office.interop.word._application oword =NewMicrosoft.Office.Interop.Word.Application (); in             //set to not visible -oWord.Visible =false; to             //template file address, here is assumed in the X packing directory +             ObjectOtemplate ="D://template.dotx"; -             //generate a document on a template basis theMicrosoft.office.interop.word._document odoc = OWORD.DOCUMENTS.ADD (refOtemplate,refomissing,refomissing,refomissing); *             //declaring an array of bookmarks $             Object[] oBookMark =New Object[5];Panax Notoginseng             //Assignment Book Signature -obookmark[0] ="Beizhu"; theobookmark[1] ="name"; +obookmark[2] ="Sex"; Aobookmark[3] ="Birthday"; theobookmark[4] ="Hometown"; +             //assigning arbitrary data to the location of a bookmark -ODoc.Bookmarks.get_Item (refobookmark[0]). Range.Text ="using templates to implement word generation"; $ODoc.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             //The Save File dialog box appears, saving the generated word -SaveFileDialog SFD =NewSaveFileDialog ();WuyiSfD. Filter ="Word Document (*.doc) |*.doc"; theSfD. DEFAULTEXT ="Word Document (*.doc) |*.doc"; -             if(SFD. ShowDialog () = =DialogResult.OK) Wu             { -                 Objectfilename =SFD. FileName; About  $Odoc.saveas (refFileNamerefomissing,refomissing,refomissing, -                 refomissing,refomissing,refomissing,refomissing,refomissing, -                 refomissing,refomissing,refomissing,refomissing,refomissing, -                 refomissing,refomissing); AoDoc.Close (refomissing,refomissing,refomissing); +                 //Close Word theOword.quit (refomissing,refomissing,refomissing); -             } $         } the     } 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.

C # Simple implementation of Dynamic Data generation Word document and save

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.