本文介绍了在Word中的要点用C#互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的代码,被认为项目符号列表添加到我自动生成的Word文档。从其他的答案,我相信代码是正确的,但结果不会产生任何的要点可言,它似乎并不适用于任何缩进。
任何想法?

  Microsoft.Office.Interop.Word.Paragraph资产; 
资产= doc.Content.Paragraphs.Add(Type.Missing);

//一些代码来生成文本

的foreach(在assetsList字符串资产)
{
assetText = assetText +资产+\\\

}

assets.Range.ListFormat.ApplyBulletDefault(Type.Missing);

//将它添加到文档
assets.Range.ParagraphFormat.LeftIndent = -1;
assets.Range.Text = assetText;
assets.Range.InsertParagraphAfter();


解决方案

这是因为您要添加多个段落的范围的范围后(似乎设置Text属性等同于InsertAfter)。你想使你设置的格式被应用到的insertBefore范围。

 段落资产= doc.Content.Paragraphs.Add( ); 

assets.Range.ListFormat.ApplyBulletDefault();
的String [] = bulletItems新的字符串[] {一,二,三};

的for(int i = 0; I< bulletItems.Length;我++)
{
串bulletItem = bulletItems [I]
如果(I< bulletItems.Length - 1)
bulletItem = bulletItem +\\\

assets.Range.InsertBefore(bulletItem);
}

请注意,我们段落标记的末尾添加到所有项目除了最后一个。如果你添加一个到最后,你会得到一个空的子弹。


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 + "\n";
}

assets.Range.ListFormat.ApplyBulletDefault(Type.Missing);

// Add it to the document
assets.Range.ParagraphFormat.LeftIndent = -1;
assets.Range.Text = assetText;
assets.Range.InsertParagraphAfter();
解决方案

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 + "\n";
        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.

这篇关于在Word中的要点用C#互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:34