Openxml entry-add text to Word documents

Source: Internet
Author: User

Use openxml to add text to Word documents. Each module has its own attributes and content. To set a style, declare the attribute object and append the style to the attribute, append the attribute to the module, and the content in the module will have the style. This method appends content to the end of the file by default.

Code:

 

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using documentformat. openxml; using documentformat. openxml. packaging; using documentformat. openxml. wordprocessing; namespace addstringtoword {public class program {public static void main (string [] ARGs) {addstring ("test.docx", "");} public static void addstring (string filepath, string Str) {using (wordprocessingdocument Doc = wordprocessingdocument. open (filepath, true) {paragraph = new paragraph (); Run = new run (); runproperties = new runproperties (); // attribute runfonts fonts = new runfonts () {Eastasia = "dfkai-Sb"}; // set fontsize = new fontsize () {val = "52 "}; // set the font size color = new color () {val = "red"}; // set the font style // Add the style to runproperties in the property. append (color); runproperties. append (size); runproperties. append (fonts); run. append (runproperties); run. append (new text (STR); paragraph. append (run); Doc. maindocumentpart. document. body. append (paragraph); Doc. maindocumentpart. document. save ();}}}}

As follows:

Openxml entry-add text to Word documents

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.