[简单]poi替换word 2007模版常用方法小结
使用已有的word 2007模版导出,常用的方法一般以下几种:
(一)解压word模版后替换变量再压缩为word导出
这是最简单的一种方法,适应性广,代码简单。解压word后,替换变量对应的文件即可,至于怎么替换,方法很多,可以直接替换字符串,也可以解析xml文件替换,替换图片可以直接把/word/media/对应的图片换掉。
放一段简单的使用xpath替换的demo:
public void replaceValueByXPath(String filePath,
Map<String, String> paramMap) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder
.parse(new FileInputStream(new File(filePath)));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
HashMap<String, String> prefMap = new HashMap<String, String>();
prefMap.put("ve","http://schemas.openxmlformats.org/markup-compatibility/2006");
prefMap.put("o", "urn:schemas-microsoft-com:office:office");
prefMap.put("r","http://schemas.openxmlformats.org/officeDocument/2006/relationships");
prefMap.put("m","http://schemas.openxmlformats.org/officeDocument/2006/math");
prefMap.put("v", "urn:schemas-microsoft-com:vml");
prefMap.put("wp","http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
prefMap.put("w10", "urn:schemas-microsoft-com:office:word");
prefMap.put("w","http://schemas.openxmlformats.org/wordprocessingml/2006/main");
prefMap.put("wne","http://schemas.microsoft.com/office/word/2006/wordml");
SimpleWordNamespaceContext context = new SimpleWordNamespaceContext(prefMap);
xpath.setNamespaceContext(context);
XPathExpression expr = xpath.compile("//w:t");
NodeList resultNodeList = (NodeList) expr.evaluate(doc,
XPathConstants.NODESET);
for (int i = 0, len = resultNodeList.getLength(); i < len; i++) {
Node node = resultNodeList.item(i);
String textValue = node.getTextContent();
for (Entry<String, String> entry : paramMap.entrySet()) {
textValue = textValue.replaceAll("\\$\\{" + entry.getKey()
+ "\\}", Matcher.quoteReplacement(entry.getValue()));
}
node.setTextContent(textValue);
}
saveDoc2XmlFile(doc, filePath);
}
public boolean saveDoc2XmlFile(Document document, String filename) {
boolean flag = true;
try {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
DOMSource source = new DOMSource();
source.setNode(document);
StreamResult result = new StreamResult();
FileOutputStream fileOutputStream = new FileOutputStream(filename);
result.setOutputStream(fileOutputStream);
transformer.transform(source, result);
fileOutputStream.close();
} catch (Exception ex) {
flag = false;
ex.printStackTrace();
}
return flag;
}
class SimpleWordNamespaceContext implements NamespaceContext {
private final Map<String, String> PREF_MAP = new HashMap<String, String>();
public SimpleWordNamespaceContext(final Map<String, String> prefMap) {
PREF_MAP.putAll(prefMap);
}
public String getNamespaceURI(String prefix) {
return PREF_MAP.get(prefix);
}
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
public Iterator getPrefixes(String uri) {
throw new UnsupportedOperationException();
}
}
其他的参考网上的代码,网上使用这种方法的比较多。
(二)使用代码查找所有段落中的文本替换
和第一种思路类似,这种方法也有很多人使用。注意表格中单元格也有段落。代码自己搜。
(三)使用书签定位段落中位置后替换,如下所示:
这种方法网上也有人使用,iteye上有位博友上传了源码,并且实现了在书签位置前/后追加文本(在poi论坛上也有类似的代码),可以搜下。
有其他的方法的可以留言,谢谢。
转载请注明出处,原文链接:http://53873039oycg.iteye.com/blog/2190420.
全文完。