Hey smile,
Please refer below sample.
HTML
<asp:Button ID="btnImportWord" runat="server" Text="Import MS Word" Class="btn btn-info"
OnClick="btnImportWord_Click" /><br />
<br />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="AdmissionNo" HeaderText="AdmissionNo" />
<asp:BoundField DataField="SubjectID" HeaderText="SubjectID" />
<asp:BoundField DataField="SetExamID" HeaderText="SetExamID" />
<asp:BoundField DataField="ObtainMarks" HeaderText="ObtainMarks" />
</Columns>
</asp:GridView>
Namespaces
C#
using Microsoft.Office.Interop.Word;
VB.Net
Imports Microsoft.Office.Interop.Word
Code
C#
protected void btnImportWord_Click(object sender, EventArgs e)
{
Application word = new Application();
Document doc = new Document();
string filePath = @"C:\Users\anand\Desktop\Test.docx";
object missing = System.Type.Missing;
object fileName = filePath;
Microsoft.Office.Interop.Word.Table table = null;
doc = word.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
for (int i = 0; i < doc.Tables.Count; i++)
{
table = doc.Tables[i + 1];
}
GridView1.DataSource = GetDataTableFromWordTable(table);
doc.Close(ref missing, ref missing, ref missing);
((_Application)word).Quit();
GridView1.DataBind();
}
private System.Data.DataTable GetDataTableFromWordTable(Microsoft.Office.Interop.Word.Table table)
{
System.Data.DataTable dt = new System.Data.DataTable();
foreach (Row row in table.Rows)
{
if (row.Index > 1)
{
dt.Rows.Add();
}
foreach (Column column in table.Columns)
{
Cell cell = table.Cell(row.Index, column.Index);
string cellValue = cell.Range.Text;
if (row.Index == 1)
{
dt.Columns.Add(cellValue.ToString().Replace("\r\a", ""));
}
else
{
dt.Rows[row.Index - 2][column.Index - 1] = cellValue.ToString().Replace("\r\a", "");
}
}
}
return dt;
}
VB.Net
Protected Sub btnImportWord_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim word As Application = New Application()
Dim doc As Document = New Document()
Dim filePath As String = "C:\Users\anand\Desktop\Test.docx"
Dim missing As Object = System.Type.Missing
Dim fileName As Object = filePath
Dim table As Microsoft.Office.Interop.Word.Table = Nothing
doc = word.Documents.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
For i As Integer = 0 To doc.Tables.Count - 1
table = doc.Tables(i + 1)
Next
GridView1.DataSource = GetDataTableFromWordTable(table)
doc.Close(missing, missing, missing)
CType(word, _Application).Quit()
GridView1.DataBind()
End Sub
Private Function GetDataTableFromWordTable(ByVal table As Microsoft.Office.Interop.Word.Table) As System.Data.DataTable
Dim dt As System.Data.DataTable = New System.Data.DataTable()
For Each row As Row In table.Rows
If row.Index > 1 Then
dt.Rows.Add()
End If
For Each column As Column In table.Columns
Dim cell As Cell = table.Cell(row.Index, column.Index)
Dim cellValue As String = cell.Range.Text
If row.Index = 1 Then
dt.Columns.Add(cellValue.ToString().Replace(vbCr & ChrW(7), ""))
Else
dt.Rows(row.Index - 2)(column.Index - 1) = cellValue.ToString().Replace(vbCr & ChrW(7), "")
End If
Next
Next
Return dt
End Function
Screenshot