Word 2003后提供了一种xml的文档格式。可以利用这一点方便的生成doc文档
Word 2007更完全使用xml与数据压缩包的方式存储,使得用类似的方法处理附件也变为可能。
相关demo下载 doc-creator
概述
实现步骤如下:
- 定义Word文档样式,存为xml文档。
- 定义数据xml格式,生成样本数据。
- 修改word文档样式xml文件,制作xsl模板文件。
- 编程实现:数据->xml数据->模板转换->doc文档
创建模板
打开Word文档,另存为:Word 2003 xml格式。
使用xsl编辑器 (如 Stylus Studio)或手工编辑,创建XSL模板 。
相关XSL问题可以参考 http://www.w3schools.com/xsl/
利用dom4j处理相关xml操作
下面是一些基础实现方法
/*
* (non-Javadoc)
*
* @see cn.ccb.sarm.bizprocess.assetdeal.exam.approve.bizservice.IWfConferenceBS#writeXML(java.io.OutputStream,
* cn.ccb.sarm.bizprocess.assetdeal.exam.approve.common.bo.TaskVO,
* java.util.List, cn.ccb.sarm.common.model.bo.WfConferenceBO,
* cn.ccb.sarm.common.model.bo.WfConfBpBO)
*/
public void writeXML(OutputStream output, TaskVO task, List opinionList, WfConferenceBO conf,
WfConfBpBO confBP) throws IOException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("conference");
Element taskNode = root.addElement("task");
addBeanProp(task, taskNode);
Element confNode = root.addElement("conferenceInfo");
addBeanProp(conf, confNode);
Element confBpNode = root.addElement("confBPInfo");
addBeanProp(confBP, confBpNode);
Element approveNode = root.addElement("approvers");
addListMapEntry(opinionList, approveNode);
XMLWriter writer = new XMLWriter(output);
writer.write(doc);
}
/**
* 转换list对象为多个元素
*
* @param list
* 对象list
* @param node
* 父节点
*/
private void addListMapEntry(List list, Element node) {
Iterator it = list.iterator();
while (it.hasNext()) {
Map map = (Map) it.next();
Element approveNode = node.addElement("approver");
addMapEntry(map, approveNode);
}
}
/**
* 转换bean为xml
*
* @param bean
* 对象bean
* @param node
* 父节点
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
private void addBeanProp(Object bean, Element node) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Map propMap = BeanUtils.describe(bean);
addMapEntry(propMap, node);
}
/**
* 转换map对象为xml
*
* @param map
* 数据对象
* @param node
* 父节点
*/
private void addMapEntry(Map map, Element node) {
Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry e = (Entry) it.next();
node.addElement(e.getKey().toString()).addText(
(e.getValue() == null ? "" : e.getValue().toString()));
}
}
/*
* (non-Javadoc)
*
* @see cn.ccb.sarm.bizprocess.assetdeal.exam.approve.bizservice.IWfConferenceBS#transformDocument(java.io.OutputStream,
* java.io.FileInputStream, java.io.FileInputStream)
*/
public void transformDocument(OutputStream out, InputStream data, InputStream template)
throws TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transform = factory.newTransformer(new StreamSource(template));
Result result = new StreamResult(out);
transform.transform(new StreamSource(data), result);
}测试代码
public void testWriteXML() throws Exception {
IWfConferenceBS conferenceBS = new WfConferenceBS();
TaskVO task = new TaskVO();
task.setBizTypeName("测试业务类型");
List opinionList = new ArrayList();
Map m = new HashMap();
m.put("userName", "testUser");
m.put("userConfRole", "testUserRole");
m.put("approverOpinion", "approverOpinion");
m.put("lastChangeTime", "time");
opinionList.add(m);
WfConferenceBO conf = new WfConferenceBO();
conf.setConfPlace("place");
WfConfBpBO confBP = new WfConfBpBO();
confBP.setConfResult("testResult");
confBP.setConfOpinion("confOpinion");
File tempOutputFile = File.createTempFile("specialApproveTemp", ".xml");
OutputStream output = new FileOutputStream(tempOutputFile);
conferenceBS.writeXML(output, task, opinionList, conf, confBP);
output.flush();
output.close();
OutputStream out = new FileOutputStream(File.createTempFile("specialApproveTemp", ".doc"));
File template = ResourceUtils
.getFile("classpath:cn/ccb/clpm/wf/common/specialApproveTemplate.xsl");
conferenceBS.transformDocument(out, new FileInputStream(tempOutputFile),
new FileInputStream(template));
output.flush();
output.close();
}
After the day