向Open Office XML添加间距和空行

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

标签: c# ms-word openxml openxml-sdk

我正在尝试使用Open Office XML输出word文件,但似乎无法使间距正确。

Variable name:ATTEND
 Description:Student's attendance status during the 

我希望word文件是这样的(在:)之后有空格:

Variable name: ATTEND
Description:Student's attendance status during the 

我的代码如下,间距消失了:

start of my function
// Add a new main document part. 
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

            // Create the Document DOM.
            mainPart.Document = new Document();

            Body body = mainPart.Document.AppendChild(new Body());

            ParagraphProperties paragraphProperties = new ParagraphProperties
            (
                new ParagraphStyleId() { Val = "No Spacing" },
                new SpacingBetweenLines() { After = "0" }
            );

            Paragraph para = body.AppendChild(new Paragraph(paragraphProperties));
            Run run = para.AppendChild(new Run());

            RunProperties runProperties = run.AppendChild(new RunProperties(new Bold()));
            run.AppendChild(new Text("Variable name: "));

            run = para.AppendChild(new Run());
            run.AppendChild(new Text(" ATTEND"));

            para = body.AppendChild(new Paragraph());

            run = para.AppendChild(new Run());

            runProperties = run.AppendChild(new RunProperties(new Bold()));
            run.AppendChild(new Text("Description: "));

            run = para.AppendChild(new Run());
            run.AppendChild(new Text(" Student's attendance status during the "));


            // Save changes to the main document part. 
            wordDocument.MainDocumentPart.Document.Save();
        }

1 个答案:

答案 0 :(得分:6)

通常,OpenXML会修剪每个Text成员。要保留每个Text成员中的空格,因此您将使用test test代替test test,设置Space成员的特殊Text属性:< / p>

Text txt = new Text("text here ") { Space = SpaceProcessingModeValues.Preserve };