如何使用Open XML和C#4.0从Word文件中检索基于内容的章节和段落?

问题描述

我正在使用c#并打开xml sdk 2.0来访问Word文件

为此,我现在想根据给定的文本检索章节和段落。

如果该章包含我的文字,则检索包含该文字的段落...

FOR EXAMPLE: Given Word is: Chapter 1 - Events

检索包含单词"Chapter 1 - Events"的章节和段落

我想在Word文件搜索给定的单词。

如果找到任何匹配项,那么我想显示方法并根据此模式插入数据库

enter image description here

如果找不到匹配项,则无需获取段落。

我怎么办?

代码隐藏(现在Visual Studio调试的返回为空)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

public partial class Default : Page
{
    protected void Page_Load(object sender,EventArgs e)
    {
        if (!IsPostBack)
        {
            string file = @"C:\Users\file.docx";

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(file,true))
            {
                Body body = wordDoc.MainDocumentPart.Document.Body;

                string paras = "";

                foreach (Paragraph p in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(p => p.InnerText.Contains("Chapter [0-9]{1;} ")))
                {
                    paras += p.InnerText + "<br/>";
                }

                Response.Write(paras);
            }
        }
    }
}

file.docx

Chapter 1 - Events
 - alert or disservices
Lorem ipsum dolor sit amet,consectetur adipiscing elit
….
….
- significant activities
Phasellus dui nunc,rutrum vitae dictum eleifend,ullamcorper hendrerit sem
….
….

Chapter 2 – Safety
- near miss
Praesent venenatis convallis nunc,quis ultrices massa
….
….
- security checks
Phasellus mollis dapibus porta. Phasellus ac tristique dui
….
….

Chapter 3 – Training
- environment
Nunc hendrerit scelerisque mauris vel eleifend
….
….
- upkeep
Mauris id elit nec nisl laoreet posuere. Donec ac molestie sem
….
….

数据库

DROP TABLE IF EXISTS `chapters`;
CREATE TABLE `chapters` (
  `chapter` longtext CHaraCTER SET utf8 COLLATE utf8_general_ci,`subheading` longtext CHaraCTER SET utf8 COLLATE utf8_general_ci,`contents` longtext CHaraCTER SET utf8 COLLATE utf8_general_ci,`sID` int(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (`sID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...