package com;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class Word {

	private Configuration configuration = null;

	public Word() {
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
	}

	public void createDoc() {

		// 要填入模本的数据文件
		Map dataMap = new HashMap();
		getData(dataMap);

		// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
		// ftl文件存放路径
		configuration.setClassForTemplateLoading(this.getClass(), "/com/ftl");

		Template t = null;
		try {
			// test.ftl为要装载的模板
			t = configuration.getTemplate("人员情况.ftl");
			t.setEncoding("utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 输出文档路径及名称
		File outFile = new File("F:/人员情况New.doc");
		Writer out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}

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

	}

	/**
	 * 
	 * 注意dataMap里存放的数据Key值要与模板中的参数相对应
	 * @param dataMap
	 */
	private void getData(Map dataMap) {
		dataMap.put("name", "唐钰小宝");
		dataMap.put("sex", "男");
		dataMap.put("nationality", "中国");
		dataMap.put("usedName", "唐钰小宝");
		dataMap.put("politicsStatus", "团员");
		dataMap.put("nation", "汉族");
		dataMap.put("post", "成员");
		dataMap.put("secretPost", "成员");
		dataMap.put("IDcard", "");
		dataMap.put("phone", "1300000000");
		dataMap.put("residenceAddrs", "陕西省西安市");
		dataMap.put("publicSecurityResidenceAddrs", "陕西省西安市");
		dataMap.put("residence", "陕西省西安市");
		dataMap.put("publicSecurityResidence", "陕西省西安市");
		dataMap.put("isGuard", "是");
		dataMap.put("dimissionTime", "1");
		dataMap.put("timeLimit", "2");
		dataMap.put("unit", "3");
		dataMap.put("carrier", "4");
		dataMap.put("computers", "5");
		dataMap.put("isRegain", "6");
		dataMap.put("isRegainJurisdiction", "7");
		dataMap.put("secretGrade", "非密");
		String ss="";
		try {
			ss = Word.getImageBase("C:\\Users\\Desktop\\index.jpg");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dataMap.put("image", ss );
		//<!-- 列表加载  -->
		List learnings = new ArrayList();
		List familys = new ArrayList();
		List abroads = new ArrayList();

		Learning l1 = new Learning();
		l1.setLearning("西安市第三中学");
		l1.setLearnStartTime("2020-06-02");
		l1.setLearnEndTime("2020-06-02");
		l1.setWitness("李逍遥");
		Learning l2 = new Learning();
		l2.setLearning("唐南中学");
		l2.setLearnStartTime("2020-06-02");
		l2.setLearnEndTime("2020-06-02");
		l2.setWitness("唐钰小宝");
		learnings.add(l1);
		learnings.add(l2);
		
		dataMap.put("learnings", learnings);
		
		Family family = new Family();
		family.setRelation("Relation111");
		family.setName("Name1111");
		family.setNationality("nationality111");
		family.setCompany("company111");
		family.setPermit("permit111");
		family.setPoliticalOutlook("politicalOutlook111");
		family.setPosition("position111");
		family.setRelation("relation1111");
		
		Family family1 = new Family();
		family1.setRelation("Relation22");
		family1.setName("Name22");
		family1.setNationality("nationality222");
		family1.setCompany("company222");
		family1.setPermit("permit222");
		family1.setPoliticalOutlook("politicalOutlook222");
		family1.setPosition("position222");
		family1.setRelation("relation222");
		
		
		familys.add(family);
		familys.add(family1);
		
		
		Abroad abroad1 = new  Abroad();
		abroad1.setStartTime("setStartTime1111");
		abroad1.setEndTime("setEndTime111");
		abroad1.setCountry("setCountry111");
		abroad1.setCause("setCause111");
		Abroad abroad = new  Abroad();
		abroad.setStartTime("setStartTime2222");
		abroad.setEndTime("setEndTime22");
		abroad.setCountry("setCountry22");
		abroad.setCause("setCause222");
		
		
		
		abroads.add(abroad);
		abroads.add(abroad1);
		dataMap.put("familys", learnings);
		dataMap.put("familys", familys);
		dataMap.put("abroads", abroads);
		
		dataMap.put("holdingCertificatesLicenses", "是");
		dataMap.put("holdingCertificatesNum", "123456789123456789");
		dataMap.put("storageCertificatesLicenses", "保管情况");
		dataMap.put("HongKongCertificatesLicenses", "是");
		dataMap.put("HongKongCertificatesNum", "123456789123456789");
		dataMap.put("HongKongstorageCertificatesLicenses", "保管情况");
		dataMap.put("taiWanCertificatesLicenses", "是");
		dataMap.put("taiWanCertificatesNum", "123456789123456789");
		dataMap.put("taiWanstorageCertificatesLicenses", "保管情况");
		dataMap.put("majorEventReport", "1");
		dataMap.put("whetherCensorship", "是");
		
		
	}

	public static void main(String[] args) {
		System.out.println("--------------加载完成前--------------");
		new Word().createDoc();
		System.out.println("--------------加载完成后--------------");
	}

	
	//获得图片的base64码
   public static String getImageBase(String src) throws Exception {
		if (src == null || src == "") {
			return "";
		}
		File file = new File(src);
		if (!file.exists()) {
			return "";
		}
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(file);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
   }
}
最近下载更多
1358849392  LV21 2023年10月12日
RrrReNax  LV3 2023年7月18日
tinashui2  LV1 2023年5月19日
yangdoudou  LV1 2022年9月21日
yinyun1985  LV14 2022年4月11日
xuyongff  LV24 2021年11月25日
x2b2d2  LV12 2021年8月17日
123456nty  LV37 2021年8月10日
ericxu1116  LV24 2021年5月3日
lz88888  LV11 2021年4月29日
最近浏览更多
quartz  LV7 3月13日
qiuqiu233 2023年10月17日
暂无贡献等级
1358849392  LV21 2023年10月10日
randian  LV1 2023年8月30日
RrrReNax  LV3 2023年7月18日
tinashui2  LV1 2023年5月19日
IT达人  LV22 2023年2月16日
jirui_zz  LV12 2023年1月20日
微信网友_6191697646571520  LV6 2022年10月31日
and123456  LV11 2022年10月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友