Use the Aspose. Word Control to operate Word documents

Source: Internet
Author: User

The Aspose series of controls have good functions and have been using Aspose in my Winform development framework. cell is used for report output, which can implement a variety of report design and output. Generally, the output content is more formal or most of them are tables, so Aspose is generally used. cell to implement the various Excel report outputs I want. Although I have always known that Aspose. Word is used to generate Word Documents, and I believe it is also a very powerful control, it has never been used, so I am not very familiar with it.

By chance, the report function of a project was designated to be exported as a Word document. Therefore, I searched for many articles. However, most of the articles were simple, so I also referred to the official help introduction, finally met the customer's needs. Next, let me introduce how to use this control in actual business.

1. Word operations for two-dimensional tables
In daily work, the common content output is a two-dimensional table. The header is relatively fixed, and each row of content contains one line. How can we operate on the actual use of controls, in fact, there are many articles about this control. You can refer to it for reference. In fact, this article describes the importance of bookmarks. This is also true for the Aspose. Cell Control. Bookmarks can be used to replace content and mark the start position of content input.

First, we will draw a table header in a blank Word document, and then insert a tag reference when we start to wrap the line. There are two ways to insert bookmarks, one is in Word (2007, 2010) click Insert in the left-side navigation pane.

One is to add other commands on the custom quick access Toolbar of Word, as shown in the following steps:

The bookmarks inserted by the former do not contain text or special marks, but do exist. The latter inserts a gray block as a placeholder, as shown below, in my example of this two-dimensional table, use the latter for testing (the two are equally effective)

After the Word template is designed, the next step is to generate a two-dimensional table using code. First, I intentionally set a different width for each table cell. Therefore, the generated row must correspond to the header. Therefore, before each row is generated, you must obtain the style attribute of the corresponding column, otherwise it will not be matched. See the code below.

Copy codeThe Code is as follows: try
{
Aspose. Words. Document doc = new Aspose. Words. Document (templateFile );
Aspose. Words. DocumentBuilder builder = new Aspose. Words. DocumentBuilder (doc );

DataTable nameList = DataTableHelper. CreateTable ("No., name, time ");
DataRow row = null;
For (int I = 0; I <50; I ++)
{
Row = nameList. NewRow ();
Row ["no."] = I. ToString (). PadLeft (4, '0 ');
Row ["name"] = "Wu huacong" + I. ToString ();
Row ["time"] = DateTime. Now. ToString ();
NameList. Rows. Add (row );
}

List <double> widthList = new List <double> ();
For (int I = 0; I <nameList. Columns. Count; I ++)
{
Builder. MoveToCell (0, 0, I, 0); // move the cell
Double width = builder. CellFormat. Width; // obtain the cell width.
WidthList. Add (width );
}

Builder. MoveToBookmark ("table"); // start adding a value
For (var I = 0; I <nameList. Rows. Count; I ++)
{
For (var j = 0; j <nameList. Columns. Count; j ++)
{
Builder. InsertCell (); // Add a cell
Builder. CellFormat. Borders. LineStyle = LineStyle. Single;
Builder. CellFormat. Borders. Color = System. Drawing. Color. Black;
Builder. CellFormat. Width = widthList [j];
Builder. CellFormat. VerticalMerge = Aspose. Words. Tables. CellMerge. None;
Builder. CellFormat. VerticalAlignment = CellVerticalAlignment. Center; // vertical Center alignment
Builder. ParagraphFormat. Alignment = ParagraphAlignment. Center; // align horizontally
Builder. Write (nameList. Rows [I] [j]. ToString ());
}
Builder. EndRow ();
}
Doc. Range. Bookmarks ["table"]. Text = ""; // clear the mark

Doc. Save (saveDocFile );
If (MessageUtil. ShowYesNoAndTips ("saved successfully. Do you want to open the file? ") = System. Windows. Forms. DialogResult. Yes)
{
System. Diagnostics. Process. Start (saveDocFile );
}
}
Catch (Exception ex)
{
LogHelper. Error (ex );
MessageUtil. ShowError (ex. Message );
Return;
}

The steps for the above Code are:
1) Create Aspose. Words. Document and Aspose. Words. DocumentBuilder objects, and then generate the two-dimensional table content of the data.
2) traverse the template table or the width of each column for future use.
3) Move to the bookmarks position of the table and start to input data. The style and width of each Cell in the Word table must be set to match the table header.
4) save the file content to the new file.
The output result is as follows.

2. Merge Cells

The content of merged cells is often seen in common Word or Excel files. Therefore, this part is also very common and must be mastered.

Let's first look at an example code and its effect.

Copy codeThe Code is as follows: try
{
Aspose. Words. Document doc = new Aspose. Words. Document (templateFile );
Aspose. Words. DocumentBuilder builder = new Aspose. Words. DocumentBuilder (doc );

Builder. InsertCell ();
Builder. CellFormat. Borders. LineStyle = LineStyle. Single;
Builder. CellFormat. Borders. Color = System. Drawing. Color. Black;
Builder. CellFormat. VerticalMerge = CellMerge. First;
Builder. Write ("Text in merged cells .");

Builder. InsertCell ();
Builder. CellFormat. Borders. LineStyle = LineStyle. Single;
Builder. CellFormat. Borders. Color = System. Drawing. Color. Black;
Builder. CellFormat. VerticalMerge = CellMerge. None;
Builder. Write ("Text in one cell ");
Builder. EndRow ();

Builder. InsertCell ();
Builder. CellFormat. Borders. LineStyle = LineStyle. Single;
Builder. CellFormat. Borders. Color = System. Drawing. Color. Black;
// This cell is vertically merged to the cell above and shocould be empty.
Builder. CellFormat. VerticalMerge = CellMerge. Previous;

Builder. InsertCell ();
Builder. CellFormat. Borders. LineStyle = LineStyle. Single;
Builder. CellFormat. Borders. Color = System. Drawing. Color. Black;
Builder. CellFormat. VerticalMerge = CellMerge. None;
Builder. Write ("Text in another cell ");
Builder. EndRow ();

Doc. Save (saveDocFile );
If (MessageUtil. ShowYesNoAndTips ("saved successfully. Do you want to open the file? ") = System. Windows. Forms. DialogResult. Yes)
{
System. Diagnostics. Process. Start (saveDocFile );
}
}
Catch (Exception ex)
{
LogHelper. Error (ex );
MessageUtil. ShowError (ex. Message );
Return;
}

The effect is as follows:

About the introduction of cell merge, you can also refer to this official introduction: http://www.aspose.com/docs/display/wordsnet/Working+with+Merged+Cells

If the above example is not clear enough, OK, I will introduce a practical example to illustrate the operation mode of merging cells.

The actual document is generated as follows:

The document template is as follows:

In fact, the "test" content in this section is written in code. In fact, it is a line of business data that is displayed in two rows. Some of the merged cells are displayed in a table form of a real project. We noticed that each row has 13 cells, and columns 1, 2, and 13th are merged columns. A feature of the two indexes is that they are both valid, but they can only be copied using the first index, and the second index is useless.

For example, if the first column is a parallel column, it should have an index like 0 and 13, and the second column is also a parallel column. It also has an index like 1 and 14, and so on.

After learning about the logical relationship, we can see the actual operation code as follows.

Written by: Wu huacong

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.