Hi RPA,
Refer the below sample code. For this i have used OpenXml dll.
C#
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Birthday", typeof(string)) });
dt.Rows.Add(1, "Firuz", "16.06.1988");
dt.Rows.Add(2, "Sadriddin", "08.12.1992");
dt.Rows.Add(3, "Nurullo", "25.03.1986");
string[] splitString = { "bin" };
// Path of blank document.
CreateWordDocument(AppDomain.CurrentDomain.BaseDirectory.Split(splitString, StringSplitOptions.None)[0] + "Files\\test.doc", dt);
}
public void CreateWordDocument(string filePath, DataTable data)
{
WordprocessingDocument doc = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document);
MainDocumentPart mainDocPart = doc.AddMainDocumentPart();
mainDocPart.Document = new Document();
Body body = new Body();
mainDocPart.Document.Append(body);
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
for (int i = 0; i < data.Rows.Count; ++i)
{
TableRow row = new TableRow();
for (int j = 0; j < data.Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Append(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data.Rows[i][j].ToString()))));
cell.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "1500" }));
row.Append(cell);
}
table.Append(row);
}
body.Append(table);
doc.MainDocumentPart.Document.Save();
doc.Dispose();
}
VB.Net
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Birthday", GetType(String))})
dt.Rows.Add(1, "Firuz", "16.06.1988")
dt.Rows.Add(2, "Sadriddin", "08.12.1992")
dt.Rows.Add(3, "Nurullo", "25.03.1986")
Dim splitString As String() = {"bin"}
' Path of blank document.
CreateWordDocument(AppDomain.CurrentDomain.BaseDirectory.Split(splitString, StringSplitOptions.None)(0) + "Files\test.doc", dt)
End Sub
Public Sub CreateWordDocument(filePath As String, data As DataTable)
Dim doc As WordprocessingDocument = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document)
Dim mainDocPart As MainDocumentPart = doc.AddMainDocumentPart()
mainDocPart.Document = New Document()
Dim body As New Body()
mainDocPart.Document.Append(body)
Dim table As New DocumentFormat.OpenXml.Wordprocessing.Table()
For i As Integer = 0 To data.Rows.Count - 1
Dim row As New TableRow()
For j As Integer = 0 To data.Columns.Count - 1
Dim cell As New TableCell()
cell.Append(New Paragraph(New DocumentFormat.OpenXml.Wordprocessing.Run(New DocumentFormat.OpenXml.Wordprocessing.Text(data.Rows(i)(j).ToString()))))
cell.Append(New TableCellProperties(New TableCellWidth() With { _
Key .Type = TableWidthUnitValues.Dxa, _
Key .Width = "1500" _
}))
row.Append(cell)
Next
table.Append(row)
Next
body.Append(table)
doc.MainDocumentPart.Document.Save()
doc.Dispose()
End Sub