字处理文档 C# 中的 SUM 值到字内容控件



所以这个论坛上的某个人可能对如何解决这个问题有很好的意见,我试图解决这个问题最好的IDE。 下面是我的代码的摘录,它的作用是读取传入消息,在本例中是具有类似声明的 XML

<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 文档具有相应的内容控件名称,因此Avg_inkl_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

然后将它们放在相应的内容控件名称中。

关于如何处理这个问题的任何建议?

非常基本,创建了 3 个列表,将所有值相加,然后将其全部汇总。