Hi PRA,
Refer the below sample. For this i have used OpenXml.
C#
using System;
using System.Data;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Diagnostics;
public partial class CS : System.Web.UI.Page
{
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[,] Tablero = new string[1, 3] { { col1, col2, col3 } };
AddTable(@"C:\Users\dharmendra\Desktop\Test.docx", Tablero);
Process.Start(@"C:\Users\dharmendra\Desktop\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();
TableProperties props = new TableProperties(
new TableBorders(
new TopBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new LeftBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new RightBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new InsideHorizontalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new InsideVerticalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 }
));
table.ClearAllAttributes();
table.AppendChild<TableProperties>(props);
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 }));
}
//tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Auto }));
tr.Append(tc);
}
table.Append(tr);
}
doc.Body.Append(table);
doc.Save();
}
}
}
VB.Net
Imports System.Data
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Imports System.Diagnostics
Partial Class VB
Inherits System.Web.UI.Page
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 Tablero As String(,) = New String(0, 2) {{col1, col2, col3}}
AddTable("C:\Users\dharmendra\Desktop\Test.docx", Tablero)
Process.Start("C:\Users\dharmendra\Desktop\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()
Dim props As New TableProperties(New TableBorders(
New TopBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.[Single]), .Size = 6},
New BottomBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.[Single]), .Size = 6},
New LeftBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.[Single]), .Size = 6},
New RightBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.[Single]), .Size = 6},
New InsideHorizontalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.[Single]), .Size = 6},
New InsideVerticalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.[Single]), .Size = 6}))
table.ClearAllAttributes()
table.AppendChild(Of TableProperties)(props)
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
'tc.Append(New TableCellProperties(New TableCellWidth() With {.Type = TableWidthUnitValues.Auto}))
tr.Append(tc)
Next
table.Append(tr)
Next
doc.Body.Append(table)
doc.Save()
End Using
End Sub
End Class
Output
LastName: Pulodov
Name: Rustam
|
FatherName: Abdulloevich
Birthday: 22.12.1987
|
Adress: city Dushanbe
|