blue skies

 

OpenXml开发-向文档中添加样式

在上面插入文本的时候,有一个参数是样式ID,这个参数是怎么来的呢,我们来看下面的Xml片段

<w:style w:type="paragraph" w:styleId="MySubTitle"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:name w:val="MySubTitle" />
<w:pPr>
    <w:jc w:val="center"/><!--这里定义对齐方式-->
    <w:spacing w:before="200" w:after="0" />
</w:pPr>
<w:rPr>
    <w:b />
    <w:sz w:val="26" /> <!--这里定义字体大小-->
</w:rPr>
</w:style>

以上就是一段简单的样式定义,当然更详细的东西只能去查文档里,我也没记住多少。在定义了上面的样式后,接下来就是把这个加入到文档中去了,具体方法如下:

        public void AppendStyleString(string xmlStyleString)
        {
            // write the styles into the style part
            Uri stylesUri =
                new Uri(@"/word/styles.xml", UriKind.Relative);
            XmlDocument stylesXml =
                package.GetWritablePart(stylesUri);
            // append the styles to the document
            XPathNavigator stylesNav =
                stylesXml.DocumentElement.CreateNavigator();
            stylesNav.AppendChild(xmlStyleString);
            // write the styles back into the package part
            package.SavePart(stylesUri, stylesXml);
        }

通过上面的函数将样式字符串加入到文档中去之后,就可以在添加文本的时候指定它使用这个样式了。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sinory/archive/2009/12/04/4939146.aspx

posted on 2010-07-19 17:50  blue skies  阅读(346)  评论(0编辑  收藏  举报

导航