This post aims at introducing a simple method to set table column width with C#, VB.NET. An MS Word component Spire.Doc is needed in the method, you can Freely Download Spire.Doc in a few minutes.
I only need three steps to finish the whole function by using Spire.Doc. Please look at the below procedure.
Step1. Create a new project.
1. Create a new project in Visual Studio, this project needs a Form. Please add a button in Form1.
2. Set the Target framework to be .NET Framework 2 or above.
3. Add Spire.Doc dll as reference.
4. Add the following code at the top of the method.
C# Code:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
VB.NET Code:
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Step2. Set Word table column width.
1. Load a word document from system.
C# Code:
Document Document = new Document();
Document.LoadFromFile(@"E:\word table.docx", FileFormat.Docx);
VB.NET Code:
Dim Document As New Document()
Document.LoadFromFile("E:\word table.docx", FileFormat.Docx)
2. Set the first table column width in Word.
C# Code:
for (int i = 0; i < Document.Sections[0].Tables[0].Rows.Count; i++)
{
Document.Sections[0].Tables[0].Rows[i].Cells[0].Width = 30;
}
VB.NET Code:
For i As Integer = 0 To Document.Sections(0).Tables(0).Rows.Count - 1
Document.Sections(0).Tables(0).Rows(i).Cells(0).Width = 30
Next
C# Code:
Document.SaveToFile(@"Sample.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Sample.docx");
VB.NET Code:
Document.SaveToFile("Sample.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("Sample.docx")
Preview
Word is so powerful that it can help use edit our text in different ways. As its component, Spire.Doc can realize word functions in a simple way with C#, VB.NET.
More Word Functions.