locked
Read word document that contains a table in c# RRS feed

  • Question

  •  I have a word document that contains a table.
     I need to be able to read this data into my C# ASP.NET application. 


     Does anyone how know to successfully read an word document ?

    Many thanks.


    shobha
    • Moved by VidhyaSagar Tuesday, December 1, 2009 12:05 PM Moving thread to correct Category. OP Wrongly posted in SQL Server Section (From:Database Mirroring)
    Tuesday, December 1, 2009 6:25 AM

Answers

  • Hello Shobha,

    As far as I know, you need to add a reference to Microsoft Word 12.0/11.0 Object Library on COM tab. If this library is not listed on COM tab, office 2007/2003 PIA need to be installed on your computer. You could download Office PIA from Microsoft Download Center. Here are the links: Office 2007 PIA and Office 2003 PIA. And then use code like this,

                Word.Application wordApp = new Word.Application();
                object
    filename=@"C:\Temp\11.docx";
                object missing=Type.Missing;
                Word.Document doc = wordApp.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);
                Word.Table table = doc.Tables[0]; //define this index depending on the number of table which you want to get

                doc.Close(ref missing, ref missing, ref missing);
                Marshal.ReleaseComObject(wordApp);

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Geert van Horrik Monday, December 7, 2009 12:05 PM
    • Marked as answer by Bessie Zhao Thursday, December 10, 2009 9:44 AM
    Wednesday, December 2, 2009 9:32 AM

All replies

  • Hi Shubha !

    This is not the right forum to post the question . This is all abt sql sever database mirroring .

    ---------------
    Thanks , Suhas v
    Tuesday, December 1, 2009 7:45 AM
  • Hello Shobha,

    As far as I know, you need to add a reference to Microsoft Word 12.0/11.0 Object Library on COM tab. If this library is not listed on COM tab, office 2007/2003 PIA need to be installed on your computer. You could download Office PIA from Microsoft Download Center. Here are the links: Office 2007 PIA and Office 2003 PIA. And then use code like this,

                Word.Application wordApp = new Word.Application();
                object
    filename=@"C:\Temp\11.docx";
                object missing=Type.Missing;
                Word.Document doc = wordApp.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);
                Word.Table table = doc.Tables[0]; //define this index depending on the number of table which you want to get

                doc.Close(ref missing, ref missing, ref missing);
                Marshal.ReleaseComObject(wordApp);

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Geert van Horrik Monday, December 7, 2009 12:05 PM
    • Marked as answer by Bessie Zhao Thursday, December 10, 2009 9:44 AM
    Wednesday, December 2, 2009 9:32 AM
  • Hi, here is simple code snippet you can try:

    // Load Word file.
    var document = DocumentModel.Load(Server.MapPath("Document.docx"));
    // Get first Table element.
    var table = document.GetChildElements(true, ElementType.Table).Cast<Table>().First();
    
    // Read Table's data.
    foreach(var row in table.Rows)
        foreach(var cell in row.Cells)
            // Do something with cell's content ...

    I used this C# Word component to avoid issues of using an interop on server side (see here).

    But also beside reading and writing it will enable you to export Word document in ASP.NET to the client browser with ease.

    Tuesday, December 24, 2013 8:15 AM
  • Hellow,

    Please let me know is there a way to find out a text inside a table cell in second column and 

    based on the search criteria, replace the text in third column cell of the same row.

    regards

    SCOMDev


    SCOMDev

    Monday, October 27, 2014 3:33 AM
  • Only single correction, index in word starts from 1 and not 0.

    Thanks,

    Wednesday, August 14, 2019 2:00 PM