Insert on database contents of word file using OpenXML and C#

Home Forums Open-Xml-Sdk Insert on database contents of word file using OpenXML and C#

This topic contains 2 replies, has 3 voices, and was last updated by  Anonymous 4 years, 1 month ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #9686

    cms_9651
    Participant

    Hello, I need your help.

    I want to read a .docx file line by line and insert chapters and paragraphs of the chapter in database table.

    I have tried using Microsoft.Office.Interop.Word to read the document, without success because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    Now I read on google that the suitable tool for this is OpenXML.

    My file.docx is divided for chapters and paragraphs of the chapter

    The structure of file.docx

    Chapter 1 - Events
    -alert or disservices
    -significant activities
    
    Chapter 2 – Safety
    -near miss
    -security checks
    
    Chapter 3 – Training
    -environment
    -upkeep

    I need insert on database table according with this schema

    How to do resolve this?
    Thanks in advance for any help or suggestion
    Chevy

    • This topic was modified 4 years, 2 months ago by  cms_9651.
    #9709

    sanjaysingh
    Participant

    Contact RKSA Packers & Movers Company for your transfer, as we provide the best and cheapest packing and moving service. We are verified and professional packers and movers companies.

    #9762

    Anonymous

    Looking at your document and your code, I see two places that could be the source of your problem:

    First: the xml layout for your SecondTemplate.docx containing Bookmark1 is like so:

    <Paragraph>
    <Bookmarkstart name=bookmark1/>
    <Run>
    <Text “Item 1”>
    </Run>
    </Paragraph>
    <Paragraph>
    <Run>
    <Text “Item 2”>
    </Run>
    </Paragraph>
    <Paragraph>
    <Run>
    <Text “Item 3”>
    </Run>
    </Paragraph>
    <Paragraph>
    <Run>
    <Text “Item 4”>
    </Run>
    <Bookmarkend/>
    </Paragraph>
    and your code here:

    if(bookmarkStart.Name == bookmarkKey)
    {
    foreach(Run run in bookmarkStart.Parent.Descendants<Run>())
    {
    returnVal += run.Descendants<Text>().FirstOrDefault().Text + “<br/>”;
    }
    }
    when the bookmarkstart.Parent call runs, it matches on the Paragraph that is directly above the bookmark :

    <Paragraph>
    <Bookmarkstart name=bookmark1/>
    <Run>
    <Text “Item 1”>
    </Run>
    </Paragraph>
    so when the rest of the loop executes, you only get the “Item 1” pulled into your merge process. You need to re-work your logic to correctly match the Text in the Run for all four paragraphs between the BookmarkStart and BookmarkEnd.

    Second: Another issue that often trips people up in OpenXml is when you are trying to match the Run in the Descendants call here:

    bookmarkStart.Parent.Descendants<Run>
    If you are referring to the DocumentFormat.OpenXml.Drawing.Run , not the correct ‘DocumentFormat.OpenXml.Wordprocessing.Run’, this can prevent a match – so mouse over that Run in Visual Studio and ensure you are matching the correct Run. Adjust your using statements to get the correct one. A Using statement like

    using Run = DocumentFormat.OpenXml.Wordprocessing.Run;
    is often used depending on the rest of your code in that file. Hope these clues help you.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.