本文将介绍通过使用spire.doc for .net根据段落标题样式名称来读取word中相应段落的方法。
c#
//创建document对象,并加载测试文档
document doc = new document();
doc.loadfromfile("sample.doc");
//实例化stringbuilder对象
stringbuilder sb = new stringbuilder();
//遍历文档
foreach (section section in doc.sections)
{
foreach (paragraph paragraph in section.paragraphs)
{
//判断段落标题名称
if (paragraph.stylename == "标题1")
{
sb.appendline(paragraph.text);
}
if (paragraph.stylename == "标题2")
{
sb.appendline(paragraph.text);
}
if (paragraph.stylename == "标题3")
{
sb.appendline(paragraph.text);
}
if (paragraph.stylename == "标题4")
{
sb.appendline(paragraph.text);
}
}
}
//将符合标题名称的段落写入.txt文档
file.writealltext("extract.txt", sb.tostring());
vb.net
'创建document对象,并加载测试文档
dim doc as document = new document
doc.loadfromfile("sample.doc")
'实例化stringbuilder对象
dim sb as stringbuilder = new stringbuilder
'遍历文档
for each section as section in doc.sections
for each paragraph as paragraph in section.paragraphs
'判断段落标题名称
if (paragraph.stylename = "标题1") then
sb.appendline(paragraph.text)
end if
if (paragraph.stylename = "标题2") then
sb.appendline(paragraph.text)
end if
if (paragraph.stylename = "标题3") then
sb.appendline(paragraph.text)
end if
if (paragraph.stylename = "标题4") then
sb.appendline(paragraph.text)
end if
next
next
'将符合标题名称的段落写入.txt文档
file.writealltext("extract.txt", sb.tostring)
读取结果: