使用c#Interop在Word中的子弹点

时间:2012-07-16 16:12:27

标签: c# list ms-word interop

我有以下代码,它应该将一个项目符号列表添加到我自动生成的word文档中。从其他答案我相信代码是正确的,但结果根本没有产生任何要点,它似乎也没有应用缩进。 有任何想法吗?

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();

4 个答案:

答案 0 :(得分:4)

这是因为您在范围之后的范围中添加了多个段落(似乎设置Text属性等同于InsertAfter)。您希望在范围之前插入,以便应用您设置的格式。

    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);
    }

请注意,我们在除最后一项之外的所有项目中添加了段落结束标记。如果你在最后添加一个子弹,你将得到一个空的子弹。

答案 1 :(得分:2)

这是基于Tergiver的回答。不同之处在于它在最初创建的段落之后以正确的顺序插入列表项。对于您自己的使用,使起始范围等于您要在之后插入列表的项目。

Paragraph assets = doc.Content.Paragraphs.Add();
rng = assets.Range;
rng.InsertAfter("\n");
start = rng.End;
end = rng.End;
rng = _oDoc.Range(ref start, ref end);
object listType = 0;
rng.ListFormat.ApplyBulletDefault(ref listType);
string[] bulletItems = new string[] { "One", "Two", "Three" };
for (int i = 0; i < bulletItems.Length; i++)
{
  string bulletItem = bulletItems[i];
  if (i < RowCount - 1)
    bulletItem = bulletItem + "\n";
  rng.InsertAfter(bulletItem);
}

请注意我真的不明白我在这里使用的是什么。这个解决方案经过大量的试验和错误后得出。我怀疑它可能与我重复使用相同范围的事实有关,而Tergiver的解决方案是每次访问范围时都会获取新范围。我特别不明白以下几行:

rng.InsertAfter("\n");
start = rng.End;
end = rng.End;
rng = _oDoc.Range(ref start, ref end);

通常,对上述代码和列表的任何更改都会与前一个元素混合在一起。如果有人能够解释为什么会有效,我将不胜感激。

答案 2 :(得分:0)

如果您想要 list-sublist 关系,可以尝试以下代码块:

static void Main(string[] args)
    {
        try
        {
            Application app = new Application();
            Document doc = app.Documents.Add();
            Range range = doc.Range(0, 0);
            range.ListFormat.ApplyNumberDefault();
            range.Text = "Birinci";
            range.InsertParagraphAfter();
            ListTemplate listTemplate = range.ListFormat.ListTemplate;

            //range.InsertAfter("Birinci");
            //range.InsertParagraphAfter();

            //range.InsertAfter("İkinci");
            //range.InsertParagraphAfter();

            //range.InsertAfter("Üçüncü");
            //range.InsertParagraphAfter();

            Range subRange = doc.Range(range.StoryLength - 1);
            subRange.ListFormat.ApplyBulletDefault();
            subRange.ListFormat.ListIndent();
            subRange.Text = "Alt Birinci";
            subRange.InsertParagraphAfter();
            ListTemplate sublistTemplate = subRange.ListFormat.ListTemplate;

            Range subRange2 = doc.Range(subRange.StoryLength - 1);
            subRange2.ListFormat.ApplyListTemplate(sublistTemplate);
            subRange2.ListFormat.ListIndent();
            subRange2.Text = "Alt İkinci";
            subRange2.InsertParagraphAfter();

            Range range2 = doc.Range(range.StoryLength - 1);
            range2.ListFormat.ApplyListTemplateWithLevel(listTemplate,true);
            WdContinue isContinue =  range2.ListFormat.CanContinuePreviousList(listTemplate);
            range2.Text = "İkinci";
            range2.InsertParagraphAfter();

            Range range3 = doc.Range(range2.StoryLength - 1);
            range3.ListFormat.ApplyListTemplate(listTemplate);
            range3.Text = "Üçüncü";
            range3.InsertParagraphAfter();



            string path = Environment.CurrentDirectory;
            int totalExistDocx = Directory.GetFiles(path, "test*.docx").Count();
            path = Path.Combine(path, string.Format("test{0}.docx", totalExistDocx + 1));

            app.ActiveDocument.SaveAs2(path, WdSaveFormat.wdFormatXMLDocument);
            doc.Close();

            Process.Start(path);
        }
        catch (Exception exception)
        {

            throw;
        }

    }

