使用OpenXML在单词中创建动态项目的编号列表

时间:2018-11-29 07:25:20

标签: c# asp.net-mvc ms-word openxml openxml-sdk

我有一个要求,我的C#代码将从数据库中生成一个商品列表,这些商品实际上是订单说明,即如何下订单。该编号列表将包含一些超链接和一些粗体折扣代码。

我不确定如何获取该列表并将其转换为具有正确格式的单词编号列表。请帮忙。列表示例如下:

  
      
  1. 导航到http://www.google.com/ordername/basket.aspx?quantity=1&productId=23334
  2.   
  3. 输入续订代码: DG%$ 2&* JH ,然后按“更新”按钮。
  4.   
  5. 遵循结帐流程并使用您选择的方法付款。
  6.   

所有这些都是在运行时生成的,并且值来自DB

我尝试使用Open XML Productivity Tool来获取代码,但将其上方的每个列表项分成多个部分然后附加,则太复杂了。

我还需要在文档中间使用此列表-在此处使用.dotx。

我尝试的代码如下(部分没有任何超链接和粗体文本即可工作):

private static void AddBulletList(List<Run> runList, MainDocumentPart mainDocumentPart)
    {
        Body body = mainDocumentPart.Document.Body;
        Paragraph parentPara = body.Descendants<Paragraph>().FirstOrDefault(k => k.InnerText.Contains("text after which I need numbered list"));
        Paragraph emptyPara = new Paragraph(new Run(new Text("")));
        parentPara.InsertAfterSelf(emptyPara);
        NumberingDefinitionsPart numberingPart = mainDocumentPart.NumberingDefinitionsPart;
        var numberId = numberingPart.Numbering.Elements<NumberingInstance>().Count() + 1;
        foreach (Run runItem in runList)
        {
            // Create items for paragraph properties
            var numberingProperties = new NumberingProperties(new NumberingLevelReference() { Val = 0 }, new NumberingId() { Val = numberId });
            var spacingBetweenLines1 = new SpacingBetweenLines() { After = "60" };  // Get rid of space between bullets
            var indentation = new Indentation() { Left = "720", Hanging = "360" };  // correct indentation 

            ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
            RunFonts runFonts1 = new RunFonts() { Ascii = "Arial", HighAnsi = "Arial", EastAsia = "Calibri", ComplexScript = "Arial" };
            FontSize fontSize1 = new FontSize() { Val = "20" };
            FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript() { Val = "20" };
            paragraphMarkRunProperties1.Append(runFonts1);
            paragraphMarkRunProperties1.Append(fontSize1);
            paragraphMarkRunProperties1.Append(fontSizeComplexScript1);

            // create paragraph properties
            var paragraphProperties = new ParagraphProperties(numberingProperties, spacingBetweenLines1, indentation, paragraphMarkRunProperties1);

            // Create paragraph 
            var newPara = new Paragraph(paragraphProperties);

            // Add run to the paragraph
            newPara.AppendChild(runItem);

            // Add one bullet item to the body
            //body.AppendChild(newPara);
            emptyPara.InsertAfterSelf(newPara);
        }
    }

0 个答案:

没有答案