问题描述
我正在使用c#并打开xml sdk 2.0来访问Word文件。
为此,我现在想根据给定的文本检索章节和段落。
FOR EXAMPLE: Given Word is: Chapter 1 - Events
检索包含单词"Chapter 1 - Events"
的章节和段落
如果找到任何匹配项,那么我想显示该方法并根据此模式插入数据库
如果找不到匹配项,则无需获取段落。
我怎么办?
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 (将#修改为@)