使用Openxml插入表后,替换文本在word documnt中不起作用 [英] Replacing of text is not working in word documnt after inserting a table using Openxml

查看:236
本文介绍了使用Openxml插入表后,替换文本在word documnt中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有word文档,在该文档中我有一张桌子和一些数据。

我的任务是:

1)对于那个表我需要在表的末尾添加一些行。

2)替换word文档中的一些文本(不在表中) ),还有其他一些文字。



我已经完成了两个。

,这是我的代码。



表格:

-----------

DocumentFormat.OpenXml.Wordprocessing.TableRow theRow = tab .Elements< documentformat.openxml.wordprocessing.tablerow>()。Last();

for(int i = 0; i<(rcount - 1); i ++)

$

DocumentFormat.OpenXml.Wordprocessing.TableRow rowCopy =(DocumentFormat.OpenXml.Wordprocessing.TableRow)theRow.CloneNode(true);

for(int j = 0 ; j< Columncells.Count(); j ++)

{

DocumentFormat.OpenXml.Wordprocessing.TableCell cell = rowCopy.Descendants< documentformat.openxml.wordprocessing.tablecell>()。ElementAt(j);

DocumentFormat.OpenXml。 Wordprocessing.Paragraph parg = cell.Elements< documentformat.openxml.wordprocessing.paragraph>()。First();

DocumentFormat.OpenXml.Wordprocessing.Run r = parg.Elements< documentformat.openxml.wordprocessing .run>()。First();

DocumentFormat.OpenXml.Wordprocessing.Text tex = r.Elements< documentformat.openxml.wordprocessing.text>()。First();

tex.Text = tex.Text.Trim('1')+(i + 2);

if(j == 0&& i == 0)

{

rowCopy.Descendants< documentformat.openxml.wordprocessing.tablecell>()。ElementAt(j).Append(new DocumentFormat.OpenXml.Wordprocessing .Paragraph(run));

}

else

{

rowCopy.Descendants< documentformat.openxml。 wordprocessing.tablecell>()。ElementAt(j).Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());

}

}

tab.InsertBefore< tablerow>(rowCopy,theRow);

}



替换文字:

--------------------



正则表达式regexTe xt = new Regex(Helo);

docText = regexText.Replace(docText,Hello);



其实我的问题是



通过使用我用于表的代码,替换word文档中的文本不是工作,当我删除代码的那部分时,替换功能正常工作。



任何人都可以帮助我,代码的哪个位置阻止替换功能。



提前谢谢。

Hi All,

I have word document, and in that document i have a table and also some data.
My tasks are:
1) For that table i need to add some rows at the end of the table.
2) Replace some text in the word document(Not in the table), with some other text.

I have done both of them.
and here is my code.

For table:
-----------
DocumentFormat.OpenXml.Wordprocessing.TableRow theRow = tab.Elements<documentformat.openxml.wordprocessing.tablerow>().Last();
for (int i = 0; i < (rcount - 1); i++)
{
DocumentFormat.OpenXml.Wordprocessing.TableRow rowCopy = (DocumentFormat.OpenXml.Wordprocessing.TableRow)theRow.CloneNode(true);
for (int j = 0; j < Columncells.Count(); j++)
{
DocumentFormat.OpenXml.Wordprocessing.TableCell cell = rowCopy.Descendants<documentformat.openxml.wordprocessing.tablecell>().ElementAt(j);
DocumentFormat.OpenXml.Wordprocessing.Paragraph parg = cell.Elements<documentformat.openxml.wordprocessing.paragraph>().First();
DocumentFormat.OpenXml.Wordprocessing.Run r = parg.Elements<documentformat.openxml.wordprocessing.run>().First();
DocumentFormat.OpenXml.Wordprocessing.Text tex = r.Elements<documentformat.openxml.wordprocessing.text>().First();
tex.Text = tex.Text.Trim('1') + (i + 2);
if (j == 0 && i == 0)
{
rowCopy.Descendants<documentformat.openxml.wordprocessing.tablecell>().ElementAt(j).Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run));
}
else
{
rowCopy.Descendants<documentformat.openxml.wordprocessing.tablecell>().ElementAt(j).Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
}
}
tab.InsertBefore<tablerow>(rowCopy, theRow);
}

For replace of text:
--------------------

Regex regexText = new Regex("Helo");
docText = regexText.Replace(docText, "Hello");

Actually my problem is

By using the code which i used for table, replacing of the text in word document is not working, When i remove that part of code than, replace functionality is working fine.

Can anybody help me, at which place of the code it is preventing replace functionality.

Thanks in advance.

推荐答案

通过添加行更改文档后,docText可能无效?

通过逐步调试来查看详细信息可能会对您有所帮助。
The docText may be invalid after you change the document by add rows?
Stey by step debug to see the details may help you。


这篇关于使用Openxml插入表后,替换文本在word documnt中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