Aspose. Words insert a table using code

Source: Internet
Author: User

Aspose. Words is a powerful word document processing control. You can create, modify, and convert a word without having to install it.

Aspose. Words can be used to insert Word tables simply by using the DocumentBuilder class library provided by this product.

DocumentBuilder. StartTable starts to build a new table.
DocumentBuilder. InsertCell Insert new rows and cells to the table
DocumentBuilder. Writeln writes text to the current cell
DocumentBuilder. EndRow is used to indicate that the current row is ended and a new row is started.
DocumentBuilder. EndTable indicates that the table has been built.

The following code inserts a simple unformatted table into word:
Document doc = new Document ();
DocumentBuilder builder = new DocumentBuilder (doc );

// We call this method to start building the table.
Builder. StartTable ();
Builder. InsertCell ();
Builder. Write ("Row 1, Cell 1 Content .");

// Build the second cell
Builder. InsertCell ();
Builder. Write ("Row 1, Cell 2 Content .");
// Call the following method to end the row and start a new row.
Builder. EndRow ();

// Build the first cell of the second row.
Builder. InsertCell ();
Builder. Write ("Row 2, Cell 1 Content ");

// Build the second cell.
Builder. InsertCell ();
Builder. Write ("Row 2, Cell 2 Content .");
Builder. EndRow ();

// Signal that we have finished building the table.
Builder. EndTable ();

// Save the document to disk.
Doc. Save (MyDir + "DocumentBuilder. CreateSimpleTable Out.doc ");

The following code shows how to use the code to insert a formatted table to word:

Document doc = new Document ();
DocumentBuilder builder = new DocumentBuilder (doc );

Table table = builder. StartTable ();

// Make the header row.
Builder. InsertCell ();

// Set the left indent for the table. Table wide formatting must be applied after
// At least one row is present in the table.
Table. LeftIndent = 20.0;

// Set height and define the height rule for the header row.
Builder. RowFormat. Height = 40.0;
Builder. RowFormat. HeightRule = HeightRule. AtLeast;

// Some special features for the header row.
Builder. CellFormat. Shading. BackgroundPatternColor = Color. FromArgb (198,217,241 );
Builder. ParagraphFormat. Alignment = ParagraphAlignment. Center;
Builder. Font. Size = 16;
Builder. Font. Name = "Arial ";
Builder. Font. Bold = true;

Builder. CellFormat. Width = 100.0;
Builder. Write ("Header Row, \ n Cell 1 ");

// We don't need to specify the width of this cell because it's inherited from the previous cell.
Builder. InsertCell ();
Builder. Write ("Header Row, \ n Cell 2 ");

Builder. InsertCell ();
Builder. CellFormat. Width = 200.0;
Builder. Write ("Header Row, \ n Cell 3 ");
Builder. EndRow ();

// Set features for the other rows and cells.
Builder. CellFormat. Shading. BackgroundPatternColor = Color. White;
Builder. CellFormat. Width = 100.0;
Builder. CellFormat. VerticalAlignment = CellVerticalAlignment. Center;

// Reset height and define a different height rule for table body
Builder. RowFormat. Height = 30.0;
Builder. RowFormat. HeightRule = HeightRule. Auto;
Builder. InsertCell ();
// Reset font formatting.
Builder. Font. Size = 12;
Builder. Font. Bold = false;

// Build the other cells.
Builder. Write ("Row 1, Cell 1 Content ");
Builder. InsertCell ();
Builder. Write ("Row 1, Cell 2 Content ");

Builder. InsertCell ();
Builder. CellFormat. Width = 200.0;
Builder. Write ("Row 1, Cell 3 Content ");
Builder. EndRow ();

Builder. InsertCell ();
Builder. CellFormat. Width = 100.0;
Builder. Write ("Row 2, Cell 1 Content ");

Builder. InsertCell ();
Builder. Write ("Row 2, Cell 2 Content ");

Builder. InsertCell ();
Builder. CellFormat. Width = 200.0;
Builder. Write ("Row 2, Cell 3 Content .");
Builder. EndRow ();
Builder. EndTable ();

Doc. Save (MyDir + "DocumentBuilder. CreateFormattedTable Out.doc ");

Trial download: http://www.componentcn.com/html/wbbjkj_281_3926.html

Contact: 846631466

Related Article

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.