'Is any method to extract tables from docx resumes with accuracy and store it into dataset

using System;

using System.Data;                              // FOR "DataTable". 
using Word = Microsoft.Office.Interop.Word;     // For Ms-Word application.

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void PopulateGrid(object sender, EventArgs e)
    {
        // ' Check if any file is selected.
        if ((FileUpload.HasFile))
        {
            if (!Convert.IsDBNull(FileUpload.PostedFile) &
                    FileUpload.PostedFile.ContentLength > 0)
            {
                // Save the file in the root folder.
                FileUpload.SaveAs(Server.MapPath(".") + "\\" + FileUpload.FileName);

                // Create Ms-Word objects.
                Word.Application objWord;
                objWord = new Word.Application();
                Word.Document objDoc;

                try
                {
                    // Open the Word file in "ReadOnly" mode.
                    objDoc = objWord.Documents.Open(Server.MapPath(".") + @"\" + FileUpload.FileName, ReadOnly: true);

                    if (objDoc.Tables.Count != 0)       // Check if Word file has any tables.
                    {
                        if (objDoc.Tables[1].Columns.Count > 0)     // Check if the table has columns.
                        {
                            int iTotalCols;   // Get total columns in the table.
                            iTotalCols = objDoc.Tables[1].Columns.Count;

                            int iTotalRows;   // Get total rows in the table.
                            iTotalRows = objDoc.Tables[1].Rows.Count;

                            int iRows, iCols;

                            DataTable dt = new DataTable();

                            // Get the table columns and add it to the DataTable as "headers".
                            for (iCols = 1; iCols <= iTotalCols; iCols++)
                                dt.Columns.Add(objDoc.Tables[1].Cell(1, iCols).Range.Text);

                            object[] myObj = new object[iTotalCols - 1 + 1];

                            // Now extract the table data.
                            for (iRows = 2; iRows <= iTotalRows; iRows++)
                            {
                                var row = dt.NewRow();      // Create and add new row to the DataTable.

                                for (iCols = 1; iCols <= iTotalCols; iCols++)
                                    myObj[iCols - 1] = objDoc.Tables[1].Cell(iRows, iCols).Range.Text;

                                row.ItemArray = myObj;
                                dt.Rows.Add(row);           // Add a new row to the DataTable.
                            }

                            GridView1.DataSource = dt;      // Finally, populate the grid with the data.
                            GridView1.DataBind();

                            lblConfirm.Text = "Data successfully imported to GridView.";
                            lblConfirm.Attributes.Add("style", "color:green");
                        }
                    }

                }
                catch (Exception ex)
                {
                    // Show a message (if any) on the web page.
                    lblConfirm.Text = ex.Message;
                    lblConfirm.Attributes.Add("style", "color:red");
                }
                finally
                {
                    // Clean up.
                    objWord.Quit(); objWord = null;
                    objDoc = null;
                }
            }
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source