Export word using docx in SharePoint

Source: Internet
Author: User
ArticleDirectory
    • Docx Main Functions
    • Create document attributes
    • Docx logic implementation
    • View the generated approval attachment
    • Summary

Generally, you need to deal with word more or less during development. The special change is to edit and export the word.

Using docx, an open-source read/write word component, you can quickly help us perform word operations.

Docx Official Website: http://docx.codeplex.com/

Docx Main Functions
    • Insert, delete, or replace text in a document (Word). All standard text formats, such as font {family, size, color}, output, italic, underline, and highlight, are supported.
    • Provides paragraph attributes. You can set its direction, such as left-to-right and center-to-right.
    • Docx also supports image operations, hyperlinks, tables, headers, headers, and so on.
    • Most importantly, docx supports custom document properties)

Recently, you want to modify a Sharepoint project. You can export the word to print the submitted documents after approval, for example. This is an important function for the OA system. The customer is in a hurry and needs to develop it quickly.

Create document attributes

Docx supports loading a pre-defined template. By modifying the template, you can create a new docx type object and directly call docx. the saveas method can be saved to the specified path, and you can also save it to a memory stream.For specific powerful functions, refer to docx to provide example, which is very detailed.

Let's get started. Word2010 (07 uncertain, never used) and later versions support document properties. Document properties are a very important feature for developers. You can extend the attributes of Word documents, take word 2013 as an example. Open word 2013, click the file in the upper-left corner, and on the new pop-up page, select Properties> Advanced properties, as shown below:

Next, create a new document property. Make sure that you have a proper name. do not duplicate the name with an existing property. For complaint approval, you can create the following document properties:

 

 

After the creation is complete, insert document properties: switch to the insert tab, find the document parts, click the document parts to select a domain, and then select docproperty in the new window that appears, find the property to be inserted on the right and insert it to the corresponding location:

This allows you to quickly create a standard template, such. The next step is to fill in the following content in the traps:

Docx logic implementation
    • Create the complaintmodel attribute

