c# - Microsoft Word 中的编号列表

标签 c# ms-word

我正在使用 Interop.Microsoft.Office.Interop.Word.dll 在 C# 中动态构建 Word 文档。

有人有创建编号列表的代码示例吗?

最佳答案

试试这个...它假定您有对 Word10 的引用(您可以使用其他版本,但必须更改常量)。不要忘记 using Microsoft.Office.Interop.Word;

// setup
object missing = System.Reflection.Missing.Value;
ApplicationClass app = new ApplicationClass();
Document doc = app.Documents.Add(ref missing, ref missing, 
    ref missing, ref missing);
app.Visible = true;

// whatever is selected will be turned into a numbered list.
object n = 1;
ListTemplate template = 
    app.ListGalleries[WdListGalleryType.wdNumberGallery].ListTemplates.get_Item(ref n);
ListLevel level = template.ListLevels[1];
level.NumberFormat = "%1.";
level.TrailingCharacter = WdTrailingCharacter.wdTrailingTab;
level.NumberStyle = WdListNumberStyle.wdListNumberStyleArabic;
level.NumberPosition = app.InchesToPoints(0.25f);
level.Alignment = WdListLevelAlignment.wdListLevelAlignLeft;
level.TextPosition = app.InchesToPoints(0.5f);
level.TabPosition = (float)WdConstants.wdUndefined;
level.ResetOnHigher = 0;
level.StartAt = 1;

level.Font.Bold = (int)WdConstants.wdUndefined;
level.Font.Italic = (int)WdConstants.wdUndefined;
level.Font.StrikeThrough = (int)WdConstants.wdUndefined;
level.Font.Subscript = (int)WdConstants.wdUndefined;
level.Font.Superscript = (int)WdConstants.wdUndefined;
level.Font.Shadow = (int)WdConstants.wdUndefined;
level.Font.Outline = (int)WdConstants.wdUndefined;
level.Font.Emboss = (int)WdConstants.wdUndefined;
level.Font.Engrave = (int)WdConstants.wdUndefined;
level.Font.AllCaps = (int)WdConstants.wdUndefined;
level.Font.Hidden = (int)WdConstants.wdUndefined;
level.Font.Underline = WdUnderline.wdUnderlineNone;
level.Font.Color = WdColor.wdColorAutomatic;
level.Font.Size = (int)WdConstants.wdUndefined;
level.Font.Animation = WdAnimation.wdAnimationNone;
level.Font.DoubleStrikeThrough = (int)WdConstants.wdUndefined;

level.LinkedStyle = "";

template.Name = "";
object bContinuePrevList = false;
object applyTo = WdListApplyTo.wdListApplyToWholeList;
object defBehavior = WdDefaultListBehavior.wdWord10ListBehavior;

app.Selection.Range.ListFormat.ApplyListTemplateWithLevel(
    template, ref bContinuePrevList, 
    ref applyTo, ref defBehavior, ref missing);

编辑:格式化。

关于c# - Microsoft Word 中的编号列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1939995/

相关文章:

c# - 从 bak 文件中检索数据库名称

c# - 多对多关系基本示例 (MVC3)

c# - .NET MVC 4 - foreach 循环中的按钮操作

c# - 可以内联静态类扩展吗?

c# - 泛型协变转换或转换为实型

C# 和 Word - 替换为格式

ms-word - 在 Word 文档中插入带有插入文本的换行符

java - 我如何使用java获取Word文档中段落的一部分的字符串的页码

vba - 带有 VBA 的 Word 中的单个源文档

python - .doc 到 pdf 使用 python