OpenXml 生成Office Word的 擴展類

代碼如下:

 

using DocumentFormat.OpenXml.Wordprocessing;

namespace StateStreet.Imix2.Web
{
    public enum Headings
    {
        Heading1,
        Heading2,
        Heading3,
        Heading4,
        Heading5,
        Heading6,
        Heading7,
        Heading8,
        Heading9
    }

    public static class BodyExtensions
    {
        public static void Append(this Body body, string text)
        {
            body.Append(new Paragraph(new Run(new Text(text))));
        }

        public static void AppendBold(this Body body, string text)
        {
            Run r = new Run(new Text(text));
            r.RunProperties = new RunProperties();
            r.RunProperties.InnerXml = boldRunStyleXml;
            body.Append(new Paragraph(r));
        }

        public static void Append(this Body body, string text, Headings heading)
        {
            Paragraph p1 = new Paragraph();
            p1.ParagraphProperties = new ParagraphProperties();
            p1.ParagraphProperties.InnerXml = "<w:pStyle w:val=\"" + heading.ToString() + "\"/><w:rPr><w:rFonts w:eastAsia=\"宋体\"/><w:lang w:eastAsia=\"zh-CN\"/></w:rPr>";
            p1.Append(new Run(new Text(text)));
            body.Append(p1);
        }

        public static void AppendTitle(this Body body, string text)
        {
            Text t = new Text(text);

            Run r = new Run(t);
            r.RunProperties = new RunProperties();
            r.RunProperties.InnerXml = titleRunStyleXml;

            Paragraph p1 = new Paragraph(r);
            p1.ParagraphProperties = new ParagraphProperties();
            p1.ParagraphProperties.InnerXml = titleFormatXml2;
            //p1.Append(new Run(new Text(text)));
            body.Append(p1);
        }

        private static string titleFormatXml
        {
            get
            {
                string s = string.Empty;
                s += "<w:rFonts w:ascii=\"宋体\" w:hAnsi=\"宋体\" w:hint=\"eastAsia\"/>";
                s += "<w:b/>";
                s += "<w:sz w:val=\"40\"/>";
                s += "<w:szCs w:val=\"40\"/>";
                return s;
            }
        }

        private static string titleFormatXml2
        {
            get
            {
                string s = string.Empty;
                s += "<w:jc w:val=\"center\"/>";
                s += "<w:rPr>";
                s += "<w:rFonts w:ascii=\"宋体\" w:eastAsia=\"PMingLiU\" w:hAnsi=\"宋体\"/>";
                s += "<w:b/>";
                s += "<w:sz w:val=\"40\"/>";
                s += "<w:szCs w:val=\"40\"/>";
                s += "<w:lang w:eastAsia=\"zh-TW\"/>";
                s += "</w:rPr>";
                return s;
            }
        }

        private static string titleRunStyleXml
        {
            get
            {
                string s = string.Empty;
                s += "<w:rPr>";
                s += "<w:rFonts w:ascii=\"宋体\" w:hAnsi=\"宋体\" w:hint=\"eastAsia\"/>";
                s += "<w:b/>";
                s += "<w:sz w:val=\"40\"/>";
                s += "<w:szCs w:val=\"40\"/>";
                s += "</w:rPr>";
                return s;
            }
        }

        private static string boldRunStyleXml
        {
            get
            {
                string s = string.Empty;
                s += "<w:rFonts w:hint=\"eastAsia\"/>";
                s += "<w:b/>";
                return s;
            }
        }
    }
}


 

我們來看,Word文檔的結構

WordprocessingDocument DocumentFormat.OpenXml.Packaging
 Document DocumentFormat.OpenXml.Wordprocessing
  Body  DocumentFormat.OpenXml.Wordprocessing
   Paragraph  DocumentFormat.OpenXml.Wordprocessing
    Run    DocumentFormat.OpenXml.Wordprocessing
     Text    DocumentFormat.OpenXml.Wordprocessing

 

一些屬性:

這些屬性位於DocumentFormat.OpenXml.Wordprocessing命名空間

Bold

Indentation

Italic

Underline

Font

設置屬性的方法是,新建以上屬性,將這些屬性加入X.XProperties中。X可以是Paragraph,也可以是Run

