使用 c# Interop 在 Word 中的项目符号点 [英] Bullet points in Word with c# Interop
问题描述
我有以下代码,它应该将项目符号列表添加到我自动生成的 Word 文档中.从其他答案我相信代码是正确的,但结果根本没有产生任何要点,它似乎也没有应用缩进.有任何想法吗?
I have the following code which is supposed to add a bulleted list to a word document that I'm generating automatically. From other answers I believe the code is correct, but the result doesn't produce any bullet points at all, it doesn't seem to apply the indent either. Any Ideas?
Microsoft.Office.Interop.Word.Paragraph assets;
assets = doc.Content.Paragraphs.Add(Type.Missing);
// Some code to generate the text
foreach (String asset in assetsList)
{
assetText = assetText + asset + "
";
}
assets.Range.ListFormat.ApplyBulletDefault(Type.Missing);
// Add it to the document
assets.Range.ParagraphFormat.LeftIndent = -1;
assets.Range.Text = assetText;
assets.Range.InsertParagraphAfter();
推荐答案
发生这种情况是因为您在范围之后向范围添加了多个段落(似乎设置 Text 属性等效于 InsertAfter).您希望在范围之前插入,以便应用您设置的格式.
This happens because you're adding multiple paragraphs to the range after the range (it seems that setting the Text property is equivalent to InsertAfter). You want to InsertBefore the range so that the formatting you set gets applied.
Paragraph assets = doc.Content.Paragraphs.Add();
assets.Range.ListFormat.ApplyBulletDefault();
string[] bulletItems = new string[] { "One", "Two", "Three" };
for (int i = 0; i < bulletItems.Length; i++)
{
string bulletItem = bulletItems[i];
if (i < bulletItems.Length - 1)
bulletItem = bulletItem + "
";
assets.Range.InsertBefore(bulletItem);
}
请注意,我们为除最后一项之外的所有项目添加了段落结束标记.如果在最后一个添加一个,您将得到一个空项目符号.
Notice that we add an End of Paragraph mark to all items except the last one. You will get an empty bullet if you add one to the last.
这篇关于使用 c# Interop 在 Word 中的项目符号点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!