Hi PRA,
Refer below sample code.
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] { new DataColumn("LastName"), new DataColumn("FatherName"), new DataColumn("Adress"), new DataColumn("Name"), new DataColumn("Birthday") });
dt.Rows.Add("Pulodov", "Abdulloevich", "city Dushanbe", "Rustam", "22.12.1987");
string col1 = "LastName: " + dt.Rows[0]["LastName"].ToString() + '\n' + "Name: " + dt.Rows[0]["Name"].ToString();
string col2 = "FatherName: " + dt.Rows[0]["FatherName"].ToString() + '\n' + "Birthday: " + dt.Rows[0]["Birthday"].ToString();
string col3 = "Adress: " + dt.Rows[0]["Adress"].ToString();
string[,] tablerowcolumn = new string[1, 3] { { col1, col2, col3 } };
AddTable(@"C:\Test.docx", tablerowcolumn);
Process.Start(@"C:\Test.docx");
}
public static void AddTable(string fileName, string[,] data)
{
using (var document = WordprocessingDocument.Open(fileName, true))
{
var doc = document.MainDocumentPart.Document;
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
table.ClearAllAttributes();
for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
string[] datas = data[i, j].ToString().Split('\n');
for (int k = 0; k < datas.Length; k++)
{
tc.Append(new Paragraph(new Run(new Text(datas[k]))));
tc.Append(new TableCellProperties(new TableCellVerticalAlignment { Val = TableVerticalAlignmentValues.Center }));
}
tr.Append(tc);
}
table.Append(tr);
}
doc.Body.Append(table);
doc.Save();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(4) {New DataColumn("LastName"), New DataColumn("FatherName"), New DataColumn("Adress"), New DataColumn("Name"), New DataColumn("Birthday")})
dt.Rows.Add("Pulodov", "Abdulloevich", "city Dushanbe", "Rustam", "22.12.1987")
Dim col1 As String = "LastName: " + dt.Rows(0)("LastName").ToString() + ControlChars.Lf + "Name: " + dt.Rows(0)("Name").ToString()
Dim col2 As String = "FatherName: " + dt.Rows(0)("FatherName").ToString() + ControlChars.Lf + "Birthday: " + dt.Rows(0)("Birthday").ToString()
Dim col3 As String = "Adress: " + dt.Rows(0)("Adress").ToString()
Dim tablerowcolumn As String(,) = New String(0, 2) {{col1, col2, col3}}
AddTable("C:\Test.docx", tablerowcolumn)
Process.Start("C:\Test.docx")
End Sub
Public Sub AddTable(fileName As String, data As String(,))
Using document = WordprocessingDocument.Open(fileName, True)
Dim doc = document.MainDocumentPart.Document
Dim table As New DocumentFormat.OpenXml.Wordprocessing.Table()th {.Val = New EnumValue(Of BorderValues)(BorderValues.[Single]), .Size = 6}))
table.ClearAllAttributes()
For i As Integer = 0 To data.GetUpperBound(0)
Dim tr = New DocumentFormat.OpenXml.Wordprocessing.TableRow()
For j As Integer = 0 To data.GetUpperBound(1)
Dim tc = New DocumentFormat.OpenXml.Wordprocessing.TableCell()
Dim datas As String() = data(i, j).ToString().Split(ControlChars.Lf)
For k As Integer = 0 To datas.Length - 1
tc.Append(New Paragraph(New Run(New Text(datas(k)))))
tc.Append(New TableCellProperties(New TableCellVerticalAlignment() With {.Val = TableVerticalAlignmentValues.Center}))
Next
tr.Append(tc)
Next
table.Append(tr)
Next
doc.Body.Append(table)
doc.Save()
End Using
End Sub
OutPut
LastName: Pulodov
Name: Rustam
|
FatherName: Abdulloevich
Birthday: 22.12.1987
|
Adress: city Dushanbe
|