In this article I will explain with an example, how to convert HTML file to Word document in Windows Forms (WinForms) Application using C# and VB.Net.
Install Microsoft.Office.Interop.Word package
Form Design
The Form consist of following controls:
DataGridView – For displaying data.
Button – For exporting DataGridView to HTML file and converting it to Word document.
The Button has been assigned with a Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using DataTable = System.Data.DataTable;
using Microsoft.Office.Interop.Word;
VB.Net
Imports System.IO
Imports System.Data
Imports DataTable = System.Data.DataTable
Imports Microsoft.Office.Interop.Word
Binding DataGridView using C# and VB.Net
Inside the Form_Load event handler, the DataGridView is populated with dummy data.
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Customer Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country", typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
this.dataGridView1.DataSource = dt;
this.dataGridView1.AllowUserToAddRows = false;
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() { New DataColumn("Customer Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Me.dataGridView1.DataSource = dt
Me.dataGridView1.AllowUserToAddRows = False
End Sub
Exporting HTML to Word document using C# and VB.Net
When the Export Button is clicked, the DataGridView is converted into HTML and saved into the Server Folder (Directory) named Files.
Exporting to Word
First, an instance of Application class of Interop.Word library is created and using Open method, the HTML file is read.
Then, using SaveAs method of _Document interface, which accepts the file name of Word document and the format of the file in which the HTML file will be exported i.e. Word document.
Finally, the HTML file is deleted where it is saved.
C#
private void OnExport(object sender, EventArgs e)
{
//Table start.
string html = "<meta charset='utf-8' />";
html += "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:arial'>";
//Adding HeaderRow.
html += "<tr>";
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
html += "<th style='background-color: #F7F7F7;border: 1px solid #ccc'>" + column.HeaderText + "</th>";
}
html += "</tr>";
//Adding DataRow.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
html += "<tr>";
foreach (DataGridViewCell cell in row.Cells)
{
html += "<td style='width:120px;border: 1px solid #ccc'>" + cell.Value.ToString() + "</td>";
}
html += "</tr>";
}
//Table end.
html += "</table>";
//Save the HTML string as HTML File.
string htmlFilePath = @"E:\Files\DataGridView.htm";
File.WriteAllText(htmlFilePath, html);
//Convert the HTML File to Word document.
_Application word = new Microsoft.Office.Interop.Word.Application();
_Document wordDoc = word.Documents.Open(FileName:htmlFilePath, ReadOnly:false);
wordDoc.SaveAs(FileName:@"E:\Files\DataGridView.doc", FileFormat: WdSaveFormat.wdFormatRTF);
wordDoc.Close();
word.Quit();
//Delete the HTML File.
File.Delete(htmlFilePath);
}
VB.Net
Private Sub OnExport(sender As Object, e As EventArgs) Handles btnExport.Click
'Table start.
Dim html As String = "<meta charset='utf-8' />"
html &= "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:arial'>"
'Adding HeaderRow.
html &= "<tr>"
For Each column As DataGridViewColumn In dataGridView1.Columns
html &= "<th style='background-color: #F7F7F7;border: 1px solid #ccc'>" & column.HeaderText & "</th>"
Next
html &= "</tr>"
'Adding DataRow.
For Each row As DataGridViewRow In dataGridView1.Rows
html &= "<tr>"
For Each cell As DataGridViewCell In row.Cells
html &= "<td style='width:120px;border: 1px solid #ccc'>" & cell.Value.ToString() & "</td>"
Next
html &= "</tr>"
Next
'Table end.
html &= "</table>"
'Save the HTML string as HTML File.
Dim htmlFilePath As String = "E:\Files\DataGridView.htm"
File.WriteAllText(htmlFilePath, html)
'Convert the HTML File to Word document.
Dim word As _Application = New Application
Dim wordDoc As _Document = word.Documents.Open(FileName:=htmlFilePath, ReadOnly:= False)
wordDoc.SaveAs(FileName:= "E:\Files\DataGridView.doc", FileFormat:= WdSaveFormat.wdFormatRTF)
wordDoc.Close()
word.Quit()
'Delete the HTML File.
File.Delete(htmlFilePath)
End Sub
Screenshots
Form
Exported HTML File
Exported Word File
Downloads