代码之家  ›  专栏  ›  技术社区  ›  Wookoai

WordprocessingDocument C#to word contentControl中的SUM值

  •  0
  • Wookoai  · 技术社区  · 7 年前

    所以这个论坛上的一些人可能对如何解决这个问题有很好的意见,我正在努力思考什么可能是最好的ide。

    <Key> Avg_exkl_moms </Key>
    <Value>123</Value>
    <Key> Tjal_exkl_moms </Key>
    <Value>321</Value>
    <Key> Prov_exkl_moms </Key>
    <Value>7375</Value>
    
    <Key> Avg _moms </Key>
    <Value>13</Value>
    <Key> Tjal _moms </Key>
    <Value>31</Value>
    <Key> Prov _moms </Key>
    <Value>75</Value>
    
    <Key> Avg_ inkl _moms </Key>
    <Value>456</Value>
    <Key> Tjal_ inkl _moms </Key>
    <Value>555</Value>
    <Key> Prov_ inkl _moms </Key>
    <Value>555</Value>
    

    word文档具有相应的contentControlTagName,因此Avg\u inkl\u moms将用字段值7375填充

          private static void FillContentControls(Dictionary<string, string> parameters, MemoryStream stream)
                {
                    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true))
                    {
                        foreach (KeyValuePair<string, string> parameter in parameters)
                        {
    
       string contentControlTagName = parameter.Key;
                            string newValue = parameter.Value;
                            IEnumerable<XElement> contentControls = wordDoc.MainDocumentPart.GetXDocument().Descendants(sdt)
                                                                       .Elements(sdtContent)
                                                                       .Descendants(wordText)
                                                                       .Where(e => ((string)e.Ancestors(sdt)
                                                                       .Elements(sdtPr)
                                                                       .Elements(tag)
                                                                       .Attributes(tagVal).First().Value == contentControlTagName));
    
                            int i = 0;
                            foreach (XElement element in contentControls)
                            {
    
                                element.Value = (i == 0 ? newValue : String.Empty);
                                i++;
                            }
    

    现在我要做的是将不同的值相加

    Avg_exkl_moms + Tjal_exkl_moms + Prov_exkl_moms = total_exlk_moms
    Avg_moms + Tjal_moms + Prov_moms = total_moms
    Avg_inkl_moms + Tjal_ inkl _moms + Prov_ inkl _moms = total_ inkl _moms
    

    然后将它们放入相应的contentControlTagName中。

    对如何处理这个问题有什么建议吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Wookoai    7 年前

    只是非常基本,创建了3个列表,将所有的值相加,然后汇总。