Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c使用open xml替换word表中的标记#_C#_Openxml_Openxml Sdk - Fatal编程技术网

C# 使用c使用open xml替换word表中的标记#

C# 使用c使用open xml替换word表中的标记#,c#,openxml,openxml-sdk,C#,Openxml,Openxml Sdk,我有一个要求,我要根据计数将word表克隆到多个表中,并插入到word文档中。如果我有5条记录,它应该使用现有的表克隆添加5个表,并替换表内的令牌。它正在添加5个表,但正在用最后一条记录替换所有五个表的内容,以便所有五个表都具有与最后一行相同的数据 //Main code string tableInnerText = tblMain.InnerText; List<string> fieldsList = GetFieldList(tableInnerText, repeatedT

我有一个要求,我要根据计数将word表克隆到多个表中,并插入到word文档中。如果我有5条记录,它应该使用现有的表克隆添加5个表,并替换表内的令牌。它正在添加5个表,但正在用最后一条记录替换所有五个表的内容,以便所有五个表都具有与最后一行相同的数据

//Main code
string tableInnerText = tblMain.InnerText;
List<string> fieldsList = GetFieldList(tableInnerText, repeatedTableListName);

var parentParagraph = printBookmarkStart.Parent;
tbl[0].Remove();
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
  if (pageBreak.ToUpper() == "YES" && rowIndex > 0)
  {
    _wordDoc.MainDocumentPart.Document.Body.InsertAfter(new Paragraph(new Run(new Break() { Type = BreakValues.Page })), parentParagraph);
  }

  tblMainTmp = (Table)tblMain.Clone();
  _wordDoc.MainDocumentPart.Document.Body.InsertAfter<Table>(tblMainTmp, parentParagraph);
  _wordDoc.MainDocumentPart.Document.Save();
  UpdateRepeatedTable(dt.Rows[rowIndex], dt, rowIndex, fieldsList, repeatedTableListName);
} 

[...]

private void UpdateRepeatedTable(DataRow dr, DataTable dt, int dataRowIndex, List<string> tableColumnsList, string repeatedTableListName)
{
    try
    {
        DataTable dtFunction = GetFunctionDataTable(tableColumnsList, repeatedTableListName);

        for (int j = 0; j < tableColumnsList.Count; j++)
        {
            string dtColumnName = ParseRelatedListFieldName(tableColumnsList[j], repeatedTableListName, true);
            string dtColumnValue = dr[dtColumnName].ToString();
            string formatValue = string.Empty;

            string replaceValue = dtColumnValue;
            //TODO: Check if fields a function field
            DataRow[] findRow = dtFunction.Select(string.Format("FieldName='{0}'", tableColumnsList[j]));
            bool bIsCheckBoxField = false;
            if (findRow.Length > 0)
            {
                bIsCheckBoxField = (findRow[0]["FunctionName"].ToString().ToUpper() == "CHECKBOX" ? true : false);
                formatValue = findRow[0]["Format"].ToString();
                replaceValue = GetFunctionValue(dt, 0, findRow[0], ""/*, null*/);
            }

            _clsLog.WriteMessage("Replacing " + tableColumnsList[j] + " with " + replaceValue, ClsLog.LogType.Log);

            SearchAndReplacer.SearchAndReplace(_wordDoc, tableColumnsList[j], replaceValue, true);              

            if (bIsCheckBoxField == true)
            {
                ProcessStandardFieldCheckBox(/*wordApp,*/ dtColumnValue, formatValue);
            }
        }
    }
    catch (Exception ex)
    {
        _clsLog.WriteMessage(ex);
    }
}
//主代码
string tableInnerText=tblMain.InnerText;
List fieldsList=GetFieldList(tableInnerText,repeatedTableListName);
var parentparation=printBookmarkStart.Parent;
tbl[0]。删除();
对于(int rowIndex=0;rowIndex0)
{
_wordDoc.MainDocumentPart.Document.Body.InsertAfter(新段落(新运行(new Break(){Type=BreakValues.Page})),parent段落);
}
tblMainTmp=(表)tblMain.Clone();
_wordDoc.MainDocumentPart.Document.Body.InsertAfter(tblMainTmp,parent段落);
_wordDoc.MainDocumentPart.Document.Save();
UpdateRepeatedTable(dt.Rows[rowIndex],dt,rowIndex,fieldsList,repeatedTableListName);
} 
[...]
私有void UpdateRepeatedTable(DataRow dr、DataTable dt、int dataRowIndex、List tableColumnsList、string repeatedTableListName)
{
尝试
{
DataTable dtFunction=GetFunctionDataTable(tableColumnsList,repeatedTableListName);
对于(int j=0;j0)
{
bIsCheckBoxField=(findRow[0][“FunctionName”].ToString().ToUpper()==“复选框”?真:假);
formatValue=findRow[0][“格式”].ToString();
replaceValue=GetFunctionValue(dt,0,findRow[0],“”/*,null*/);
}
_clsLog.WriteMessage(“将“+tableColumnsList[j]+”替换为“+replaceValue,clsLog.LogType.Log”);
SearchAndReplacer.SearchAndReplace(_wordDoc,tableColumnsList[j],replaceValue,true);
如果(bIsCheckBoxField==true)
{
ProcessStandardFieldCheckBox(/*wordApp,*/dtColumnValue,formatValue);
}
}
}
捕获(例外情况除外)
{
_clsLog.WriteMessage(ex);
}
}

我能够使用开放xml powertools中的DocumentAssembler()解决这个问题,这是Eric White建议的


DocumentAssembler为重复内容、表格和匹配条件处理文档提供了多种选择。

您能否在问题中添加UpdateReportedTable的源代码?还有一个简短的数据示例输入和输出。在这里,我们喜欢在问题中包含整个上下文。您可能会受益于阅读指南“如何提出一个好问题”,谢谢Guillaume RAYMOND,我已经更新了代码,输入和输出是Word文档。您还没有真正提出问题,尽管问题可能可以从最后一句中推断出来。更具体地说,您在寻求什么帮助?Cindy Meister我在使用开放式xml处理word时遇到了替换问题。