如何使用POI 3.8替换占位符的docx的头在Java中
答案:2 悬赏:40 手机版
解决时间 2021-02-07 14:50
- 提问者网友:沦陷
- 2021-02-07 01:44
如何使用POI 3.8替换占位符的docx的头在Java中
最佳答案
- 五星知识达人网友:一把行者刀
- 2021-02-07 02:43
公共XWPFDocument的setHeader(XWPFDocument文件,字符串标记,字符串textToReplace){
XWPFHeaderFooterPolicy政策= document.getHeaderFooterPolicy();
XWPFHeader头= policy.getHeader(0);
replaceInParagraphs(header.getParagraphs(),令牌,textToReplace);
返回文件;
}私人无效replaceInParagraphs(列表< XWPFParagraph>段落,字符串占位符,字符串replaceText){
对于(XWPFParagraph xwpfParagraph:段){
清单< XWPFRun>奔跑= xwpfParagraph.getRuns();
对于(XWPFRun运行:运行){
串runText = run.getText(run.getTextPosition()); 如果(占位=“与”&!&安培;!placeHolder.isEmpty()){
如果(runText = NULL&放大器;!&安培;
Pattern.compile(占位符,Pattern.CASE_INSENSITIVE).matcher(runText).find()){
runText = replaceText;
}
}
run.setText(runText,0);
}
}
}
XWPFHeaderFooterPolicy政策= document.getHeaderFooterPolicy();
XWPFHeader头= policy.getHeader(0);
replaceInParagraphs(header.getParagraphs(),令牌,textToReplace);
返回文件;
}私人无效replaceInParagraphs(列表< XWPFParagraph>段落,字符串占位符,字符串replaceText){
对于(XWPFParagraph xwpfParagraph:段){
清单< XWPFRun>奔跑= xwpfParagraph.getRuns();
对于(XWPFRun运行:运行){
串runText = run.getText(run.getTextPosition()); 如果(占位=“与”&!&安培;!placeHolder.isEmpty()){
如果(runText = NULL&放大器;!&安培;
Pattern.compile(占位符,Pattern.CASE_INSENSITIVE).matcher(runText).find()){
runText = replaceText;
}
}
run.setText(runText,0);
}
}
}
全部回答
- 1楼网友:你哪知我潦倒为你
- 2021-02-07 03:37
同问。。。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