网上相关文章很多。word2007版没试过,这里用的是2003也就是兼容版,且是已经定好的模板,使用标记替换里面需要动态生成的内容。
然后将word模板保存为2003xml版本,记得word版本必须要配对应的xml版本,不然后面会报错。
下一步是将xml模板中的base64编码替换掉。然后保存为.ftl,放入项目中。
如果图片有多张,需要循环,可以使用list标签。
java代码如下:
public Map<String, Object> setParams(Map<String, Object> map, SysPeccancy peccancy , String base) {
try {
DocUtil docUtil = new DocUtil();
Map<String, Object> dataMap = new HashMap<String, Object>();
if (peccancy.getPeccancyTime() != null && !peccancy.getPeccancyTime().trim().equals("")) {
dataMap.put("peccancyTime",peccancy.getPeccancyTime().substring(0, 10));
} else {
dataMap.put("peccancyTime", " ");
}
if (peccancy.getPeccancyNum() != null && !peccancy.getPeccancyNum().trim().equals("")) {
dataMap.put("peccancyNum", peccancy.getPeccancyNum());
}else {
dataMap.put("peccancyNum", " ");
}
if (peccancy.getOffice() != null && !peccancy.getOffice().equals("")) {
dataMap.put("office", peccancy.getOffice().getName());
}else {
dataMap.put("office", " ");
}
if (peccancy.getCheckMaster()!= null ) {
dataMap.put("checkMaster", peccancy.getCheckMaster().getName().trim());
}else {
dataMap.put("checkMaster", " ");
}
/*if (peccancy.getImgList() != null ) {
List<String> imgList = Lists.newArrayList();
for (SysSgfile file : peccancy.getImgList()) {
if(file != null && !file.getAttachpath().trim().equals("")){
String filepath = file.getAttachpath().replace("/", "\\");
String localbasePath = Global.getUserfilesBaseDir().substring(0,Global.getUserfilesBaseDir().length()-5) ;
System.out.println(localbasePath);
imgList.add(docUtil.getImageBase(localbasePath+filepath));
}
}
dataMap.put("imgList", imgList);
}else {
dataMap.put("imgList", " ");
}
String basePath = Global.getUserfilesBaseDir().substring(0,Global.getUserfilesBaseDir().length()-1)+"userfiles\\upload\\"+peccancy.getTitle()+".docx";
File oldFile = new File(basePath);
if(oldFile.exists()){
oldFile.deleteOnExit();
}
docUtil.createDoc(dataMap, "source",basePath);
} catch (Exception e) {
e.printStackTrace();
map.put("state", false);
}
return map;
}
/**
* 根据Doc模板生成word文件
*
* @param dataMap
* 需要填入模板的数据
* @param downloadType
* 文件名称
* @param savePath
* 保存路径
*/
public void createDoc(Map<String, Object> dataMap, String downloadType, String savePath) {
try {
// 加载需要装填的模板
Template template = null;
// 设置模板装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载。
// 加载模板文件,放在testDoc下
configure.setClassForTemplateLoading(this.getClass(), "/doc");
configure.setDefaultEncoding("utf-8");
// 设置对象包装器
// configure.setObjectWrapper(new DefaultObjectWrapper());
// 设置异常处理器
configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
// 定义Template对象,注意模板类型名字与downloadType要一致
template = configure.getTemplate(downloadType + ".ftl", "utf-8");
File outFile = new File(savePath);
Writer out = null;
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
template.process(dataMap, out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}
图片转base64代码:
/**
* 该方法用来将指定的文件转换成base64编码
*
* @param path:图片路径
**/
private String getImageStr2(String path) {
// 1、校验是否为空
if (path == null || path.trim().length() <= 0) {
return "";
}
// 2、校验文件是否为目录或者是否存在
File picFile = new File(path);
if (picFile.isDirectory() || (!picFile.exists()))
return "";
// 3、校验是否为图片
try {
BufferedImage image = ImageIO.read(picFile);
if (image == null) {
return "";
}
} catch (IOException ex) {
ex.printStackTrace();
return "";
}
// 4、转换成base64编码
String imageStr = " ";
try {
byte[] data = null;
InputStream in = new FileInputStream(path);
data = new byte[in.available()];
in.read(data);
BASE64Encoder encoder = new BASE64Encoder();
imageStr = encoder.encode(data);
} catch (Exception e) {
imageStr = "";
e.printStackTrace();
}
return imageStr;
}
到此完工。