Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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# 如何在Word文档中插入表格以代替文本标记?_C#_Datatable_Com_Ms Word_Ms Office - Fatal编程技术网

C# 如何在Word文档中插入表格以代替文本标记?

C# 如何在Word文档中插入表格以代替文本标记?,c#,datatable,com,ms-word,ms-office,C#,Datatable,Com,Ms Word,Ms Office,我有日期表。需要在teplate文档中插入它,而不是某些字符集 我可以用这种方式将文本替换为文本(为了避免错误,大量遗漏): 使用Word=Microsoft.Office.Interop.Word; Word.\u应用程序; Word.\u文件; Object missingObj=System.Reflection.Missing.Value; 对象trueObj=true; 对象falseObj=false; 私有无效创建按钮1\u单击(对象发送者,事件参数e){ application=

我有
日期表
。需要在teplate文档中插入它,而不是某些字符集

我可以用这种方式将文本替换为文本(为了避免错误,大量遗漏):

使用Word=Microsoft.Office.Interop.Word;
Word.\u应用程序;
Word.\u文件;
Object missingObj=System.Reflection.Missing.Value;
对象trueObj=true;
对象falseObj=false;
私有无效创建按钮1\u单击(对象发送者,事件参数e){
application=新单词.application();
对象模板pathBj;
templatePathBj=“template.dot”;
试一试{
document=application.Documents.Add(参考TemplatePathBJ,
ref missingObj,ref missingObj,ref missingObj);
}
捕获(异常错误){
文件关闭(参考falseObj、参考missingObj、参考missingObj);
申请。退出(ref missingObj,ref missingObj,ref missingObj);
document=null;
application=null;
投掷误差;
}
对象strToFindObj=“%%标记%%”;
object replaceStrObj=“要替换的文本”;
单词范围单词范围;
对象替换类型OBJ;
replaceTypeObj=Word.WdReplace.wdReplaceAll;
对于(int i=1;i
为了避免这些错误,有很多遗漏

Target.Net 4.0或更高版本支持COM调用的命名参数和可选参数,因此您不需要包含所有
ref missingObj
。请参阅此MSDN文章:-此功能大大方便了对COM接口(如Microsoft Office Automation API)的调用


我必须更改什么,以便可以使用
数据表进行搜索
字符串变量
strofindobj

您必须遍历DataTables行和单元格来替换Word文档中的DataTable单元格,例如:

foreach(var dr in dt.Rows)
{
  foreach (var cell in dr.ItemArray)
  {
    string strToFind = cell.ToString();
    string replaceStr = "replace old value";
    ReplaceTextInWord(@"C:\Temp\template.docx", strToFind, replaceStr);
  }
}
如果您发现使用数据表太难,并且想要一个更简单的列表(如字典):

var listOfTextToReplace = new Dictionary<string,string>();
listOfTextToReplace.Add("%%mark%%","text to replace");
foreach(var item in listOfTextToReplace )
{
    string strToFind = item.Key;
    string replaceStr = item.Value;
    ReplaceTextInWord(@"C:\Temp\template.docx", strToFind, replaceStr);
}
var listOfTextToReplace=new Dictionary();
添加(“%%标记%%”,“要替换的文本”);
foreach(文本替换列表中的var项)
{
字符串strToFind=item.Key;
字符串replaceStr=item.Value;
ReplaceTextInWord(@“C:\Temp\template.docx”,strToFind,replacetstr);
}

以下是ReplaceTextInWord方法:

using Word = Microsoft.Office.Interop.Word;
Word._Application application;
Word._Document document;
Object missingObj = System.Reflection.Missing.Value;
Object trueObj = true;
Object falseObj = false;

private void create_button1_Click(object sender, EventArgs e) {
   //ReplaceTextInWord("template.dot", "find me", "Found"); <-- Are you sure you want to replace in a Template?
   ReplaceTextInWord(@"C:\Temp\template.docx", "%%mark%%","text to replace");  //I think you want a .DOC or DOCX file
}

private void ReplaceTextInWord(string wordDocFilePath, string strToFind, string replaceStr) {
    application = new Word.Application();
    try {
        //document = application.Documents.Add(ref templatePathObj, ref missingObj, ref missingObj, ref missingObj); 
        document = application.Documents.Open(wordDocFilePath);  //You need to open Word Documents, not add them, as per https://msdn.microsoft.com/en-us/library/tcyt0y1f.aspx
    }
    catch (Exception error) {
        document.Close(ref falseObj, ref missingObj, ref missingObj);
        application.Quit(ref missingObj, ref missingObj, ref missingObj);
        document = null;
        application = null;
        throw error;
    }

    for (int i = 1; i <= document.Sections.Count; i++) {
    Word.Range wordRange = document.Sections[i].Range;
    Word.Find findObject = wordRange.Find;
    findObject.ClearFormatting();
    findObject.Text = strToFind;
    findObject.Replacement.ClearFormatting();
    findObject.Replacement.Text = replaceStr;

    object replaceAll = Word.WdReplace.wdReplaceAll;
    findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);
    }
    application.Visible = true;
}
使用Word=Microsoft.Office.Interop.Word;
Word.\u应用程序;
Word.\u文件;
Object missingObj=System.Reflection.Missing.Value;
对象trueObj=true;
对象falseObj=false;
私有无效创建按钮1\u单击(对象发送者,事件参数e){
//ReplaceTextInWord(“template.dot”、“find me”、“Found”);
using Word = Microsoft.Office.Interop.Word;
Word._Application application;
Word._Document document;
Object missingObj = System.Reflection.Missing.Value;
Object trueObj = true;
Object falseObj = false;

private void create_button1_Click(object sender, EventArgs e) {
   //ReplaceTextInWord("template.dot", "find me", "Found"); <-- Are you sure you want to replace in a Template?
   ReplaceTextInWord(@"C:\Temp\template.docx", "%%mark%%","text to replace");  //I think you want a .DOC or DOCX file
}

private void ReplaceTextInWord(string wordDocFilePath, string strToFind, string replaceStr) {
    application = new Word.Application();
    try {
        //document = application.Documents.Add(ref templatePathObj, ref missingObj, ref missingObj, ref missingObj); 
        document = application.Documents.Open(wordDocFilePath);  //You need to open Word Documents, not add them, as per https://msdn.microsoft.com/en-us/library/tcyt0y1f.aspx
    }
    catch (Exception error) {
        document.Close(ref falseObj, ref missingObj, ref missingObj);
        application.Quit(ref missingObj, ref missingObj, ref missingObj);
        document = null;
        application = null;
        throw error;
    }

    for (int i = 1; i <= document.Sections.Count; i++) {
    Word.Range wordRange = document.Sections[i].Range;
    Word.Find findObject = wordRange.Find;
    findObject.ClearFormatting();
    findObject.Text = strToFind;
    findObject.Replacement.ClearFormatting();
    findObject.Replacement.Text = replaceStr;

    object replaceAll = Word.WdReplace.wdReplaceAll;
    findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);
    }
    application.Visible = true;
}