首页 > 编程语言 >C#使用openxml-sdk生成word文档

C#使用openxml-sdk生成word文档

时间:2023-02-10 13:55:34浏览次数:39  
标签:Body SectionProperties word C# mainPart table new Document openxml

从nuget中安装openxml

 

 测试代码

            using (WordprocessingDocument doc = WordprocessingDocument.Create("test.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
            {


                MainDocumentPart mainPart = doc.AddMainDocumentPart();
                //文档内容html
                new Document(new Body()).Save(mainPart);
                string altChunkId = "id";
                string table = "<table style=\"border-collapse: collapse; \">";
                for (int i = 0; i < 3; i++)
                {
                    table += "<tr>";
                    for (int j = 0; j < 5; j++)
                    {
                        table += "<td style=\"border: 1px solid red; width: 200px\">66666</td>";
                    }
                    table += "</tr>";
                }
                table += "</table>";
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes($"<html><head><meta charset=\"UTF-8\"></head><body><h1 style=\"color:red\" >我是标题111</h1><span>我是表格666666666666666666</span>{table}</body></html>"));
                AlternativeFormatImportPart formatImportPart = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);
                formatImportPart.FeedData(ms);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document.Body.Append(altChunk);

                //页眉页脚和样式
                RunProperties Properties = new RunProperties() {Underline=new Underline() { Val= UnderlineValues .Single} };
                var run = new Run(new Text() { Text = "我是页眉                         哈哈哈哈哈 0.123456" });
                run.PrependChild(Properties);


                Header header = new Header(new Paragraph(run));
                Footer footer = new Footer(new Paragraph(new Run(new Text() { Text = "我是页脚" })));


                mainPart.AddNewPart<HeaderPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", "r01").Header = header;
                mainPart.AddNewPart<FooterPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", "r02").Footer = footer;
                SectionProperties sp = new SectionProperties(
                    new HeaderReference() { Id = "r01", Type = HeaderFooterValues.Default },
                    new FooterReference() { Id = "r02", Type = HeaderFooterValues.Default }
                    );

                mainPart.Document.Body.AppendChild<SectionProperties>(sp);

                //页边距
                SectionProperties sectionProps = new SectionProperties();
                PageMargin pageMargin = new PageMargin() { Top = 1440, Right = 1440, Bottom = 1440, Left = 1440, Header = 1080, Footer = 720, Gutter = 0 };
                sectionProps.Append(pageMargin);
                mainPart.Document.Body.Append(sectionProps);

                mainPart.Document.Save();
            }

  效果

 

标签:Body,SectionProperties,word,C#,mainPart,table,new,Document,openxml
From: https://www.cnblogs.com/QJZY/p/17108635.html

相关文章

  • Docker安装使用Kafka
    通过Docker拉取镜像的方式进行安装照例先去DockerHub找一下镜像源,看下官方提供的基本操作(大部分时候官方教程比网上的要清晰一些,并且大部分教程可能也是翻译的官方的操作......
  • CSS 根据 type 类型显示不同的样式
    <spantype="par">【背景切换】</span>span[type]{font-size:13px;}span[type='par']{color:#e05c69;background-color:#e1f1fa;.modify;}span[......
  • vscode配置代码片段
    vscode设置代码片段1.编辑需要自动生成的代码片段,全部复制,可以将复制的代码片段通过以下网址设置为配置要求的标准格式。https://snippet-generator.app/2.复制已经生成......
  • MySQLInstallerConsole.exe程序弹出窗口提示“MySQL Installer is running in Communi
    电脑运行中时不时弹出一个窗口,没反应过来就一下自动关闭了,下次再弹出,怎么回事?原来是MySQL的定时更新任务。 解决办法:这个是新版本MySQL服务自带的一个定时任务,每天......
  • Java多线程06——JUC并发包02
    1线程的同步工具类​​CountDownLatch​​​​CountDownLatch​​同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。​​CountDownLatch......
  • RPC异常重试机制详解
    1异常重试的意义发起一次RPC调用,调用远程的一个服务,如用户的登录操作,先对用户的用户名以及密码进行验证,验证成功后,获取用户基本信息。通过远程的用户服务获取用户基本信息......
  • 会话技术-Cookie特点&作用、Cookie案例分析、代码实现
    会话技术-Cookie-特点&作用Cookie的特点和作用1.cookie存储数据在客户端浏览器2.浏览器对于单个cookie的大小有限制(4kb)以及对同一个域名下的总co......
  • Python opencv 图像特效处理
     ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。 ......
  • 【老王读SpringMVC】url 与 controller method 的映射关系注册
    上文提到,如果我们自己要实现springmvc框架的话,大致需要实现如下功能:0、将url与Controllermethod的对应关系进行注册1、通过请求的url找到Controllermethod(......
  • 【spring-boot-route(七)整合jdbcTemplate操作数据库+(八)整合mybatis操作数据库】
    在一部分内容中,我们学习了Restful接口的编写,及接口文档的生成。我们需要将接口数据进行持久化存储,这一部分我们主要学习几种持久化框架将数据进行存储。本部分内容中,我们都......