C# 如何知道单词列表是项目符号列表还是数字列表?
我正在阅读一个word文档(将其转换为HTML),想知道每个段落的类型(至少我想这样做) 我的代码如下所示C# 如何知道单词列表是项目符号列表还是数字列表?,c#,ms-word,office-interop,C#,Ms Word,Office Interop,我正在阅读一个word文档(将其转换为HTML),想知道每个段落的类型(至少我想这样做) 我的代码如下所示 Application application = new Application(); var doc = application.Documents.Open("D:\\myDoc.docx"); for (int i = 0; i < doc.Paragraphs.Count; i++) { Console.WriteLine($"{doc.Paragraphs[i
Application application = new Application();
var doc = application.Documents.Open("D:\\myDoc.docx");
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
Console.WriteLine($"{doc.Paragraphs[i + 1].Range.ParagraphStyle.NameLocal}");
}
应用程序=新应用程序();
var doc=application.Documents.Open(“D:\\myDoc.docx”);
对于(int i=0;i
例如,输出标题1、正常和列表段落。所以我的问题是。我看不出列表段落是项目符号列表还是数字列表。问题,我如何知道列表的类型?使用
Range.ListFormat.ListType
,它可以具有以下值:
// Summary:
// Specifies a type of list.
public enum WdListType
{
// Summary:
// List with no bullets, numbering, or outlining.
wdListNoNumbering = 0,
//
// Summary:
// ListNum fields that can be used in the body of a paragraph.
wdListListNumOnly = 1,
//
// Summary:
// Bulleted list.
wdListBullet = 2,
//
// Summary:
// Simple numeric list.
wdListSimpleNumbering = 3,
//
// Summary:
// Outlined list.
wdListOutlineNumbering = 4,
//
// Summary:
// Mixed numeric list.
wdListMixedNumbering = 5,
//
// Summary:
// Picture bulleted list.
wdListPictureBullet = 6,
}
仅仅区分两个单词表可能是不够的。 我不知道“大纲列表”的确切含义,但数字列表和项目符号列表似乎都属于这一类 那么,你能做什么 选项1。您可以使用
Range.ListFormat.ListString
确定标记列表的文本。它可以是项目符号、数字、三角形或word文件中定义的任何内容。但这不是一个好主意,因为你永远不知道存储在那里的值是什么,所以你不能比较它
选项2。您可以使用枚举,尽管它有点复杂。我会尽力解释的。
有一个名为Range.ListFormat.ListTemplate.ListLevels
的属性,用于存储列表中所有可能级别的列表格式。常用列表的格式为1级,嵌套列表的格式分别为2到9级(似乎可以在MS Word中为嵌套列表定义9种不同的格式)。因此,您需要的是获取Range.ListFormat.ListTemplate.ListLevels
属性的第一项,并检查其NumberStyle
属性(请参见上面的链接)。但是,由于ListLevels
仅支持IEnumerable
接口,因此无法获取特定元素。您可以使用以下内容:
private static Word.WdListNumberStyle GetListType(Word.Range sentence)
{
foreach (Word.ListLevel lvl in sentence.ListFormat.ListTemplate.ListLevels)
{
return lvl.NumberStyle;
}
}
或者更具体地说
private static Word.WdListNumberStyle GetListType(Word.Range sentence, byte level)
{
foreach (Word.ListLevel lvl in sentence.ListFormat.ListTemplate.ListLevels)
{
if (level == 1)
return lvl.NumberStyle;
level--;
}
}
我不知道这对问题的作者是否有帮助,但因为我有一个问题,在寻找解决方案的过程中,我来到这里,但没有找到解决方案,所以我决定将我发现的东西张贴出来。我不知道为什么它必须如此复杂,也不知道为什么不能直接从
列表模板
获取描述列表样式的值,谢谢,就是这样!