注意这一点:如果你不知道输入长度,你不能像这样定义范围结束值:

static void Main(string[] args)
    {
        try
        {
            Application app = new Application();
            Document doc = app.Documents.Add();
            Range range = doc.Range(0, 0);
            range.ListFormat.ApplyNumberDefault();
            range.Text = "Birinci";
            range.InsertParagraphAfter();
            ListTemplate listTemplate = range.ListFormat.ListTemplate;

            //range.InsertAfter("Birinci");
            //range.InsertParagraphAfter();

            //range.InsertAfter("İkinci");
            //range.InsertParagraphAfter();

            //range.InsertAfter("Üçüncü");
            //range.InsertParagraphAfter();

            Range subRange = doc.Range(range.StoryLength - 1, range.StoryLength - 1);
            subRange.ListFormat.ApplyBulletDefault();
            subRange.ListFormat.ListIndent();
            subRange.Text = "Alt Birinci";
            subRange.InsertParagraphAfter();
            ListTemplate sublistTemplate = subRange.ListFormat.ListTemplate;

            Range subRange2 = doc.Range(subRange.StoryLength - 1, range.StoryLength - 1);
            subRange2.ListFormat.ApplyListTemplate(sublistTemplate);
            subRange2.ListFormat.ListIndent();
            subRange2.Text = "Alt İkinci";
            subRange2.InsertParagraphAfter();

            Range range2 = doc.Range(range.StoryLength - 1, range.StoryLength - 1);
            range2.ListFormat.ApplyListTemplateWithLevel(listTemplate,true);
            WdContinue isContinue =  range2.ListFormat.CanContinuePreviousList(listTemplate);
            range2.Text = "İkinci";
            range2.InsertParagraphAfter();

            Range range3 = doc.Range(range2.StoryLength - 1, range.StoryLength - 1);
            range3.ListFormat.ApplyListTemplate(listTemplate);
            range3.Text = "Üçüncü";
            range3.InsertParagraphAfter();



            string path = Environment.CurrentDirectory;
            int totalExistDocx = Directory.GetFiles(path, "test*.docx").Count();
            path = Path.Combine(path, string.Format("test{0}.docx", totalExistDocx + 1));

            app.ActiveDocument.SaveAs2(path, WdSaveFormat.wdFormatXMLDocument);
            doc.Close();

            Process.Start(path);
        }
        catch (Exception exception)
        {

            throw;
        }

    }

答案 3 :(得分:0)

您只需要跟踪列表的开始和结束位置,然后应用列表格式即可。

        Application wordApp = new Application() {
            Visible = true
        };

        Document doc = wordApp.Documents.Add();

        Range range = doc.Content;
        range.Text = "Hello world!";

        range.InsertParagraphAfter();
        range = doc.Paragraphs.Last.Range;

        // start of list
        int startOfList = range.Start;

        // each \n character adds a new paragraph...
        range.Text = "Item 1\nItem 2\nItem 3";

        // ...or insert a new paragraph...
        range.InsertParagraphAfter();
        range = doc.Paragraphs.Last.Range;
        range.Text = "Item 4\nItem 5";

        // end of list
        int endOfList = range.End;

        // insert the next paragraph before applying the format, otherwise
        // the format will be copied to the suceeding paragraphs.
        range.InsertParagraphAfter();

        // apply list format
        Range listRange = doc.Range(startOfList, endOfList);
        listRange.ListFormat.ApplyBulletDefault();

        range = doc.Paragraphs.Last.Range;
        range.Text = "Bye for now!";
        range.InsertParagraphAfter();