To make your message easy to understand in your Word document, usually a table is enough. Table in Word document will give a detailed explanation for its audiences. And table is very easy to be created no matter in Microsoft Word or through other ways such as using C#.
How to Use C#/VB.NET Create Word Table?
Use C# create Word table might be a little complicated compare with in Microsoft Word. But with this solution, you don’t have to install Microsoft Word on system. There are some libraries which may help you save time or give you some other choices like editing something. Spire.Doc is one of these libraries. Spire.Doc is a MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight.
Use C# create Word table through the help of Spire.Doc, only 4 simple steps are enough.
1, Create a Word Table
Use section.AddTable() method to create a table object
Note: the table class in Spire.Doc is Spire.Doc.Table.
2, Give Table Size
Use table.ResetCells method to specify how many rows and columns you need in the table. Just give 2 numbers of parameters, one for rows and another for columns.
3, Fill Table Header
The first row of table is header for usual. You may operate the header by setting height and background color, and then fill data.
4, Fill other data in Table
Download Spire.Doc (or Spire.Office which includes Spire.Doc) with .NET framework 2.0 (or above). Follow the steps above and use the code below to create Word table with C#.
C# Create Word Table:
001 using Spire.Doc.Documents;
002 using Spire.Doc;
003 using System.Drawing;
004 using Spire.Doc.Fields;
005 using System;
006
007
008 namespace InsertingComments
009 {
010
011 class Program
012 {
013 static void Main(string[] args)
014 {
015 //Create word document.
016 Document document = new Document();
017
018 //Add a section in the document.
019 Section section = document.AddSection();
020
021 //Fill the table with header.
022 String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
023 String[][] data =
024 {
025 new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
026 new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
027 new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
028 new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
029 new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
030 new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"},
031 new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
032 new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
033 new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
034 new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
035 new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"},
036 new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"},
037 new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"},
038 new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
039 new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
040 new String[]{"United States of America", "Washington", "North America", "9363130",
041 "249200000"},
042 new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"},
043 new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"}
044 };
045
046 //Create a table.
047 Spire.Doc.Table table = section.AddTable();
048
049 //Set table size.
050 table.ResetCells(data.Length + 1, header.Length);
051
052 //***************** First Row *************************
053 TableRow row = table.Rows[0];
054 row.IsHeader = true;
055
056 //Set the Height of the header.
057 row.Height = 20; //unit: point, 1point = 0.3528 mm
058
059 //Set the Height Type of the header.
060 row.HeightType = TableRowHeightType.Exactly;
061
062 //Set the Back Color of the header.
063 row.RowFormat.BackColor = Color.Gray;
064 for (int i = 0; i < header.Length; i++)
065 {
066
067 //Set the Cell Format of the header.
068 row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
069 Paragraph p = row.Cells[i].AddParagraph();
070
071 //Set the Horizon of the header.
072 p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
073 TextRange txtRange = p.AppendText(header[i]);
074
075 //Set the Character Format of the header.
076 txtRange.CharacterFormat.Bold = true;
077 }
078
079 //***************** Data Row *************************
080 for (int r = 0; r < data.Length; r++)
081 {
082 TableRow dataRow = table.Rows[r + 1];
083
084 //Set the Height of the data.
085 dataRow.Height = 20;
086
087 //Set the Height Type of the data.
088 dataRow.HeightType = TableRowHeightType.Exactly;
089
090 //Set the Back Color of the data.
091 dataRow.RowFormat.BackColor = Color.Empty;
092
093 //"c" is the Column of the data.
094 for (int c = 0; c < data[r].Length; c++)
095 {
096
097 //Set the Cell Format of the data.
098 dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
099
100 //Fill the Cell Format with the data.
101 dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
102 }
103 }
104
105 //Save doc file.
106 document.SaveToFile("Sample.doc", FileFormat.Doc);
107
108 //Launch the file.
109 System.Diagnostics.Process.Start("Sample.doc");
110 }
111 }
112 }
After you running your application with the code above, you will get your Word table:
How to Use C#/VB.NET Create Word Table?
Use C# create Word table might be a little complicated compare with in Microsoft Word. But with this solution, you don’t have to install Microsoft Word on system. There are some libraries which may help you save time or give you some other choices like editing something. Spire.Doc is one of these libraries. Spire.Doc is a MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight.
Use C# create Word table through the help of Spire.Doc, only 4 simple steps are enough.
1, Create a Word Table
Use section.AddTable() method to create a table object
Note: the table class in Spire.Doc is Spire.Doc.Table.
2, Give Table Size
Use table.ResetCells method to specify how many rows and columns you need in the table. Just give 2 numbers of parameters, one for rows and another for columns.
3, Fill Table Header
The first row of table is header for usual. You may operate the header by setting height and background color, and then fill data.
4, Fill other data in Table
Download Spire.Doc (or Spire.Office which includes Spire.Doc) with .NET framework 2.0 (or above). Follow the steps above and use the code below to create Word table with C#.
C# Create Word Table:
001 using Spire.Doc.Documents;
002 using Spire.Doc;
003 using System.Drawing;
004 using Spire.Doc.Fields;
005 using System;
006
007
008 namespace InsertingComments
009 {
010
011 class Program
012 {
013 static void Main(string[] args)
014 {
015 //Create word document.
016 Document document = new Document();
017
018 //Add a section in the document.
019 Section section = document.AddSection();
020
021 //Fill the table with header.
022 String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
023 String[][] data =
024 {
025 new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
026 new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
027 new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
028 new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
029 new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
030 new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"},
031 new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
032 new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
033 new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
034 new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
035 new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"},
036 new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"},
037 new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"},
038 new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
039 new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
040 new String[]{"United States of America", "Washington", "North America", "9363130",
041 "249200000"},
042 new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"},
043 new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"}
044 };
045
046 //Create a table.
047 Spire.Doc.Table table = section.AddTable();
048
049 //Set table size.
050 table.ResetCells(data.Length + 1, header.Length);
051
052 //***************** First Row *************************
053 TableRow row = table.Rows[0];
054 row.IsHeader = true;
055
056 //Set the Height of the header.
057 row.Height = 20; //unit: point, 1point = 0.3528 mm
058
059 //Set the Height Type of the header.
060 row.HeightType = TableRowHeightType.Exactly;
061
062 //Set the Back Color of the header.
063 row.RowFormat.BackColor = Color.Gray;
064 for (int i = 0; i < header.Length; i++)
065 {
066
067 //Set the Cell Format of the header.
068 row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
069 Paragraph p = row.Cells[i].AddParagraph();
070
071 //Set the Horizon of the header.
072 p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
073 TextRange txtRange = p.AppendText(header[i]);
074
075 //Set the Character Format of the header.
076 txtRange.CharacterFormat.Bold = true;
077 }
078
079 //***************** Data Row *************************
080 for (int r = 0; r < data.Length; r++)
081 {
082 TableRow dataRow = table.Rows[r + 1];
083
084 //Set the Height of the data.
085 dataRow.Height = 20;
086
087 //Set the Height Type of the data.
088 dataRow.HeightType = TableRowHeightType.Exactly;
089
090 //Set the Back Color of the data.
091 dataRow.RowFormat.BackColor = Color.Empty;
092
093 //"c" is the Column of the data.
094 for (int c = 0; c < data[r].Length; c++)
095 {
096
097 //Set the Cell Format of the data.
098 dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
099
100 //Fill the Cell Format with the data.
101 dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
102 }
103 }
104
105 //Save doc file.
106 document.SaveToFile("Sample.doc", FileFormat.Doc);
107
108 //Launch the file.
109 System.Diagnostics.Process.Start("Sample.doc");
110 }
111 }
112 }
After you running your application with the code above, you will get your Word table: