Insert text, images, and tables into the Word template using win form

Source: Internet
Author: User

 

Source Address: http://apps.hi.baidu.com/share/detail/17026118

1. Create a Word template file "person. Dot" to mark the filling position of related fields with bookmarks.

2. Create a web application and add it to Microsoft. Office. InterOP. Word.

Reference specific add reference see http://www.microsoft.com/china/msdn/library/office/office/OfficePrIntopAssFAQ.mspx? MFR = true3. sample code

 

Protected void createreport_click (Object sender, eventargs e) {Microsoft. office. interOP. word. application appword = NULL; // application Microsoft. office. interOP. word. documentclass Doc = NULL; // document try {appword = new Microsoft. office. interOP. word. application (); appword. visible = false; object objtrue = true; object objfalse = false; object objtemplate = server. mappath ("person. dot "); // template path object objdoctype = Wddocumenttype. wdtypedocument; Doc = (documentclass) appword. documents. add (ref objtemplate, ref objfalse, ref objdoctype, ref objtrue); // generate a Word document in the first step. // define the bookmarks variable object obdd_name = "bm_name "; // name object obdd_sex = "bm_sex"; // sex object obdd_birthday = "bm_birthday"; // Date of Birth object obpic = "pic"; object obtable = "obtable "; object nothing = system. reflection. missing. value; // inlineshape shape = appword. Selection. inlineshapes. addpicture (@ "F: \ picture \ _ dsc1602.jpg", ref nothing); // read data in step 2 and fill in the dataset system. data. datatable dt = new datatable (); DT. columns. add ("p_name"); DT. columns. add ("p_sex"); DT. columns. add ("p_birthday"); datarow DR = DT. newrow (); Dr ["p_name"] = "Zhang San"; Dr ["p_sex"] = "male"; Dr ["p_birthday"] = "1980-01-01"; DT. rows. add (DR); // assign a value to the bookmarks in step 3 // assign a value to the bookmarks in Doc. bookmarks. get_it EM (ref obdd_name ). range. TEXT = DT. rows [0] ["p_name"]. tostring (); // Doc. bookmarks. get_item (ref obdd_sex ). range. TEXT = DT. rows [0] ["p_sex"]. tostring (); // sex Doc. bookmarks. get_item (ref obdd_birthday ). range. TEXT = DT. rows [0] ["p_birthday"]. tostring (); // age Doc. bookmarks. get_item (ref obpic ). range. inlineshapes. addpicture (@ "F: \ picture \ _ dsc1602.jpg", ref nothing); // insert a table in the document // Doc. Bookmarks. get_item (ref obtable ). range. tables. add (Doc. bookmarks. get_item (ref obtable ). range, 12, 3, ref nothing, ref nothing); Microsoft. office. interOP. word. table newtable = Doc. tables. add (Doc. bookmarks. get_item (ref obtable ). range, 12, 3, ref nothing, ref nothing); newtable. borders. outsidelinestyle = wdlinestyle. wdlinestylesingle; newtable. borders. insidelinestyle = wdlinestyle. wdlinestylesingle ;/ /Add content Doc. Paragraphs. Last. range. Text = "" to the last line of the document; // generate the word object filename = server. mappath ("~ ") +" \ BG \ "+ dt. rows [0] ["p_name"]. tostring () + ". doc "; object Miss = system. reflection. missing. value; Doc. saveas (ref filename, ref miss, ref miss, ref miss, ref Miss); object missingvalue = type. missing; object donotsavechanges = wdsaveoptions. wddonotsavechanges; Doc. close (ref donotsavechanges, ref missingvalue, ref missingvalue); appword. application. quit (ref miss, ref miss, ref Miss); Doc = NULL; appword = NULL;} catch (system. exception ex) {// capture exception. If an exception occurs, clear the instance, exit word, and release the resource string AA = ex. tostring (); object Miss = system. reflection. missing. value; object missingvalue = type. missing; object donotsavechanges = wdsaveoptions. wddonotsavechanges; Doc. close (ref donotsavechanges, ref missingvalue, ref missingvalue); appword. application. quit (ref miss, ref miss, ref Miss); Doc = NULL; appword = NULL ;}}

 

-----
If the above Code encounters a 80070005 error during running

Solution 1:
Control Panel-> Administrative Tools-> component services-> Computer-> my computer-> DCOM configuration-> Microsoft Word documents
After
Click properties to open the Properties dialog box for this application.
2. Click the ID tab and select an interactive user.
3. Click the "Security" tab, select "Custom" in the "Start and activate Permissions" and "Access Permissions" groups, and then
Custom-> edit-> Add ASP. NET account and iuser _ computer name
4. Make sure that each user is allowed to access the service and click OK.
5. Click OK to disable dcomcnfg.

Solution 2:
If the above method cannot solve the problem, it should be a permission problem. Please try the following method:
Use identity simulation in Web. config and add <identity impersonate = "true" username = "your username to the <system. Web> section.

"Password =" password "/>
</System. Web>

Appendix: Detailed operations on Images

Object filename = @ "C: \ Inetpub \ wwwroot \ testwebapp \ test.doc"; // file name word. application A = new word. applicationclass (); // create a word program to object nothing = system. reflection. missing. value; // null value word. document B =. documents. open (ref filename, ref nothing, ref nothing, ref nothing, ref nothing); // create a Word document object. // In fact, this step executes the macro inlineshape shape =. selection. inlineshapes. addpicture (@ "C: \ Documents ents and Settings \ Administrator \ Desktop \ 2003121512223104481.jpg", ref nothing); shape. height = inchestopoints (0.5) shape. width = inchestopoints (0.5) // selection. inlineshapes. addpicture filename: = "C: \ Documents ents and Settings \ Administrator \ Desktop \ 2003121512223104481.bmp", linktofile: = false, savewithdocument: = true end sub B. save (); // save B. close (ref nothing, ref nothing, ref nothing); // close Word document. quit (ref nothing, ref nothing, ref nothing); // exit the word Program

 

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.