创建Word文档并编写格式化的文本

时间:2018-08-21 19:22:14

标签: c# text ms-word

using Word = Microsoft.Office.Interop.Word;

public static void CreateWord()
{
    Word.Application objWord = new Word.Application();
    Word.Document objDoc = objWord.Documents.Add();

    Word.Paragraph objPara;
    objPara = objDoc.Paragraphs.Add();
    objPara.Range.Text = "Bold text aligned center";
    objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    objPara.Range.Bold = 1;
    objPara.Range.InsertParagraphAfter();

    objPara = objDoc.Paragraphs.Add();
    objPara.Range.Text = "Regular text aligned left";
    objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
    objPara.Range.Bold = 0;
    objPara.Range.InsertParagraphAfter();

    objPara = objDoc.Paragraphs.Add();
    objPara.Range.Text = "Regular text aligned center\nwith something on the next line";
    objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    objPara.Range.Bold = 0;
    objPara.Range.InsertParagraphAfter();

    objPara = objDoc.Paragraphs.Add();
    objPara.Range.Text = "Regular text aligned left, with some Bold text here\nand some regular text on next line";
    objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
    objPara.Range.Bold = 0;
    objPara.Range.InsertParagraphAfter();

    objWord.Visible = true;
    objWord.WindowState = Word.WdWindowState.wdWindowStateNormal;
}

上面的代码创建了一个Word文档并向其中写入文本,但是看起来像这样:Word screenshot

我假设Microsoft.Office.Interop.Word中的文本不应该那样做,或者应该以其他方式添加文本,但是我找不到任何有关如何执行此操作的好信息。 / p>

所以,我的问题是-如何将c#中的文本添加到具有正确属性的新创建的word文档中,因此结果如下:Desired result

P.S。使用.Interop.Word创建表的技巧也将不胜感激。

1 个答案:

答案 0 :(得分:1)

使用Word文档中的内容的关键是了解如何使用Range对象。实现这种目标的方法不止一种。我更喜欢以下代码段:折叠每个步骤的Range(想像一下,就像按右箭头键将选择“折叠”到一个点),而不是混合AddInsertAfter和类似的方法。

对于格式的每次更改,Range必须被“折叠”。因此,如果您在一行中间加粗,那是一个单独的步骤。如果许多段落应采用相同的格式,则可以将其合并。

Environment.NewLine\n可以互换使用。

通常,分配文本内容,然后格式化Range

    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    doc.Paragraphs.Add();
    Word.Range objRange = doc.Content;
    objRange.Collapse(ref oCollapseEnd);

    objRange.Text = "Bold text aligned center" + Environment.NewLine;
    objRange.Bold = 1;
    objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    objRange.Collapse(ref oCollapseEnd);

    objRange.Text = "Regular text aligned left" + Environment.NewLine;
    objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
    objRange.Bold = 0;
    objRange.Collapse(ref oCollapseEnd);

    objRange.Text = "Regular text aligned center\nwith something on the next line" + Environment.NewLine;
    objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    objRange.Bold = 0;
    objRange.Collapse(ref oCollapseEnd);

    objRange.Text = "Regular text aligned left, with some ";
    objRange.Collapse(ref oCollapseEnd);
    objRange.Text = "Bold text here";
    objRange.Bold = 1;
    objRange.Collapse(ref oCollapseEnd);
    objRange.Text = "\nand some regular text on next line" + Environment.NewLine;
    objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
    objRange.Bold = 0;