C # uses OPENXML to add tables to Word documents

Source: Internet
Author: User

The example in this paper describes how C # uses OPENXML to add tables to Word documents, which is a very practical technique. Share to everyone for your reference. The specific analysis is as follows:

This will show you how to add a table to Word using OPENXML. Header and data in the code we use the same TableRow to add, in fact, can be through tableheader, in fact, are the same. We'll also show you how to set the cell style later. The table header line can be controlled by setting its own style.

The sample code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using documentformat.openxml;using documentformat.openxml.packaging;using DocumentFormat.OpenXml.Wordprocessing; Namespace addtabletoword{public class program {public static void Main (string[] args) {List<string[]&gt ;      Lstdata = new List<string[]> () {new string[] {"1", "2", "3"}, new string[] {"3", "2", "1"}};      string[] Headerarray = new string[] {"A", "B", "C"};    AddTable ("Test.docx", Lstdata, Headerarray); }///<summary>//Word Add Table///</summary>//<param name= "wordpath" >word file path </par am>//<param name= "Lstdata" > Data </param>//<param name= "Headerarray" > Header </param> Publ IC static void AddTable (String wordpath, list<string[]> lstdata, string[] headerarray) {using (Wordprocessi Ngdocument doc = Wordprocessingdocument.open (Wordpath, True)) {       Tablegrid Grid = new Tablegrid (); int maxcolumnnum = lstdata.select (x = X.count ()).        Max (); for (int index = 0; index < maxcolumnnum; index++) {grid.        Append (New Tablegrid ());  }//Set table border tableproperties Tblprop = new Tableproperties (new Tableborders (new Topborder () { val = new Enumvalue<bordervalues> (bordervalues.single), Size = 2}, new Bottomborder () {val = new enumvalue <BorderValues> (bordervalues.single), Size = 2}, new Leftborder () {Val = new enumvalue<bordervalues> (B Ordervalues.single), Size = 2}, new Rightborder () {Val = new enumvalue<bordervalues> (bordervalues.single), size = 2}, new Insidehorizontalborder () {Val = new enumvalue<bordervalues> (bordervalues.single), size = 2}        , new Insideverticalborder () {Val = new enumvalue<bordervalues> (bordervalues.single), Size = 2})        );   Table table = new Table ();     Table.        Append (Tblprop); Add a table header.        In fact, there are tableheader objects, the younger brother can not use.        TableRow HeaderRow = new TableRow ();          foreach (String headerstr in Headerarray) {TableCell cell = new TableCell (); Cell.          Append (New Paragraph (New-Text (HEADERSTR)));        Headerrow.append (cell); } table.        Append (HeaderRow);          Add data foreach (string[] Rowarray in lstdata) {TableRow row = new TableRow ();            foreach (String Strcell in Rowarray) {TableCell cell = new TableCell (); Cell.            Append (New Paragraph (New-Text (Strcell))); Row.          Append (cell); } table.        Append (row); } doc.      MainDocumentPart.Document.Body.Append (New Paragraph, New Run (table)); }    }  }}

The execution renders the result as follows:

I hope this article is helpful to everyone's C # program design

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # uses OPENXML to add tables to Word documents

This address: http://www.paobuke.com/develop/c-develop/pbk23474.html






Related content C # How to get (read) property values by property name c#êμ?? ′???? ÷1|? Üμ? Comboboxc# several ways to process pictures in the background C # code operations XML to add, delete, change operations
Distinguish between the similarities and differences between WCF and WebService, the advantage of C # implementation to image Watermarking method C # Connect the database and update the database using Aspose.word third-party plug-in implementation to export word

C # uses OPENXML to add tables to Word documents

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.