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

从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();
            }

  效果

 

posted @ 2023-02-10 13:47  奇迹之耀  阅读(459)  评论(2编辑  收藏  举报