DsWord allows you to add, modify, and delete a table from a Word document. A table can be created in a document using Add or Insert method of the TableCollection class. It also lets you format the table using the TableFormat class properties and apply styles to the table using Style class properties.
To create a table in Word document using DsWord:
C# |
Copy Code |
---|---|
GcWordDocument doc = new GcWordDocument(); //Access the first section of the document Section section = doc.Body.Sections.First; //Add a paragraph in the section Paragraph header = section.GetRange().Paragraphs.Add(" Adding a table:"); // Add an empty table with: int rows = 6; int cols = 4; Table t = section.GetRange().Tables.Add(0, 0); // Add some rows and cells to it: List<string> cells = new List<string>(cols); for (int col = 0; col < cols; ++col) cells.Add(string.Empty); for (int row = 0; row < rows; ++row) { for (int col = 0; col < cols; ++col) cells[col] = $"Row {row + 1}, col {col + 1}"; t.Rows.Add(cells.ToArray()); } // Create a new table style: Style ts1 = doc.Styles.Add("Table Style 1", StyleType.Table); foreach (var border in ts1.Table.Borders) { border.LineStyle = LineStyle.BasicBlackDots; border.LineWidth = 0.5f; border.Color.RGB = Color.Purple; } //Apply the table style on the table t.Style = ts1; //Save the document doc.Save("CreateTable.docx"); |
To modify the table formatting in a Word document:
C# |
Copy Code |
---|---|
//Load an existing document doc.Load("CreateTable.docx"); //Access the table Table t = doc.Body.Sections.First.GetRange().Tables[0]; //Modify the table t.Format.AllowAutoFit = true; t.Format.Alignment = TableAlignment.Center; //Save the document doc.Save("ModifiedDoc.docx"); |
To delete a table from a Word document:
C# |
Copy Code |
---|---|
//Load an existing document doc.Load("CreateTable.docx"); //Access the table Table t = doc.Body.Sections.First.GetRange().Tables[0]; //Delete the table t.Delete(); //Save the document doc.Save("TableDeleted.docx"); |
To allow automatic resizing of cells in a table according to their content, use AllowAutoFit property of the TableFormatBase class.
C# |
Copy Code |
---|---|
//Load an existing document doc.Load("CreateTable.docx"); //Access the table and allow automatic resizing of the cells Table t = doc.Body.Sections.First.GetRange().Tables[0]; t.Format.AllowAutoFit = true; //Save the document doc.Save("AutoFitEnabled.docx"); |
To spit a table in a Word document:
C# |
Copy Code |
---|---|
doc.Load("CreateTable.docx"); //Access the table and it's second row Table t = doc.Body.Sections.First.GetRange().Tables[0]; Row row = t.Rows[1]; //This generates a new table from the specified row of an existing table. Table splitTable1 = t.Split(row, InsertLocation.After); //Insert a paragraph (without content) between the two tables splitTable1.GetRange().Paragraphs.Insert(InsertLocation.Before); //Save the document doc.Save("SplitTable.docx"); |
To set table styles in a Word document:
C# |
Copy Code |
---|---|
//Load the document doc.Load("CreateTable.docx"); //Access the table Table t = doc.Body.Sections.First.GetRange().Tables[0]; // Create a new table style: Style ts1 = doc.Styles.Add("Table Style 2", StyleType.Table); ts1.Table.ColumnStripe=3; // Assign the style to the table: t.Style = ts1; //Set table formatting t.Format.Alignment = TableAlignment.Center; t.Format.Title = "Test Table"; t.Format.Description = "This is the table description"; //Save the document doc.Save("SetTableInfo.docx"); |
To get table styles and formatting:
C# |
Copy Code |
---|---|
//Load the document doc.Load("SetTableInfo.docx"); //Access the table Table t = doc.Body.Sections.First.GetRange().Tables[0]; //Get table styles Style st=t.Style; uint cstripe = st.Table.ColumnStripe; Console.WriteLine("Table stripe: " + cstripe.ToString()); //Get table formatting TableFormat tformat = t.Format; string title = tformat.Title; //Write the fetched title of the table on the console Console.WriteLine("Table Title: " + title); |
To set the indentation of a table in Word document:
C# |
Copy Code |
---|---|
//Load the table doc.Load("CreateTable.docx"); //Access the table and allow automatic resizing of the cells Table t = doc.Body.Sections.First.GetRange().Tables[0]; t.Format.AllowAutoFit = true; // Create a new table style to set the Indentation Style ts1 = doc.Styles.Add("Table Style 2", StyleType.Table); //Set the indentation ts1.Table.Indent = 3; //Apply the style t.Style = ts1; //Save the document doc.Save("SetIndent.docx"); |
To get the indentation of a table from a Word document:
C# |
Copy Code |
---|---|
//Load an existing document doc.Load("SetIndent.docx"); //Access the table Table t = doc.Body.Sections.First.GetRange().Tables[0]; //Get the style applied on the table Style s = t.Style; //Get the indentation float indenting=s.Table.Indent; //Write the fetched indentation(in points) on the console Console.WriteLine("Get Indenting: " + indenting); |
For more information about implementation of tables using DsWord, see DsWord sample browser.