java生成word文档

这是工具类代码

package common.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

public class DocUtil {

private Configuration configure=null;

public DocUtil(){
configure=new Configuration();
configure.setDefaultEncoding("utf-8");
}

/**
* 根据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(), "testDoc");
//设置对象包装器
// configure.setObjectWrapper(new DefaultObjectWrapper());
//设置异常处理器
configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
//定义Template对象,注意模板类型名字与downloadType要一致
template=configure.getTemplate(downloadType+".xml");
File outFile=new File(savePath);
Writer out=null;
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
template.process(dataMap, out);
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}

这里模板路径为



public String getImageStr(String imgFile){
InputStream in=null;
byte[] data=null;
try {
in=new FileInputStream(imgFile);
data=new byte[in.available()];
in.read(data);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder=new BASE64Encoder();
return encoder.encode(data);
}

}

方法调用

@Override
public String exportWord(TeachMaterialDealInfo teachMaterialDealInfo,
String titleName) {
String s = UUID.randomUUID().toString();
String filepath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/");
String zippath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/week-job.zip");
String notice = ServletActionContext.getServletContext().getRealPath("upload/notice/");
createfile(notice);
createfile(filepath);
List<TeachMaterialDealInfo> teachMaterial = teachMaterialDealInfoDao.findDealInfo(teachMaterialDealInfo);
TeachMaterial teachMaterial1 = applicationDao.getMaterInfoByMaterId(teachMaterialDealInfo.getMaterid());
DocUtil docUtil=null;
Map<String, Object> dataMap=null;
....给dataMap赋值如dataMap.put("writername", “呵呵呵”);调用xml文件是就是通过这个来获取值得这里我用的是freemarker方法


docUtil.createDoc(dataMap, "basedoc", filepath+"/"+teachMaterial.get(i).getWritername()+".doc");


}

 xml文件为先写好word文件后转存为xml

 

原文地址:https://www.cnblogs.com/niuxi/p/7162036.html