Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReplaceText may throw null exception #554

Closed
ghost opened this issue May 14, 2021 · 6 comments
Closed

ReplaceText may throw null exception #554

ghost opened this issue May 14, 2021 · 6 comments

Comments

@ghost
Copy link

ghost commented May 14, 2021

[替换文本时,当需要替换的段落跨页时报错
image

image
而调整段落使其不跨页,则没有问题
测试过2.3和2.5.3都同样存在这个问题

网找的解决办法,可以不报错,但实际问题没有解决
image

@tonyqus
Copy link
Member

tonyqus commented May 17, 2021

能不能提供一个能够用于重现的Word文件?Word的Paragraph和run的嵌套情况比较复杂,可能之前实现的时候并未遇到此类嵌套方式,导致没有判断null。

1 similar comment
@tonyqus
Copy link
Member

tonyqus commented May 17, 2021

能不能提供一个能够用于重现的Word文件?Word的Paragraph和run的嵌套情况比较复杂,可能之前实现的时候并未遇到此类嵌套方式,导致没有判断null。

@ghost
Copy link
Author

ghost commented May 17, 2021

Template1.docx
附件为出错模板

@ghost
Copy link
Author

ghost commented May 29, 2021

oldtext,newtext值全正确,就是替换的时候出错了,是因为里面有<w:lastRenderedPageBreak/>的原因吗?
image

@tonyqus
Copy link
Member

tonyqus commented May 30, 2021

网上的方法只是判空,我也挺佩服这人的,只知道在网上发帖,就不能提交一个PR吗?开源项目是靠大家来维护的,老指望作者有啥意义。

你说的网上的方法并没有解决你的问题是什么意思?你能不能干脆告诉我你在替换哪个标签的时候会报错?把问题简化,因为你这是个循环,而且还有个DicWord,我又没有。

@tonyqus
Copy link
Member

tonyqus commented Jun 25, 2021

我仔细看了下,你的代码逻辑是有问题啊,你干嘛不用直接用ReplaceText去替换相应的placeholder值,而是要用替换过值的oldParaText,这务必会导致找不到,不仅会触发bug,性能也差很多(因为ReplaceText会从头找到尾)。你用下面的代码不行吗?

                  foreach (var para in doc.Paragraphs)
                    {
                        foreach (var placeholder in placeHolderDictionary)
                        {
                            if (para.ParagraphText.Contains(placeholder))
                            {
                                para.ReplaceText(placeholder, <替换值>);
                            }
                        }
                    }

我已经测试过了,即使在NPOI 2.5.3下面的代码也能跑完,只要你的Dictionary里面没有找不到的文本标签。

@tonyqus tonyqus added this to the NPOI 2.5.5 milestone Jun 25, 2021
@tonyqus tonyqus added the bug label Jun 25, 2021
@tonyqus tonyqus closed this as completed Jun 25, 2021
@tonyqus tonyqus modified the milestones: NPOI 2.5.5, NPOI 2.5.4 Jun 26, 2021
@tonyqus tonyqus changed the title word 段落跨页时ReplaceText报错 ReplaceText may throw null exception Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant