Create a Word file

Source: Internet
Author: User
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using Microsoft. office. interOP. word; using system. io; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protected void btncreate_click (Object sender, eventargs e) {createwordfile ();} private void createwordfi Le () {string message = ""; try {object nothing = system. reflection. missing. value; directory. createdirectory ("C:/CNSI"); // The Outline of the Creation file string name = "CNSI _" + "53 ASDF" + ". doc "; object filename =" C: // CNSI // "+ name; // file storage path // create a Word document application wordapp = new applicationclass (); document worddoc = wordapp. documents. add (ref nothing, ref nothing); // Add a header wordapp. activewind Ow. view. type = wdviewtype. wdoutlineview; wordapp. activewindow. view. seekview = wdseekview. wdseekprimaryheader; wordapp. activewindow. activepane. selection. insertafter ("[header content]"); wordapp. selection. paragraphformat. alignment = wdparagraphalignment. wdalignparagraphright; // set the right-to-full wordapp. activewindow. view. seekview = wdseekview. wdseekmaindocument; // jump out of the header and set it to wordapp. selection. paragraphformat. linespacing = 15f; // Set the line spacing of the document. // move the focal plane and wrap the line Object COUNT = 14; object wdline = wdunits. wdline; // change the line; wordapp. selection. movedown (ref wdline, ref count, ref nothing); // move the focal area wordapp. selection. typeparagraph (); // remove the CIDR Block and create a Microsoft. office. interOP. word. table newtable = worddoc. tables. add (wordapp. selection. range, 12, 3, ref nothing, ref nothing); // sets the table style newtable. borders. outsidelinestyle = wdlinestyle. wdlinestylethickthi Nlargegap; newtable. borders. insidelinestyle = wdlinestyle. wdlinestylesingle; newtable. columns [1]. width = 100f; newtable. columns [2]. width = 220f; newtable. columns [3]. width = 105f; // fill in the table content newtable. cell (1, 1 ). range. TEXT = "Specific suspicious table of the product"; newtable. cell (1, 1 ). range. bold = 2; // set the font of the dual lattice to bold. // merge the newtable cells. cell (1, 1 ). merge (newtable. cell (1, 3); wordapp. selection. cells. verticalalignment = wdcellverticalalign Ment. wdcellalignverticalcenter; // vertical center wordapp. selection. paragraphformat. alignment = wdparagraphalignment. wdalignparagraphcenter. cell (2, 1 ). range. TEXT = "basic product suspect"; newtable. cell (2, 1 ). range. font. color = wdcolor. wdcolordarkblue; // set the font color of the dual element. // merge the newtable cells. cell (2, 1 ). merge (newtable. cell (2, 3); wordapp. selection. cells. verticalignment = wdcellverticalignment. wdcellalignve Rticalcenter; // enter newtable. cell (3, 1 ). range. TEXT = "brand title:"; newtable. cell (3, 2 ). range. TEXT = "BrandName"; // merge the dual lattice newtable with the vertical backend. cell (3, 3 ). select (); // select a row of object moveunit = wdunits. wdline; object movecount = 5; object moveextend = wdmovementtype. wdextend; wordapp. selection. movedown (ref moveunit, ref movecount, ref moveextend); wordapp. selection. cells. merge (); // pull out the image string filename = "/winter.jp G "; // path of the image: Object linktofile = false; object savewithdocument = true; object anchor = worddoc. application. selection. range; worddoc. application. activedocument. inlineshapes. addpicture (filename, ref linktofile, ref savewithdocument, ref anchor); worddoc. application. activedocument. inlineshapes [1]. width = 100f; // picture width worddoc. application. activedocument. inlineshapes [1]. height = 100f; // Image Height // set the image to the surrounding image type Shape S = worddoc. application. activedocument. inlineshapes [1]. converttoshape (); S. wrapformat. type = wdwraptype. wdwrapsquare; newtable. cell (12, 1 ). range. TEXT = "special product attributes"; newtable. cell (12, 1 ). merge (newtable. cell (12, 3); // Add a row of worddoc to the table. content. tables [1]. rows. add (ref nothing); worddoc. paragraphs. last. range. TEXT = "document creation time:" + datetime. now. tostring (); // "title" worddoc. paragraphs. last. alignment = wdpara Graphalignment. wdalignparagraphright; // The worddoc file is retained. saveas (ref filename, ref nothing, ref nothing, ref nothing, ref nothing); worddoc. close (ref nothing, ref nothing, ref nothing); wordapp. quit (ref nothing, ref nothing, ref nothing); message = Name + "documentation wins To keep it under C: CNSI ";} catch {message =" File Export exception! ";}Response. Write (Message );}}

After studying for several hours this afternoon, I finally got some name hall. I am very happy. Here I will record it for review:

Code download

References

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.