Home > Back-end >  How do I show Microsoft Word .doc file with Data Gridview in asp.net
How do I show Microsoft Word .doc file with Data Gridview in asp.net

Time:02-28

I need to get .docx file from folder in my project and show it to user by using gridview in asp.net window form.

CodePudding user response:

Ok, the hard part is to get the table out of the word document.

You don’t mention or share how you doing this.

And you noted a “.doc” file vs that of a “.docx”. This is a HUGE issue, since .doc files are binary formatted word documents, whereas the .docx is an open (xml) based format in which the text and markup inside of the word document is actually xml and markup as plain text. In fact a .docx file can be re-named as a zip file, and you can open up it (try a re-name of a docx file).

However, this does assume that your web server is going to have word installed. This type of scenario is NOT really supported, since word is (mostly) un-managed code, and worse it not considered thread safe. As a result, you cannot really “automate” word this way with a high degree of reliability.

The next issue of course is that most if not all web servers are running as x64 bits, and often we find that word office installs are x32 bits. You can’t as noted mix up the bit size when using what we call non .net code (in this case the word applcation). So either you run the web site as x32 bits (not very common anymore), or you REALLY make sure you have word x64 bits installed).

So, while this is not a lot of code, there is a SIGNIFICANT number of moving parts here, and as a result such code under any kind of user or heavy load will not result in a reliability or robust setup. It is possible you are using some xml and 3rd party word document library, but using office inter-op is not recommended for web site.

Ok, so now we have all the got-ya issues here?

So, let’s do this with office inter-op features of .net. Again: This type of setup is not thread safe, and not recommended for web sites. In fact, few (in fact none) of the web hosting available these days will have word office installed. Perhaps you running your own server, and you can and are free to install office on that server, but do keep this REALLY big challenge and issue in mind (the very slim chance that you can assume office and word will be installed on the web server, AND ALSO that using office inter-op is not thread safe (which means this will not scale well at all with a lot of users on the site).

Ok, so, let’s find a word document with a table, and open up it?

Ok, I have this one:

enter image description here

So, what will the .net code look like to read that table and say display it in a grid view?

Well, as noted, we use .net officer inter-op for this:

So, we need a to set a reference to Microsoft.Office.Interop.Word

And our mark up on the page can look like this (a simple button to click to run our code, and a grid view)

   <div style="width:50%;padding:25px">
        <br />
        <asp:Button ID="Button1" runat="server" Text="Show word table" 
            CssClass="btn" OnClick="Button1_Click"/>
        <br />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" CssClass="table"></asp:GridView>
    </div>

and our code is thus this:

We open the word doc, get the table, and then create a .net data table.

Fill the table.

Then send that .net table to the grid view

    protected void Button1_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Word.Application MyWord = new Microsoft.Office.Interop.Word.Application();
        string sFile = @"c:\Test7\Fighter.docx";

        // create a .net table
        DataTable rstData = new DataTable();

        MyWord.Documents.Open(sFile);
        Microsoft.Office.Interop.Word.Document MyDoc = MyWord.ActiveDocument;
        // get first table
        Microsoft.Office.Interop.Word.Table MyTable = MyDoc.Tables[1];
        // assume first row is headings

        int iCol = 0;
        for (iCol = 1; iCol <= MyTable.Columns.Count; iCol  )
        {
            rstData.Columns.Add(CleanCellText(MyTable.Rows[1].Cells[iCol].Range.Text), typeof(string));
        }
        // now add all the rows of data to the .net table
        int iRows = 0;
        // word tables start at 1, .net tables start at 0 (zero based)
        for (iRows = 2; iRows <= MyTable.Rows.Count; iRows  )
        {
            DataRow OneRow = rstData.NewRow();
            for (iCol = 1; iCol <= MyTable.Columns.Count; iCol  )
            {
                OneRow[iCol - 1] = CleanCellText(MyTable.Rows[iRows].Cells[iCol].Range.Text);
            }
            rstData.Rows.Add(OneRow);  // add this row to .net table
        }
        MyDoc.Close();
        MyWord.Quit();

        // now bind the resulting table to the grid
        GridView1.DataSource = rstData;
        GridView1.DataBind();
    }

    string CleanCellText(string s)
    {
        // all word cells have a training cr and a char(7)
        string MyClean =  ((char)13).ToString()   ((char)7).ToString();
        return s.Replace(MyClean,"");
    }

running above, we get this:

enter image description here

Note the CleanCellText() little function. It turns out that all word tables text is followed by a carriage return, and then a char(7). So, you can remove them by shortening the cell text by 2 chars, or do a replace as per that helper function.

  • Related