使用C#生成word文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiyanglin/article/details/79660448

需要引入DocumentFormat.OpenXml.dll和WindowsBase.dll

类库和测试工程可以从这获取:https://download.csdn.net/download/jiyanglin/10303215

也可以从官网下载安装:

https://msdn.microsoft.com/en-us/library/office/cc850833.aspx

测试代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using OpenXmlParagraph = DocumentFormat.OpenXml.Wordprocessing.Paragraph;
using OpenXmlWordRun = DocumentFormat.OpenXml.Wordprocessing.Run;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            WordprocessingDocument doc = WordprocessingDocument.Create("D:\\xxxxx.docx", WordprocessingDocumentType.Document);
            MainDocumentPart mainPart = doc.AddMainDocumentPart();
            mainPart.Document = new Document();
            Body body = mainPart.Document.AppendChild(new Body());




            OpenXmlParagraph p = body.AppendChild(new OpenXmlParagraph());
            string str = "我是文章的内容。。。。";
            p.AppendChild(new OpenXmlWordRun(new Text(str)));




            doc.Close();

        }
    }
}

猜你喜欢

转载自blog.csdn.net/jiyanglin/article/details/79660448
今日推荐