Use the XML schema definition structure to generate custom Word and Excel reports in the. NET Environment

Source: Internet
Author: User
Tags xsl xslt

Basic: Microsoft official explanation of XML and word


XML document in Word

Applicable to: Microsoft Office Word 2003

NoteIn addition to saving documents as XML with the word XML architecture, other XML functions can only be used in Microsoft Office Professional Edition 2003 and an independent Microsoft Office Word 2003.

Why use XML?

Extensible Markup Language (XML): a concentrated form of standard Markup Language (SGML) that developers can use to create custom tags, it provides flexibility for organizing and providing information .) This allows you to organize and process documents and data in a way that was previously impossible or hard to implement. Using a custom XML architecture, you can identify and extract specific commercial data fragments from common business documents.

For example, a check containing the customer's name and address, or a report containing the financial results of the previous quarter, is no longer a static document. The information they contain can be transferred to a database or reused outside the document.

The ability to save Microsoft Word documents in standard XML format helps to separate content from the document category. The content can be used to automate the data collection and use change processes. The content can be easily searched and even modified by other processes, such as server-based data processing.

Because word can represent its documents as XML, by putting data from various sources together, automated server-based processes can generate WORD Documents during running. Such documents can be easily updated on a regular basis, eliminating manual searches for relevant data and unnecessary re-typing.

Word and XML

Microsoft Word allows you to process XML documents in two ways:

  • Use Word XML ArchitectureYou can create a document in word and save it as an XML document. Word uses its own XML architecture (XML architecture: a formal specification written in XML, which defines the structure of XML documents, including element names and rich data types, and which elements should appear in combination, and what attributes each element has .) And wordml to apply XML tags that store information (such as file attributes) and define the structure of a document (such as paragraphs, headers, and tables ). According to the word XML architecture, word also uses XML to mark the storage format and layout information.
  • Use any XML ArchitectureYou can create or open a document in word, add any custom XML schema to the document, and apply the XML tag to the document content. When a document is saved as an XML document, the XML tag defines the document structure based on the XML schema appended to the document.

When you save a document, both the word architecture and the custom architecture are appended to the document by default, retaining the data defined by the custom architecture and the rich format defined by the word XML architecture. Based on the custom architecture, you can also choose to save only data.

Whether using the built-in word XML architecture to define the word document structure, or attaching your own architecture to define a structure that is more suitable for your business, any software that can analyze XML can read and process the XML format you saved (. XML file.

For example, if a custom architecture is used for resume data, the XML tag in the document defines the document structure by name, address, work experience, education, etc. When saving this document, you also have a document that looks very professional when printing, and a data file that can be processed by any program that can process XML.

You can also store XML data in a document that is saved as a Word document (.doc) or template (. Dot. However, only word can read or process the XML.

XML tag

When you attach a custom XML schema to a document,"XML structure"The task pane provides a list of elements defined in the schema. Select the document content and select an element from the list to apply the XML tag to the document. If the architecture defines the attributes of an element, you can also"XML structure"Specify these attributes in the task pane.

NoteYou can add multiple architectures to a document. Elements from all the additional architectures will"XML structure"The list of elements in the task pane.

The check box on the pane allows you to view embedded XML tags in the document context.

If the document structure does not comply with the schema rules, the document will be marked with purple lines and"XML structure"This violation error is reported in the task pane.

XSL Conversion

When opening or saving an XML document, you can apply the XSL conversion (XSLT) (XSL conversion (XSLT) that presents XML data in a specific format): a file, converts an XML document to another type of document, such as HTML or XML. The design is intended to be used as part of XSL .) File. For example, you may have an XSLT that displays data as a standard, and another XSLT that displays the same data as a part list (where the quantity and price are calculated.

XSLT applied when the document is opened

An XML document may have multiple XSLT associated with it. If so, you must select the XSLT you want to use to display the document. This is in"XML document"Completed in the pane, which lists available XSLT (data view ).

If there is no XSLT associated with the XML document, word will open the document using its default XSLT or "data view only.

If the document is attached with the word XML architecture, even if the document has An XSLT associated with it, word will not apply the XSLT when opening the document.

NoteYou can define a solution to associate XSLT with a specific type of XML document, rather than manually applying XSLT. This association needs to be performed in the "Architecture library", you can"Templates and add-ons"Dialog Box ("Tools"Menu)"XML architecture"Go to the "Architecture library" tab ".


XSLT applied when saving the document






XML schema is a structure file used to define XML, which can be called an XSD file. You can add architecture files in Word and Excel, so that you can use XSD to define the template architecture and save the template in XML format. When generating a report, you can use this template to generate the corresponding report.


First step: generate the corresponding XSD file.

According to the object-oriented thinking, most of the current three-tier architecture is used, and the multi-tier architecture is basically changed, in the model layer, if the instance objects use nhib.pdf, third-party Data Access layers (such as dynamic and soft generation), Microsoft's Entity Framework set, and so on. It only generates the corresponding architecture file based on the object class. The specific operation is: first generate the DLL file for the object Layer in the vs environment, and then enter the [path of the xsd dll file] in the command prompt of the vs command, you can see a file path suffixed with XSD.


Step 2: generate a template

Find the XSD file in the path. If it is a template for making a Word document (this document uses word as an example), in the newly created DOC file, tool-template & add-on-schema-add schema select just angry XSD file, fill in URL (namespace) such as:, it is better to choose to allow saving in XML file whether or not valid. In this way, you can view the XML structure, insert the corresponding elements at the corresponding position, and adjust the format as follows:


Save the template as an XML file.


Step 3: generate a report

Here I use a closed class, and its call Method



 Lxp. asc_businessreportthinginfo ASC = new lxp. asc_businessreportthinginfo (1); // entity class
ASC. Name = "Yi Jie ";
ASC. Birthday = datetime. now;
// Set the attribute values in the class

Wordreport RT = new wordreport (@ "E: \ word and exsel report learning \ word \ lxp. xml"); // load the template and use excelreport in Excel
Rt. inputdata (ASC); // Add data
Rt. Save (@ "E: \ word and exsel report learning \ word \ generated lxp.doc"); // save



In this way, you can customize the basic implementation of reports.

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: 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.