Hi aginell4life,
Add the meta tag at the starting of the html variable.
Here is the updated sample.
Namespaces
C#
using System.IO;
using Microsoft.Office.Interop.Word;
using DataTable = System.Data.DataTable;
VB.Net
Imports System.IO
Imports Microsoft.Office.Interop.Word
Imports DataTable = System.Data.DataTable
Code
C#
private void Form1_Load(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("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");
dt.Rows.Add(5, "Àbàdá ", "Brazil");
this.dataGridView1.DataSource = dt;
this.dataGridView1.AllowUserToAddRows = false;
}
private void btnExport_Click(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: #B8DBFD;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 = @"D:\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: @"D:\DataGridView.doc", FileFormat: WdSaveFormat.wdFormatRTF);
((_Document)wordDoc).Close();
((_Application)word).Quit();
//Delete the HTML File.
File.Delete(htmlFilePath);
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("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")
dt.Rows.Add(5, "Àbàdá ", "Brazil")
Me.dataGridView1.DataSource = dt
Me.dataGridView1.AllowUserToAddRows = False
End Sub
Private Sub btnExport_Click(sender As System.Object, e As System.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: #B8DBFD;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 = "D:\DataGridView.htm"
File.WriteAllText(htmlFilePath, html)
'Convert the HTML File to Word document.
Dim word As _Application = New Microsoft.Office.Interop.Word.Application
Dim wordDoc As _Document = word.Documents.Open(FileName:=htmlFilePath, ReadOnly:=False)
wordDoc.SaveAs(FileName:="D:\DataGridView.doc", FileFormat:=WdSaveFormat.wdFormatRTF)
CType(wordDoc, _Document).Close()
CType(word, _Application).Quit()
'Delete the HTML File.
File.Delete(htmlFilePath)
End Sub
Screenshot
![](https://i.imgur.com/AEqqyjR.png)