1. 程式人生 > >java生成word文檔

java生成word文檔

efault tex ring wrap imp pub uuid import roc

這是工具類代碼

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

技術分享

技術分享

技術分享

java生成word文檔