java 写入word文档导出

方法体 

@RequestMapping("/jobapply/o_resume_download.do")
	public void resumeDownload(Integer id, HttpServletRequest request, HttpServletResponse response){
		CmsUserResume resume = cmsUserResumeMng.findById(id);//获取对象数据
		Map<String, Object> dataMap = new HashMap<String, Object>();//创建Map用于写入
		dataMap.put("resume", resume);
		if (StringUtils.isNotBlank(resume.getIcon()))
			dataMap.put("resumeImg",resume.getIcon());
		else {
			dataMap.put("resumeImg", null);
		}

		String educationJson = resume.getEducationJson();
		if(StringUtils.isNotBlank(educationJson)){
			List<Education> educationList = JsonUtil.jsonStrToList(educationJson, Education.class);
			if(educationList!=null && educationList.size()>0){
				dataMap.put("educationList", educationList);
			}
		}

		String workJson = resume.getWorkJson();
		if(StringUtils.isNotBlank(workJson)){
			List<Work> workList = JsonUtil.jsonStrToList(workJson, Work.class);
			if(workList!=null && workList.size()>0){
				dataMap.put("workList", workList);
			}
		}

		String familyJson = resume.getFamilyJson();
		if(StringUtils.isNotBlank(familyJson)){
			List<Family> familyList = JsonUtil.jsonStrToList(workJson, Family.class);
			if(familyList!=null && familyList.size()>0){
				dataMap.put("familyList", familyList);
			}
		}

		try{
			File file = ExportDoc.createDoc(realPathResolver.get("/tpl_res/upload/") + request.getSession().getId()+".doc", dataMap);//写入文件
			InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			response.reset();
			SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd"); 
			String ctime = formatter.format(resume.getSignedDate()); 
			String filename = resume.getName()+"-"+ctime+".doc";
			filename = new String(filename.getBytes("gbk"), "iso8859-1");

			response.setContentType("application/octet-stream");
			response.addHeader("Content-Type", "application/msword; charset=utf-8");  
			response.addHeader("Content-Disposition", "attachment;filename=" + filename);
			response.addHeader("Content-Length", String.valueOf(file.length()));

			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
			file.delete();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

写入类

package com.ezsite.doc;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import com.ezsite.cms.entity.main.CmsUserResume;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class ExportDoc {
    //word文档的XML格式名称
	private final static String MODEL_NAME = "docTemplate.xml";

	// 2个参数分别是:模板的名称,导出文件的路径
	public static File createDoc(String exportFilePath, Map<String, Object> dataMap) {
		Configuration configuration = new Configuration();
		configuration.setDefaultEncoding("UTF-8");
		Template t = null;
		// 1、导入模板
		configuration.setClassForTemplateLoading(ExportDoc.class, "");

		try {
			// test.ftl为要装载的模板
			t = configuration.getTemplate(MODEL_NAME);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 3、导出文件
		// 输出文档路径及名称
		File outFile = new File(exportFilePath);
		Writer out = null;
		
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			t.process(dataMap, out);
			out.flush();
			out.close();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return outFile;
	}

	public static void main(String[] args) {
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("resume",new CmsUserResume());
		ExportDoc.createDoc("D:\\jbodoc.doc", dataMap);
		
	}


}

 

转载于:https://my.oschina.net/u/3472220/blog/1502981

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值