My blog othersArticleLink:
[Original translation]C #Open source lightweight object databaseNdatabaseIntroduction
[Reprinted Summary] knowledge about generic base classes
Newlife. xcodeComponent Resource Directory
Newlife. xcodeObject container and interface operation instance
Own your ownCodeGenerator-Newlife. xcodeCode Generator Analysis
Have your own code generator-Newlife. xcodeTemplate compilation tutorial
1. Preface
read/write offic there are many ways to use documentation, there are many components. We will not discuss the advantages and disadvantages of other methods here, but will introduce you to an open-source read/write word document components. Read/write Excel Yes npoi , read/write word let's see docx.
Docx is an intuitive and simple operation.Word 2007/2010Lightweight File. NetComponent. It is fast and does not need to install MicrosoftOfficeSoftware. Free and small in ChinaWPSThere are enough reasons for many users to give up on the hugeOfficeThat is useful in the actual software development process. Unfortunately, it does not support 2003, but it is always the trend of elimination, and there is no difference in the use of WPS.
2. docxMain features
DocxThe latest version of the component isV1.0.0.12And has the following features:
1. Supports inserting, deleting, and replacing texts in files. All text formats, such as font, underline, and highlight, are supported.
2. Supports inserting images, hyperlinks, tables, headers and footers, and custom attributes.
3. Support for similarJqueryIs convenient for programming and development.
3. docx Example
The main objects of a Word document in docx are paragraph (paragraph), image, table, and custom attributes (customproperty ). For details about the usage and API interfaces, refer to the CHM document. I didn't make the latest version myself. I used the previous version and basically used it. If you have the energy, you can view the source code and make a copy. Note: Unless otherwise specified, most of the Code in this article comes from the help documentation of v1.0.0.10. The new 11 and 12 versions do not seem to have added major new features, but they only modify a lot of bugs, we have added cases and unit tests. If you haven't introduced them, you can go to the document and download the latest source code from the open-source website.
3.1 Create and Load Doc Document
Create and load word the document methods are all in docx in the class, the operation is also very simple, as shown below:
1 //Create a New Word document. load is used in the same way as create.2 Using(Docx document = docx. Create (@".. \ Test.docx"))3 {4//Add basic objects, such as paragraphs5Document. Save ();//Save6}
3.2 Add paragraph and control format
Text section and format control isWordThe most common object of a document,Docx supports inserting paragraphs and fully controlling the paragraph format. The following is an example.
To insert a paragraph, follow these steps:DocxClassThe insertparagraph method has several usage methods, which can not only control the insert position, but also control the format.
1 Using (Docx document = docx. Create ( @" Test.docx " )) 2 { 3 // First, create one format object. 4 Formatting formatting = New Formatting (); 5 Formatting. Bold = True ; 6 Formatting. fontcolor = Color. Red; 7 Formatting. size = 30 ; 8 // Control the insert position of a paragraph 9 Int Index = Document. Text. Length/ 2 ; 10 // Insert text to a specified position and control the format 11 Document. insertparagraph (index, " New text " , False , Formatting ); 12 Document. Save (); // Save document 13 }
Note: The preceding section only inserts a paragraph. You can also define a paragraph object paragraph separately, insert tables, images, and perform custom operations in this section. The following is an example of image operations:
1 Using (Docx document = docx. Create ( @" Test.docx " )) 2 { 3 // Create a paragraph object for the document 4 Paragraph P = Document. insertparagraph ( " Here is Picture 1 " , False ); 5 // Add an image to the document 6 Novacode. Image IMG = Document. addimage ( @" Image.jpg " ); 7 // Insert the image to the end of the paragraph 8 Picture PIC = P. insertpicture (IMG. ID, " Photo 31415 " , " A pie I baked. " ); 9 // Select an image and modify the image size 10 PIC. Rotation = 30 ; 11 PIC. width = 400 ; 12 PIC. Height = 300 ; 13 // Set the image shape and flip the image horizontally 14 PIC. setpictureshape (basicshapes. cube ); 15 PIC. fliphorizontal = True ; 16 Document. Save (); // Save document 17 }
The formatting class is all attributes and there is no method. There are many attributes that control the format, and you can directly set it. Currently, we have not found that this version supports styles, so we can only define one global format to control the title. You can view the documents of this class for specific format control. Too many. If you do not list them one by one, the above Code is basically used.
In addition, the new version supports the hyperlink function, which is easy to use. The addhyperlink method of docx is directly completed.
3.3 Insert image and image control
In the above example, we have already introduced several ways to insert images.DocxObject directlyAddimageAnd insert it into a paragraph. Where3.2The example has detailed the image control, such as modifying the size and rotating. For more detailed settings, seePicture class.
3.4 Insert table and Table Control
Table is another important word document. It is also a very high proportion in the actual use environment. First, we will introduce the structure of table objects in docx:
One Table object table contains rows and columns. The rows and columns can be accessed through indexes. The basic unit of a table object is cell. Each row object consists of multiple cell objects. The cell object has a default paragraph object, which allows you to control the format of this paragraph object, to control the cell format. You can also set related attributes, alignment, and adaptive table size for tables. Table objects can be operated flexibly. you can insert or remove rows or insert or remove columns.
There are also several ways to insert a docx object into a table. You can directly use the docx object inserttable method or insert a paragraph object after the paragraph object. Likewise, you can insert a paragraph object before and after the table object, this is related to the layout. Let's look at a comprehensive example:
3.5 Header and footer Control
The use of pages and footer is very similar. Three types of pages are supported: homepage, odd page, and even page. It seems that there is no supported section. Here, we will only list the basic usage of the footer.
1 Using (Docx document = docx. Create ( @" Test.docx " )) 2 { 3 Document. addfooters (); // Add all footer 4 Footers = Document. footers; // Get all footer of this document 5 // Get the footer of the first page of the document 6 Footer first = Footers. first; 7 // Retrieve the footer of an odd page 8 Footer odd = Footers. Odd; 9 // Obtains the footer of an even page. 10 Even = Footers. Even; 11 // Set different pages to use different footers 12 Document. differentfirstpage = True ; 13 Document. differentoddandevenpages = True ; 14 // Set footer content 15 Paragraph P = First. insertparagraph (); 16 P. append ( " This is the first pages footer. " ); 17 Document. Save (); // Remember to save 18 }
3.6 Custom attribute support
Although docx has not been put into practical use yet, I personally think that the support for custom attributes is the most powerful one. You can not only create templates by yourself, you can also add custom properties directly in docx, which is useful when exporting large batches of documents in the same format. For example, printing a transcript or report. Since I have never used word 2007 or a later version, I have never touched on this custom attribute function. In my understanding, it is similar to a placeholder or text control. during development, you can fill in values for these positions to generate several files in batches. The following is an example of adding custom attributes directly and assigning values. If you are more advanced, you can customize attributes in a more detailed manner.
1 Using (Docx document = docx. Load ( @" C: \ example \ test.docx " )) 2 { 3 // First define a custom property 4 Customproperty forename; 5 // First, determine whether the custom attribute of the name exists in the document. 6 If (! Document. customproperties. containskey ( " Forename " )) 7 { // If it does not exist, create one and assign it "Cathal" 8 Document. addcustomproperty ( New Customproperty ( " Forename " , " Cathal " )); 9 } 10 // Obtain the custom property and print its value. 11 Forename = Document. customproperties [ " Forename " ]; 12 Console. writeline ( String . Format ( " Name: '{0}', value: '{1}' \ npress any key... " , 13 Forename. Name, forename. Value )); 14 Document. Save (); 15 }
4. Resources
Open source Web site: http://docx.codeplex.com/
It is helpful to you. Don't try your mouse.