Asp.net generates word

Source: Internet
Author: User
Microsoft. Office. InterOP. Word. Application APP = new Microsoft. Office. InterOP. Word. Application (); // template file string templatefile = server. mappath ("~ /Wordtemplate/"+ templater); // templatefile = server. mappath ("~ /Upload/testword/testprint.doc "); // The generated new file string filename = server. mappath ("~ /Wordtemplate/document/"+ datetime. now. tostring ("yyyymmddhhmmssfffff") + ". doc "); // @" C: \ Documents ents and Settings \ Administrator \ Desktop \ "+ datetime. now. tostring ("yyyymmddhhmmssfffff") + ". doc "; string fname = datetime. now. tostring ("yyyymmddhhmmssfffff") + ". doc "; filename = server. mappath ("~ /Wordtemplate/document/"+ fname); // copy the template file to the new file. copy (templatefile, filename); Microsoft. office. interOP. word. document Doc = new Microsoft. office. interOP. word. document (); object obj_filename = filename; object visible = false; object missing = system. reflection. missing. value; // open the file Doc = app. documents. open (ref obj_filename, ref missing, ref readonly, ref missing, ref missing, ref missin G, ref missing, ref visible, ref missing, ref missing); Doc. activate (); # region generates wrod if (type = "1") {foreach (Microsoft. office. interOP. word. bookmark BM in Doc. bookmarks) {If (BM. name = "banner") {BM. select (); BM. range. TEXT = This. gbanner. innerhtml;} else if (BM. name = "content") {BM. select (); BM. range. TEXT = T His. gcontent. innerhtml;} else if (BM. name = "Foote") {BM. select (); BM. range. TEXT = This. lblgdate. innerhtml;} else if (BM. name = "right") {BM. select (); BM. range. TEXT = "case: MS-0" + this. glblnum. innerhtml + "letter: 0" + this. glblhon. innerhtml;} else if (BM. name = "tal") {BM. select (); BM. range. TEXT = "contact number of the Contractor:" + this. lbllawyertal. innerhtml. trim () ;}} else {foreach (Microsoft. office. interOP. Word. bookmark BM in Doc. bookmarks) {If (BM. name = "banner") {BM. select (); BM. range. TEXT = This. sbanner. innerhtml;} else if (BM. name = "content") {BM. select (); BM. range. TEXT = This. scontent. innerhtml;} else if (BM. name = "Foote") {BM. select (); BM. range. TEXT = This. lblsdate. innerhtml;} else if (BM. name = "right") {BM. select (); BM. range. TEXT = "case: MS-0" + this. lblsnum. innerhtml + "letter: 0" + This. lblhonnum. innerhtml ;}}// Doc. printout (ref missing, ref missing, // ref missing, // ref missing, ref missing, ref missing, ref missing, ref missing, // ref missing, ref missing); object issave = true; Doc. close (ref issave, ref missing, ref missing); // app. quit (ref issave, ref missing, Ref missing); try {fileinfo downloadfile = new fileinfo (hostingenvironment. applicationphysicalpath + "\ wordtemplate \ document \" + fname); response. clear (); response. clearheaders (); response. buffer = false; response. contenttype = "application/octet-stream"; response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (fname, system. text. encoding. utf8); response. AP Pendheader ("Content-Length", downloadfile. length. tostring (); response. writefile (downloadfile. fullname); response. flush (); response. end () ;} catch (exception ex) {Throw ex ;}// response. write ("<script language = 'javascript '> alert ('word generation successful! '); </SCRIPT> "); # endregion
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.