Java Word文档消息
我正在创建一个Word文档,它可以工作,但在打开之前会显示一条消息 我使用的是一个单页模板,我打开并多次附加到第一个模板。因为只有一页,我没有任何问题,所以我想问题就出在附件上。我复制了这个方法,我理解它的作用,但我看不出有任何问题Java Word文档消息,java,xml,ms-word,apache-poi,openxml,Java,Xml,Ms Word,Apache Poi,Openxml,我正在创建一个Word文档,它可以工作,但在打开之前会显示一条消息 我使用的是一个单页模板,我打开并多次附加到第一个模板。因为只有一页,我没有任何问题,所以我想问题就出在附件上。我复制了这个方法,我理解它的作用,但我看不出有任何问题 private void appendBody(CTBody src, CTBody append) throws Exception { XmlOptions optionsOuter = new XmlOptions(); optionsOut
private void appendBody(CTBody src, CTBody append) throws Exception {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String srcString = src.xmlText();
String prefix = srcString.substring(0,srcString.indexOf(">")+1);
String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
String suffix = srcString.substring(srcString.lastIndexOf("<"));
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + suffix);
src.set(makeBody);
}
private void appendBody(CTBody src,CTBody append)引发异常{
XmlOptions options外部=新的XmlOptions();
optionsOuter.setSaveOuter();
字符串appendString=append.xmlText(选项外部);
String srcString=src.xmlText();
字符串前缀=srcString.substring(0,srcString.indexOf(“>”)+1);
String mainPart=srcString.substring(srcString.indexOf(“>”)+1,srcString.lastIndexOf("您所展示的appendBody
方法简直是一种糟糕的做法。word文档不是多个CTBody
元素的串联。它应该只包含一个主CTBody
元素。如果需要分页符,那么应该有一个XWPFRun
添加了一个。您能打印并检查pre吗fix+mainPart+addPart+sufix
包含有效的xml/数据?@AxelRichter这是我第一次尝试的,但后来我无法将段落列表复制到新页面中。我有几个方法可以做到这一点,但我无法迭代。我将重试。谢谢!如果Word显示该消息,则您创建的打开的xml标记为not无效。为了理解到底是什么错误,您应该在问题中包含开放式XML标记。@ThomasBarnekow我不再使用appendBody方法,因为正如Axel所说,这是一种不好的做法。我使用的是模板,现在工作得很好。谢谢!
@Override
public XWPFDocument createCertificates(CandidateList candidateList, String signName, HttpServletResponse response) throws Exception {
List<CandidateList> candidates = candidateListRep.findCandidateList(candidateList.getCategory(), candidateList.getCode(), candidateList.getCourseDate().toUpperCase());
CourseName description = courseNameRep.findDescription(candidateList.getCode(), candidateList.getCategory(), candidateList.getAdminYear());
XWPFDocument firstDoc = createDocument(candidates.get(0), description.getDescription(), signName);
candidates.remove(0);
CTBody firstCTBody = firstDoc.getDocument().getBody();
for (CandidateList candidate : candidates) {
XWPFDocument doc = createDocument(candidate, description.getDescription(), signName);
CTBody docCTBody = doc.getDocument().getBody();
appendBody(firstCTBody, docCTBody);
}
return firstDoc;
}
private XWPFDocument createDocument(CandidateList candidate, String courseDesc, String singName) throws IOException {
File wordDoc = new ClassPathResource("/static/templates/Certificate.docx").getFile();
try (FileInputStream fis = new FileInputStream(wordDoc.getAbsolutePath())) {
XWPFDocument doc = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = doc.getParagraphs();
XWPFParagraph paragraph;
List<XWPFRun> runs;
replaceText(paragraphs, 12, "Name", candidate.getName() + " " + candidate.getSurname());
replaceText(paragraphs, 29, "CourseName", courseDesc);
replaceText(paragraphs, 42, "Sign", singName);
return doc;
}
}