First, we agree that the attribute value cannot contain a line break (\ r \ n). Otherwise, inserting the text of the attribute value containing the linefeed will be different from the expectation. How can we avoid this situation? It is easy to assume that the attribute value of an attribute contains a line break. Instead of adding it as an attribute to the document attribute, we directly replace []. With this Convention, we then create the attributes of our object (complainmodel) (note that the attributes of complainmodel objects must be the same as those of the previous document. The reason will be explained later)

 Public  Class  Complainmodel {  //  Title              Public   String Ctitle { Get ; Set  ;}  //  Complainant              Public   String Complainer { Get ; Set ;}  //  Contact number              Public   String Mobile { Get ; Set  ;}  //  Category              Public   String Sort { Get ; Set  ;}  // Complaint content              Public   String Complain { Get ; Set  ;}  //  Sub-District              Public   String Stretch { Get ; Set  ;}  //  Remarks              Public  String Remark { Get ; Set  ;}  //  Level 1 approver              Public   String Firstapprover { Get ; Set  ;}  //  Level 1 approval content              Public   String Firstapprovetext {Get ; Set  ;}  //  Level 2 approver              Public   String Secondapprover { Get ; Set  ;}  //  Level 2 Approval content              Public   String Secondapprovetext { Get ;Set  ;}  //  Level 3 approver              Public   String Thirdapprover { Get ; Set  ;}  //  Level 3 approval content              Public   String Thirdapprovetext { Get ; Set ;}} 
    • Next is the core step. Load the pre-defined template (if an exception is thrown, add the everyone permission and remove the read-only permission ).
 
Docx gdocument = docx. Load (@"C: \ Users \ Administrator \ Desktop \ complaint approval form .docx");
    • Initialize complainmodel
 //  Create a customproperty object Complainmodel = New  Complainmodel (); complainmodel. ctitle = Newitem [ "  Title  " ]. Tostring (); complainmodel. complainer = Newitem [ "  Complainter  "  ]. Tostring (); complainmodel. Mobile = Newitem [ "  Tel  "  ]. Tostring (); complainmodel. Sort = Newitem [ "  Complainttype  " ]. Tostring (); complainmodel. complain = Newitem [ "  Complaintcontent  "  ]. Tostring (); complainmodel. Stretch = Newitem [ "  Roadselect  "  ]. Tostring (); complainmodel. remark = Newitem [ "  Remark  "  ]. Tostring (); //  Approval comments Complainmodel. firstapprover = newitem [ "  Firstapprover  "  ]. Tostring (); complainmodel. firstapprovetext = Newitem [ "  Firstapprovertext  "  ]. Tostring (); complainmodel. secondapprover = Newitem [ "  Secondapprover  " ]. Tostring (); complainmodel. secondapprovetext = Newitem [ "  Secondapprovertext  "  ]. Tostring (); complainmodel. thirdapprover = Newitem [ "  Thirdapprover  "  ]. Tostring ();  //  So txtlevelthreesuggestion. Text Complainmodel. thirdapprovetext = txtlevelthreesuggestion. text;
    • Defines docxhelper, which provides a reflection mechanism to add document attributes to the new docx object created by the load template.
 Public   Static   Class  Docxhelper {  Public   Static   Void Addcustomproperty <t> ( This  Docx, t source) {type = Typeof  (T );  //  Reflection returns all non-static public attributes of the specified type. Propertyinfo [] props = type. getproperties (bindingflags. instance | Bindingflags. Public );  Foreach ( VaR Prop In  Props ){  //  Obtains the attribute value of the specified object.                  VaR Value = String . Format ( "  {0}  " , Prop. getvalue (source, Null ));  //  If \ r \ n is included, ignore it directly. Instead of adding it to the document attribute, replace it directly.                  If (! Value. Contains (environment. newline) {customproperty = New  Customproperty (prop. Name, value); docx. addcustomproperty (customproperty );  Continue  ;}  //  Replace [] with the specified property value Docx. replacetext (String . Format ( "  [{0}]  "  , Prop. Name), value );}} 

As mentioned above, document attributes and complainmodel object attributes are required.Same nameThe reason is docx. inside the addcustomproperty method (the docx component is open-source and you can check the implementation of addcustompropery), we perform the following steps: first, determine whether the document attribute exists. If yes, delete it (remove ), create a new document attribute (note that the name is the same; otherwiseError!Unknown document property name, You can obtain a wordManualDelete the document attribute, update the domain), and then update the domain, so that the property value is synchronized to the corresponding location of the document Attribute Insertion.

    • AllCodeAs follows:
// Create a complaint approval docx document and append it as an attachment to the docx gdocument in the attachment column; try {gdocument = docx. load (@ "C: \ Users \ Administrator \ Desktop \ Wulin Management Committee \ complaint approval form .docx"); // create the customproperty object complainmodel = new complainmodel (); complainmodel. ctitle = newitem ["title"]. tostring (); complainmodel. complainer = newitem ["complainter"]. tostring (); complainmodel. mobile = newitem ["tel"]. tostring (); complainmodel. sort = newitem ["com Plainttype "]. tostring (); complainmodel. complain = newitem ["complaintcontent"]. tostring (); complainmodel. stretch = newitem ["roadselect"]. tostring (); complainmodel. remark = newitem ["remark"]. tostring (); // approval comments complainmodel. firstapprover = newitem ["firstapprover"]. tostring (); complainmodel. firstapprovetext = newitem ["firstapprovertext"]. tostring (); complainmodel. secondapprover = newitem ["secondappro Ver "]. tostring (); complainmodel. secondapprovetext = newitem ["secondapprovertext"]. tostring (); complainmodel. thirdapprover = newitem ["thirdapprover"]. tostring (); // It has not been persisted to the database, so txtlevelthreesuggestion. text complainmodel. thirdapprovetext = txtlevelthreesuggestion. text; // Add the customproperty object gdocument to the docx document. addcustomproperty <complainmodel> (complainmodel); // Save the docx file retrieved from the template as a stream. var stream = new SY Stem. io. memorystream (); gdocument. saveas (Stream); // set the stream position to 0 stream. position = 0; // append the obtained stream to newitem in the Sharepoint list attachment column. attachments. add ("complaint approval form _export print .docx", stream. readfully (); // last update, execute the workflow, and execute the approval archive newitem. update ();} catch (exception ex) {// If the template document does not exist or you forgot to set it in list attachment, nothing will happen, that is, the entire approval process is not affected because no exported documents are generated.}
View the generated approval attachment

After the archive is approved, you can view the archive in the attachment column. Relevant leaders can download and print it.

    • Export and view word

Summary

Docx is a lightweight open-source component that allows you to conveniently operate word. For more powerful functions, see the docx codeplex official website example. More powerful functions are waiting for you to explore.

 

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.