使用 openxml C# 在 worddocument 中插入今天的日期

问题描述 投票:0回答:1

我需要使用 OPENXML 在现有的 Word 文档中插入自我刷新的今天日期。

我的代码如下所示:

public static byte[] InsertHeader(byte[] buffer, string text, string decisionNumber)
        {
            using (var stream = new MemoryStream())
            {
                stream.Write(buffer, 0, buffer.Length);
                using (var wordDocument = WordprocessingDocument.Open(stream, true))
                {
                    var mainPart = wordDocument.MainDocumentPart;
                    var relationshipId = GenerateUniqueRelationshipId(mainPart);
                    var headerPart = mainPart.AddNewPart<HeaderPart>(relationshipId);
                    var relationshipId2 = GenerateUniqueRelationshipId(mainPart);
                    var headerPart2 = mainPart.AddNewPart<HeaderPart>(relationshipId2);
                    var runProp = new RunProperties();
                    var runProp2 = new RunProperties();
                    var runFont = new RunFonts { Ascii = "Lato" };
                    var runFont2 = new RunFonts { Ascii = "Lato" };
                    var size = new FontSize { Val = new StringValue("20") };
                    var size2 = new FontSize { Val = new StringValue("20") };
                    ParagraphProperties paragraphProperties1 = new ParagraphProperties();
                    var justification2 = new Justification() { Val = JustificationValues.Right };
                    paragraphProperties1.Append(justification2);
                    runProp.Append(runFont);
                    runProp.Append(size);
                    runProp2.Append(runFont2);
                    runProp2.Append(size2);
                    var run = new Run(new Text(text));
                    run.PrependChild(runProp);
                    var run2 = new Run(new Text(decisionNumber));
                    run2.PrependChild(runProp2);
                    var header = new Header(new Paragraph(run));
                    header.Save(headerPart);
                    var paragraph2 = new Paragraph();
                    paragraph2.Append(paragraphProperties1);
                    paragraph2.Append(run2);
                    var header2 = new Header(paragraph2);
                    header2.Save(headerPart2);
                    DocumentFormat.OpenXml.Wordprocessing.SectionProperties sectionProps = mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.SectionProperties>().FirstOrDefault();
                    if (sectionProps == null)
                    {
                        sectionProps = new DocumentFormat.OpenXml.Wordprocessing.SectionProperties();
                        mainPart.Document.Body.Append(sectionProps);
                    }

                    var headerReference = new HeaderReference() { Id = relationshipId, Type = HeaderFooterValues.First };
                    var headerReference2 = new HeaderReference() { Id = relationshipId2 };
                    sectionProps.RemoveAllChildren<HeaderReference>();
                    //Adding Title Page property
                    sectionProps.PrependChild(new TitlePage());
                    sectionProps.Append(headerReference);
                    sectionProps.Append(headerReference2);

                    mainPart.Document.Save();
                }
                buffer = stream.ToArray();
            }
            return buffer;
        }

我需要通过在今天的日期中间插入来插入扩展“文本”变量。 与使用此选项在 Word 中添加类似:

c# ms-word openxml
1个回答
0
投票

我找到了适合我的解决方案:

var mainPart = wordDocument.MainDocumentPart;
                var relationshipId = GenerateUniqueRelationshipId(mainPart);
                var headerPart = mainPart.AddNewPart<HeaderPart>(relationshipId);
                
                var paragraph = new Paragraph(
                        new Run(
                            new RunProperties(
                                new RunFonts()
                                {
                                    Ascii = "Lato",
                                    HighAnsi = "Lato"
                                },
                                new FontSize() { Val = "20" }
                            ),
                            new Text(text1 + " ")
                            {
                                Space = new EnumValue<SpaceProcessingModeValues>(
                                SpaceProcessingModeValues.Preserve)
                            }),
                        new Run(
                            new RunProperties(
                                new RunFonts()
                                {
                                    Ascii = "Lato",
                                    HighAnsi = "Lato"
                                },
                                new FontSize() { Val = "20" }
                            ),
                            new FieldChar() { FieldCharType = FieldCharValues.Begin },
                            new FieldCode(" DATE Today \\* MERGEFORMAT "),
                            new FieldChar() { FieldCharType = FieldCharValues.Separate },
                            new Run(
                                new Text(DateTime.Now.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture))
                            ),
                            new FieldChar() { FieldCharType = FieldCharValues.End }
                        ),
                        new Run(
                            new RunProperties(
                                new RunFonts()
                                {
                                    Ascii = "Lato",
                                    HighAnsi = "Lato"
                                },
                                new FontSize() { Val = "20" }
                            ),
                            new Text(" " + text2)
                            {
                                Space = new EnumValue<SpaceProcessingModeValues>(
                                SpaceProcessingModeValues.Preserve)
                            })
                );
                var header = new Header(paragraph);
                header.Save(headerPart);
© www.soinside.com 2019 - 2024. All rights reserved.