注意X.XProperties默認為null,所以,你必須先實體化一個XProperties。

            Paragraph p = new Paragraph(new Run(new Text(text)));
            p.ParagraphProperties = new ParagraphProperties();
            Indentation ind=new Indentation();
            ind.FirstLine = indent.ToString();
            p.ParagraphProperties.InsertAt(ind, 0);

            Bold bold = new Bold();
            p.ParagraphProperties.InsertAt(bold, 0);

 

注意這些屬性是引用類型的,你不能把一個屬性同時加入兩個對象。好在他們都實現了System.ICloneable接口。

            Bold bold = new Bold();             Bold bold2 = (Bold)bold.Clone();


 除了上訴方法,還可以直接寫XML:

        public static void Append(this Body body, string text, Headings heading)
        {
            Paragraph p1 = new Paragraph();
            p1.ParagraphProperties = new ParagraphProperties();
            p1.ParagraphProperties.InnerXml = "<w:pStyle w:val=\"" + heading.ToString() + "\"/><w:rPr><w:rFonts w:eastAsia=\"宋体\"/><w:lang w:eastAsia=\"zh-CN\"/></w:rPr>";
            p1.Append(new Run(new Text(text)));
            body.Append(p1);
        }


當你不孰悉這麼多的屬性的用法的時候,可以在Word 2007裡調好格式,解壓,找到Document.xml文件,複製裡面的Xml。

Document.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
    <w:body>
        <w:p w:rsidR="00870D7D" w:rsidRDefault="00C560D8">
            <w:pPr>
                <w:rPr>
                    <w:rFonts w:eastAsia="PMingLiU"/>
                    <w:b/>
                </w:rPr>
            </w:pPr>
            <w:r w:rsidRPr="00C560D8">
                <w:rPr>
                    <w:rFonts w:hint="eastAsia"/>
                    <w:b/>
                </w:rPr>
                <w:t>宋体黑体字</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00E46841" w:rsidRDefault="00E46841">
            <w:r w:rsidRPr="00E46841">
                <w:rPr>
                    <w:rFonts w:hint="eastAsia"/>
                </w:rPr>
                <w:t>普通字体</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00E46841" w:rsidRDefault="00E46841">
            <w:pPr>
                <w:rPr>
                    <w:i/>
                </w:rPr>
            </w:pPr>
            <w:r w:rsidRPr="00E46841">
                <w:rPr>
                    <w:rFonts w:hint="eastAsia"/>
                    <w:i/>
                </w:rPr>
                <w:t>斜体字</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00E46841" w:rsidRDefault="00E46841">
            <w:pPr>
                <w:rPr>
                    <w:u w:val="single"/>
                </w:rPr>
            </w:pPr>
            <w:r w:rsidRPr="00E46841">
                <w:rPr>
                    <w:rFonts w:hint="eastAsia"/>
                    <w:u w:val="single"/>
                </w:rPr>
                <w:t>下划线</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00E46841" w:rsidRDefault="00E46841">
            <w:pPr>
                <w:rPr>
                    <w:strike/>
                </w:rPr>
            </w:pPr>
            <w:r w:rsidRPr="00E46841">
                <w:rPr>
                    <w:rFonts w:hint="eastAsia"/>
                    <w:strike/>
                </w:rPr>
                <w:t>删除字</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00E46841" w:rsidRPr="00712774" w:rsidRDefault="00712774" w:rsidP="00712774">
            <w:pPr>
                <w:ind w:firstLine="720"/>
            </w:pPr>
            <w:r w:rsidRPr="00712774">
                <w:rPr>
                    <w:rFonts w:hint="eastAsia"/>
                </w:rPr>
                <w:t>普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行普通字体段落空两行</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00E46841" w:rsidRPr="00712774" w:rsidSect="002D468F">
            <w:pgSz w:w="12240" w:h="15840"/>
            <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/>
            <w:cols w:space="720"/>
            <w:docGrid w:linePitch="360"/>
        </w:sectPr>
    </w:body>
</w:document>


 

 MSDN DocumentFormat.OpenXml.Wordprocessing主頁:

http://msdn.microsoft.com/en-us/library/cc820745.aspx

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

织网者Eric

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值