freemarker生成Word文档

package app.movittech.hr.inprove;

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.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

import com.eweaver.base.BaseContext;
import com.eweaver.base.BaseJdbcDao;
import com.eweaver.util.StringHelper;

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

public class WordTemplete {

    BaseJdbcDao baseJdbcDao = (BaseJdbcDao) BaseContext.getBean("baseJdbcDao");

    private Configuration configuration = null;

    public WordTemplete() {
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
    }

    /**
     * 在职证明(不含薪资)
     */
    private static final String INCOME_PROVE_TYPE_001 = "2c90839157b1be440157b251aa9c08ab";
    /**
     * 收入证明
     */
    private static final String INCOME_PROVE_TYPE_002 = "2c90839157b1be440157b251aa9c08ac";
    /**
     * 个人旅游签证证明
     */
    private static final String INCOME_PROVE_TYPE_003 = "2c90839157b1be440157b251aa9c08ad";
    /**
     * 因公出差签证证明
     */
    private static final String INCOME_PROVE_TYPE_004 = "2c90839157b1be440157b251aa9c08ae";

    /**
     * 在职证明(不含薪资)
     */
    private static final String PROVE_TYPE_001_CODE = "001";
    private static final String PROVE_TYPE_001 = "在职证明(不含薪资)";

    /**
     * 在职证明(不含薪资)_派遣员工_外服
     */
    private static final String PROVE_TYPE_001_PQ_WF_CODE = "001_WF";
    private static final String PROVE_TYPE_001_PQ_WF = "在职证明(不含薪资)_派遣员工_外服";

    /**
     * 在职证明(不含薪资)_派遣员工_爱森华
     */
    private static final String PROVE_TYPE_001_PQ_ASH_CODE = "001_ASH";
    private static final String PROVE_TYPE_001_PQ_ASH = "在职证明(不含薪资)_派遣员工_爱森华";

    /**
     * 收入证明
     */
    private static final String PROVE_TYPE_002_CODE = "002";
    private static final String PROVE_TYPE_002 = "收入证明";

    /**
     * 收入证明_派遣员工_外服
     */
    private static final String PROVE_TYPE_002_PQ_WF_CODE = "002_WF";
    private static final String PROVE_TYPE_002_PQ_WF = "收入证明_派遣员工_外服";

    /**
     * 收入证明_派遣员工_爱森华
     */
    private static final String PROVE_TYPE_002_PQ_ASH_CODE = "002_ASH";
    private static final String PROVE_TYPE_002_PQ_ASH = "收入证明_派遣员工_爱森华";

    /**
     * 个人旅游签证证明
     */
    private static final String PROVE_TYPE_003_CODE = "003";
    private static final String PROVE_TYPE_003 = "个人旅游签证证明";

    /**
     * 个人旅游签证证明_派遣员工_外服
     */
    private static final String PROVE_TYPE_003_PQ_WF_CODE = "003_WF";
    private static final String PROVE_TYPE_003_PQ_WF = "个人旅游签证证明_派遣员工_外服";

    /**
     * 因公出差签证证明
     */
    private static final String PROVE_TYPE_004_CODE = "004";
    private static final String PROVE_TYPE_004 = "因公出差签证证明";

    /**
     * 在职证明(不含薪资)模板
     */
    private static final String TEMPLATE_ZAIHI_PROVAE_FTL = "zaihiProve.ftl";
    /**
     * 在职证明(不含薪资)_派遣员工_外服模板
     */
    private static final String TEMPLATE_ZAIHI_PROVAE_WF_FTL = "zaihiProve_wf.ftl";
    /**
     * 在职证明(不含薪资)_派遣员工_爱森华模板
     */
    private static final String TEMPLATE_ZAIHI_PROVAE_ASH_FTL = "zaihiProve_ash.ftl";
    /**
     * 收入证明模板
     */
    private static final String TEMPLATE_INCOME_PROVE_FTL = "incomeProve.ftl";
    /**
     * 收入证明_派遣员工_外服模板
     */
    private static final String TEMPLATE_INCOME_PROVE_WF_FTL = "incomeProve_wf.ftl";
    /**
     * 收入证明_派遣员工_爱森华模板
     */
    private static final String TEMPLATE_INCOME_PROVE_ASH_FTL = "incomeProve_ash.ftl";
    /**
     * 个人旅游签证证明模板
     */
    private static final String TEMPLATE_GEREN_LV_YOU_FTL = "gerenLvYou.ftl";
    /**
     * 个人旅游签证证明_派遣员工_外服模板
     */
    private static final String TEMPLATE_GEREN_LV_YOU_WF_FTL = "gerenLvYou_wf.ftl";

    /**
     * 因公出差签证证明模板
     */
    private static final String TEMPLATE_YIN_GONG_FTL = "yinGongChuChai.ftl";

    /**
     * 文件后缀
     */
    private static final String FILE_SUFFIX_NAME = ".doc";



    /**
     * 根据模板生成word
     * 
     * @param destPath
     */
    @SuppressWarnings("rawtypes")
    public File createWord(String requestid) {
        HashMap dataMap = new HashMap();
        String templateType = getData(dataMap, requestid);
        System.out.println("在职收入模板类型:templateType:" + templateType + "***dataMap的数据:" + dataMap);

        String rootPath = this.getClass().getResource("").getPath();
        rootPath = rootPath.substring(1, rootPath.length());
        rootPath = rootPath.replaceAll("/", "\\\\\\\\");
        rootPath = rootPath.substring(0, rootPath.indexOf("\\WEB-INF"));
        rootPath = rootPath + "\\app\\\\incomeApprove\\\\";

        String template = "";
        if (templateType.equals(PROVE_TYPE_001_CODE)) {
            template = TEMPLATE_ZAIHI_PROVAE_FTL;
        } else if (templateType.equals(PROVE_TYPE_001_PQ_WF_CODE)) {
            template = TEMPLATE_ZAIHI_PROVAE_WF_FTL;
        } else if (templateType.equals(PROVE_TYPE_001_PQ_ASH_CODE)) {
            template = TEMPLATE_ZAIHI_PROVAE_ASH_FTL;
        } else if (templateType.equals(PROVE_TYPE_002_CODE)) {
            template = TEMPLATE_INCOME_PROVE_FTL;
        } else if (templateType.equals(PROVE_TYPE_002_PQ_WF_CODE)) {
            template = TEMPLATE_INCOME_PROVE_WF_FTL;
        } else if (templateType.equals(PROVE_TYPE_002_PQ_ASH_CODE)) {
            template = TEMPLATE_INCOME_PROVE_ASH_FTL;
        } else if (templateType.equals(PROVE_TYPE_003_CODE)) {
            template = TEMPLATE_GEREN_LV_YOU_FTL;
        } else if (templateType.equals(PROVE_TYPE_003_PQ_WF_CODE)) {
            template = TEMPLATE_GEREN_LV_YOU_WF_FTL;
        } else if (templateType.equals(PROVE_TYPE_004_CODE)) {
            template = TEMPLATE_YIN_GONG_FTL;
        }
        System.out.println("在职收入模板名称template:" + template);
        // 获得项目根目录
        rootPath = rootPath + getStringDate() + "\\\\";

        String fileName = "";
        if (templateType.equals(PROVE_TYPE_001_CODE)) {
            fileName = PROVE_TYPE_001;
        } else if (templateType.equals(PROVE_TYPE_001_PQ_WF_CODE)) {
            fileName = PROVE_TYPE_001_PQ_WF;
        } else if (templateType.equals(PROVE_TYPE_001_PQ_ASH_CODE)) {
            fileName = PROVE_TYPE_001_PQ_ASH;
        } else if (templateType.equals(PROVE_TYPE_002_CODE)) {
            fileName = PROVE_TYPE_002;
        } else if (templateType.equals(PROVE_TYPE_002_PQ_WF_CODE)) {
            fileName = PROVE_TYPE_002_PQ_WF;
        } else if (templateType.equals(PROVE_TYPE_002_PQ_ASH_CODE)) {
            fileName = PROVE_TYPE_002_PQ_ASH;
        } else if (templateType.equals(PROVE_TYPE_003_CODE)) {
            fileName = PROVE_TYPE_003;
        } else if (templateType.equals(PROVE_TYPE_003_PQ_WF_CODE)) {
            fileName = PROVE_TYPE_003_PQ_WF;
        } else if (templateType.equals(PROVE_TYPE_004_CODE)) {
            fileName = PROVE_TYPE_004;
        }
        String applyerObj = (String) dataMap.get("applyerObj");
        fileName = applyerObj + "_" + fileName;
        Random random = new Random();
        fileName = fileName + "_" + getStringDate2() + "_" + random.nextInt(100);
        System.out.println("在职收入模板的文件名字fileName:" + fileName);
        if (StringHelper.isEmpty(templateType)) {
            String errorPath = rootPath + "error.txt";
            return new File(errorPath);
        }
        File file = new File(rootPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        fileName = fileName + FILE_SUFFIX_NAME;
        String destPath = rootPath + fileName;
        System.out.println("在职收入模板的文件最终输出路径destPath:" + destPath);
        File destFile = null;
        if (dataMap != null && dataMap.size() > 0) {
            if (!StringUtils.isNotBlank(template)) {
                String errorPath = rootPath + "error2.txt";
                destFile = new File(errorPath);
            } else {
                createWord(template, destPath, dataMap);
                destFile = new File(destPath);
            }
        } else {
            String errorPath = rootPath + "error.txt";
            destFile = new File(errorPath);
        }
        return destFile;
    }


    /**
     * 获取数据
     * 
     * @param dataMap
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    private String getData(HashMap dataMap, String requestid) {
        String sqlString = "select applyer,personneltype,contractsubject,provetype,passportnumber,tothecountry,dept from uf_income_prove where requestid = '" + requestid + "'";
        Map applyerMap = baseJdbcDao.executeForMap(sqlString);
        String templateType = "";
        System.out.println("在职收入的单子" + requestid + "的数据applyerMap:" + applyerMap);
        if (applyerMap.isEmpty() || applyerMap == null) {
            System.out.println("在职收入证明requestid数据为空:");
            return StringUtils.EMPTY;
        }
        String applyer = (String) applyerMap.get("applyer");
        String personneltype = (String) applyerMap.get("personneltype");// 人员类别
        String provetype = (String) applyerMap.get("provetype");// 证明类型
        String passportnumber = (String) applyerMap.get("passportnumber");// 护照号码
        String tothecountry = (String) applyerMap.get("tothecountry");// 前往国家
        // 查询员工信息
        StringBuffer sqlBuffer = new StringBuffer("select h.objname,h.col1,h.extdatefield0,h.exttextfield24,h.exttextfield34,h.exttextfield17,");
        sqlBuffer.append("s1.objname mainstation,");
        sqlBuffer.append("s2.objname contractsubject2,");
        sqlBuffer.append("s3.objname gender,");
        sqlBuffer.append("h.gender genderId,");
        sqlBuffer.append("ou.objname orgid");
        sqlBuffer.append(" from humres h");
        sqlBuffer.append(" left join stationinfo s1 on h.mainstation=s1.id");
        sqlBuffer.append(" left join selectitem s2 on h.exttextfield17=s2.id");
        sqlBuffer.append(" left join selectitem s3 on h.gender=s3.id");
        sqlBuffer.append(" left join orgunit ou on h.orgid=ou.id");
        sqlBuffer.append(" where h.id='");
        sqlBuffer.append(applyer);
        sqlBuffer.append("'");

        System.out.println("在职收入的sql:" + sqlBuffer.toString());
        List list = baseJdbcDao.executeSqlForList(sqlBuffer.toString());
        System.out.println("结果为:" + list);
        if (list == null || list.isEmpty()) {
            System.out.println("在职收入查询的结果集为空list:" + list);
            return StringUtils.EMPTY;
        }
        Map map = (Map) list.get(0);
        String apply = (String) map.get("objname");// 申请人
        dataMap.put("applyerObj", apply);
        String card = (String) map.get("col1");// 身份证
        String extdatefield0 = (String) map.get("extdatefield0");// 司龄开始日期
        String mainstation = (String) map.get("mainstation");// 职位
        String exttextfield17 = (String) map.get("exttextfield17");// 合同主体代码
        String contractsubject2 = (String) map.get("contractsubject2");// 合同主体中文
        String gender = (String) map.get("gender");// 性别
        String genderId = (String) map.get("genderId");// 性别ID
        String orgid = (String) map.get("orgid");// 部门
        dataMap.put("deptObj", orgid);
        String exttextfield24 = (String) map.get("exttextfield24");// 英文姓名
        String employeetype = "";// 员工类型
        String exttextfield34 = (String) map.get("exttextfield34");// 拼音姓名


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = format.format(new Date());



        if (provetype.equals(INCOME_PROVE_TYPE_001)) {// 在职证明(不含薪资)
            boolean isFlag = true;
            // 派遣员工
            if (personneltype.equals("2c9083b03826486d01382681497804b4") || personneltype.equals("2c9083b03826486d01382681497804b3")) {
                if (exttextfield17.equals("2c9083b0480d5d1e01481111a35b3e67")) {// 公司主体为外服
                    isFlag = false;
                    System.out.println("在职证明:派遣员工-外服******************************************");
                    templateType = PROVE_TYPE_001_PQ_WF_CODE;
                    dataMap.put("apply", apply);
                    dataMap.put("gender", gender);
                    dataMap.put("card", card);
                    dataMap.put("year", extdatefield0.split("-")[0]);
                    dataMap.put("month", extdatefield0.split("-")[1]);
                    dataMap.put("day", extdatefield0.split("-")[2]);
                    dataMap.put("mainstation", mainstation);
                    dataMap.put("year2", currentDate.split("-")[0]);
                    dataMap.put("month2", currentDate.split("-")[1]);
                    dataMap.put("day2", currentDate.split("-")[2]);
                } else if (exttextfield17.equals("2c9083b0480d5d1e01481111a35b3e6a")) {// 爱森华
                    isFlag = false;
                    System.out.println("在职证明:派遣员工-爱森华******************************************");
                    templateType = PROVE_TYPE_001_PQ_ASH_CODE;
                    dataMap.put("apply", apply);
                    dataMap.put("card", card);
                    dataMap.put("dept", orgid);
                    dataMap.put("mainstation", mainstation);
                    dataMap.put("year", currentDate.split("-")[0]);
                    dataMap.put("month", currentDate.split("-")[1]);
                    dataMap.put("day", currentDate.split("-")[2]);
                }
            }
            if (isFlag) {// 通用模板
                templateType = PROVE_TYPE_001_CODE;
                System.out.println("在职证明******************************************");
                dataMap.put("apply", apply);
                dataMap.put("card", card);
                dataMap.put("year", extdatefield0.split("-")[0]);
                dataMap.put("month", extdatefield0.split("-")[1]);
                dataMap.put("day", extdatefield0.split("-")[2]);
                dataMap.put("mainstation", mainstation);
                dataMap.put("contractsubject", contractsubject2);
                dataMap.put("date", currentDate);
            }
        } else if (provetype.equals(INCOME_PROVE_TYPE_002)) {// 收入证明
            boolean isFlag = true;
            // 派遣员工
            if (personneltype.equals("2c9083b03826486d01382681497804b4") || personneltype.equals("2c9083b03826486d01382681497804b3")) {
                if (exttextfield17.equals("2c9083b0480d5d1e01481111a35b3e67")) {// 公司主体为外服
                    isFlag = false;
                    System.out.println("收入证明:派遣员工-外服******************************************");
                    templateType = PROVE_TYPE_002_PQ_WF_CODE;
                    dataMap.put("apply", apply);
                    dataMap.put("card", card);
                    dataMap.put("year", extdatefield0.split("-")[0]);
                    dataMap.put("month", extdatefield0.split("-")[1]);
                    dataMap.put("day", extdatefield0.split("-")[2]);
                    dataMap.put("year2", currentDate.split("-")[0]);
                    dataMap.put("month2", currentDate.split("-")[1]);
                    dataMap.put("day2", currentDate.split("-")[2]);
                } else if (exttextfield17.equals("2c9083b0480d5d1e01481111a35b3e6a")) {// 爱森华
                    isFlag = false;
                    System.out.println("收入证明:派遣员工-爱森华******************************************");
                    templateType = PROVE_TYPE_002_PQ_ASH_CODE;
                    dataMap.put("apply", apply);
                    dataMap.put("apply2", apply);
                    dataMap.put("dept", orgid);
                    dataMap.put("mainstation", mainstation);
                    dataMap.put("yearNum", getYears(extdatefield0));
                    dataMap.put("year", currentDate.split("-")[0]);
                    dataMap.put("month", currentDate.split("-")[1]);
                    dataMap.put("day", currentDate.split("-")[2]);
                }
            }
            if (isFlag) {// 通用模板
                templateType = PROVE_TYPE_002_CODE;
                System.out.println("收入证明******************************************");
                dataMap.put("apply", apply);
                dataMap.put("card", card);
                dataMap.put("year", extdatefield0.split("-")[0]);
                dataMap.put("month", extdatefield0.split("-")[1]);
                dataMap.put("day", extdatefield0.split("-")[2]);
                dataMap.put("mainstation", mainstation);
                dataMap.put("contractsubject", contractsubject2);
                dataMap.put("date", currentDate);
            }
        } else if (provetype.equals(INCOME_PROVE_TYPE_003)) {// 个人旅游签证证明
            boolean isFlag = true;
            // 派遣员工
            if (personneltype.equals("2c9083b03826486d01382681497804b4") || personneltype.equals("2c9083b03826486d01382681497804b3")) {
                if (exttextfield17.equals("2c9083b0480d5d1e01481111a35b3e67")) {// 公司主体为外服
                    isFlag = false;
                    System.out.println("个人旅游签证证明:派遣员工-外服******************************************");
                    templateType = PROVE_TYPE_003_PQ_WF_CODE;
                    dataMap.put("apply", apply);
                    dataMap.put("gender", gender);
                    dataMap.put("year", getBirthdayByCard(card).split("-")[0]);
                    dataMap.put("month", getBirthdayByCard(card).split("-")[1]);
                    dataMap.put("day", getBirthdayByCard(card).split("-")[2]);
                    dataMap.put("card", card);
                    dataMap.put("passportnumber", passportnumber);
                    dataMap.put("mainstation", mainstation);
                    dataMap.put("year2", extdatefield0.split("-")[0]);
                    dataMap.put("month2", extdatefield0.split("-")[1]);
                    dataMap.put("day2", extdatefield0.split("-")[2]);
                    dataMap.put("tothecountry", tothecountry);
                    dataMap.put("year3", currentDate.split("-")[0]);
                    dataMap.put("month3", currentDate.split("-")[1]);
                    dataMap.put("day3", currentDate.split("-")[2]);
                    dataMap.put("englishName", exttextfield34);
                    dataMap.put("card2", card);
                    dataMap.put("passportnumber2", passportnumber);
                    dataMap.put("birthday", getBirthdayByCard(card));
                    dataMap.put("since", extdatefield0);
                    dataMap.put("englishname2", exttextfield34);
                    dataMap.put("englishName3", exttextfield34);
                    if (genderId.equals("402881e90cba854b010cba9c9364000c")) {// 男
                        dataMap.put("gender1", "Sir");
                        dataMap.put("gender2", "Mr.");
                        dataMap.put("gender3", "male");
                        dataMap.put("gender4", "he");
                        dataMap.put("gender5", "His");
                        dataMap.put("gender6", "Mr.");
                        dataMap.put("gender7", "his");
                        dataMap.put("gender8", "himself");
                        dataMap.put("gender9", "his");
                        dataMap.put("gender10", "he");
                        dataMap.put("gender11", "he");
                    } else if (genderId.equals("402881e90cba854b010cba9c9364000d")) {// 女
                        dataMap.put("gender1", "Madam");
                        dataMap.put("gender2", "Ms.");
                        dataMap.put("gender3", "female");
                        dataMap.put("gender4", "she");
                        dataMap.put("gender5", "Her");
                        dataMap.put("gender6", "Ms.");
                        dataMap.put("gender7", "her");
                        dataMap.put("gender8", "herself");
                        dataMap.put("gender9", "her");
                        dataMap.put("gender10", "she");
                        dataMap.put("gender11", "she");
                    } else {
                        dataMap.put("gender1", "Sir/Madam");
                        dataMap.put("gender2", "Mr./Ms.");
                        dataMap.put("gender3", "male/female");
                        dataMap.put("gender4", "he/she");
                        dataMap.put("gender5", "His/Her");
                        dataMap.put("gender6", "Mr./Ms.");
                        dataMap.put("gender7", "his/her");
                        dataMap.put("gender8", "himself/herself");
                        dataMap.put("gender9", "his/her");
                        dataMap.put("gender10", "he/she");
                        dataMap.put("gender11", "he/she");
                    }
                }
            }
            if (isFlag) {// 通用模板
                templateType = PROVE_TYPE_003_CODE;
                System.out.println("个人旅游签证证明******************************************");
                dataMap.put("apply", exttextfield34);
                dataMap.put("passportnumber", passportnumber);
                dataMap.put("mainstation", mainstation);
                dataMap.put("since", extdatefield0);
                dataMap.put("tothecountry", tothecountry);
                dataMap.put("contractsubject", contractsubject2);
                dataMap.put("date", currentDate);
                if (genderId.equals("402881e90cba854b010cba9c9364000c")) {// 男
                    dataMap.put("gender", "his");
                    dataMap.put("gender2", "his");
                    dataMap.put("gender3", "his");
                    dataMap.put("gender4", "himself");
                    dataMap.put("gender5", "his");
                } else if (genderId.equals("402881e90cba854b010cba9c9364000d")) {// 女
                    dataMap.put("gender", "her");
                    dataMap.put("gender2", "her");
                    dataMap.put("gender3", "her");
                    dataMap.put("gender4", "herself");
                    dataMap.put("gender5", "her");
                } else {
                    dataMap.put("gender", "his/her");
                    dataMap.put("gender2", "his/her");
                    dataMap.put("gender3", "his/her");
                    dataMap.put("gender4", "himself/herself");
                    dataMap.put("gender5", "his/her");
                }
            }
        } else if (provetype.equals(INCOME_PROVE_TYPE_004)) {// 因公出差签证证明
            System.out.println("因公出差签证证明******************************************");
            templateType = PROVE_TYPE_004_CODE;
            dataMap.put("passportnumber", passportnumber);
            dataMap.put("since", extdatefield0);
            dataMap.put("pingyinname", exttextfield34);
            dataMap.put("pingyinname2", exttextfield34);
            dataMap.put("mainstation", mainstation);
            dataMap.put("tothecountry", tothecountry);
            dataMap.put("contractsubject", contractsubject2);
            dataMap.put("date", currentDate);
            if (genderId.equals("402881e90cba854b010cba9c9364000c")) {// 男
                dataMap.put("gender", "his");
                dataMap.put("gender2", "he");
                dataMap.put("gender3", "he");
                dataMap.put("gender4", "his");
                dataMap.put("gender5", "he");
                dataMap.put("gender6", "He");
            } else if (genderId.equals("402881e90cba854b010cba9c9364000d")) {// 女
                dataMap.put("gender", "her");
                dataMap.put("gender2", "she");
                dataMap.put("gender3", "she");
                dataMap.put("gender4", "her");
                dataMap.put("gender5", "she");
                dataMap.put("gender6", "She");
            } else {
                dataMap.put("gender", "his/her");
                dataMap.put("gender2", "he/she");
                dataMap.put("gender3", "he/she");
                dataMap.put("gender4", "his/her");
                dataMap.put("gender5", "he/she");
                dataMap.put("gender6", "He/She");
            }
        }
        System.out.println("组装数据完毕templateType:" + templateType);
        return templateType;
    }



    /**
     * 删除前一天的所有生成的文件 定时器
     * 
     * @param requestid
     */
    public void deleteAllFile() {
        // 获取项目的根目录
        String rootPath = this.getClass().getResource("").getPath();
        rootPath = rootPath.substring(1, rootPath.length());
        rootPath = rootPath.replaceAll("/", "\\\\\\\\");
        rootPath = rootPath.substring(0, rootPath.indexOf("\\WEB-INF"));

        rootPath = rootPath + "\\\\app\\\\incomeApprove\\\\";
        Date date = getNextDay(new Date());
        String deleteFilePath = rootPath + getStringDate(date) + "\\\\";
        System.out.println("在职收入删除文件路径deleteFilePath:" + deleteFilePath);
        File file = new File(deleteFilePath);
        if (file.isDirectory()) {
            String[] files = file.list();
            for (int i = 0; i < files.length; i++) {
                File file2 = new File(deleteFilePath + files[i]);
                if (file2.isFile()) {
                    file2.delete();
                }
            }
            file.delete();
        }
    }

    public void createWord(String templateName, String outPutDocPath, HashMap dataMap) {
        configuration.setClassForTemplateLoading(WordTemplete.class, "/app/movittech/hr/inprove/template"); // FTL文件所存在的位置
        Template t = null;
        try {
            t = configuration.getTemplate(templateName); // 文件名
        } catch (IOException e) {
            e.printStackTrace();
        }
        File outFile = new File(outPutDocPath);
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
        } catch (Exception e1) {
            e1.printStackTrace();
        }

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

    /**
     * 根据司龄开始日期计算工作多少年
     * 
     * @param time
     * @return
     */
    public String getYears(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = new Date();
        Date date2;
        double year = 0.00;
        try {
            date2 = format.parse(time);
            long day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);
            System.out.println("相隔的天数=" + day);
            double aa = (double) day;
            aa = aa / 365;
            BigDecimal bigDecimal = new BigDecimal(aa);
            year = bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(year);
    }


    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }


    /**
     * 根据身份证号码获取生日,格式yyyy-MM-dd
     * 
     * @param card
     * @return
     */
    public static String getBirthdayByCard(String card) {
        if (StringUtils.isNotEmpty(card)) {
            String year = StringUtils.substring(card, 6, 10);
            String month = StringUtils.substring(card, 10, 12);
            String day = StringUtils.substring(card, 12, 14);
            return year + "-" + month + "-" + day;
        }
        return StringUtils.EMPTY;
    }


    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringDate2() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_hhmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static void main(String[] args) throws Exception {
        Test test = new Test();
        String templateType = "";
        test.createWord(templateType);

    }

}

下面是freemarker初始化:

private Configuration configuration = null;

    public WordTemplete() {
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
    }

下面这个方法是根据doc模板来生成word:

templateName:模板的名字,包括后缀名,如1.doc

outPutDocPath:生成文件的全路径,包括文件名,如:D:\\1\2.doc

dataMap:是往模板中添加的数据,数据的格式就是普通的键值对,上面的代码有说明。

 public void createWord(String templateName, String outPutDocPath, HashMap dataMap) {
        configuration.setClassForTemplateLoading(WordTemplete.class, "/app/movittech/hr/inprove/template"); // FTL文件所存在的位置
        Template t = null;
        try {
            t = configuration.getTemplate(templateName); // 文件名
        } catch (IOException e) {
            e.printStackTrace();
        }
        File outFile = new File(outPutDocPath);
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
        } catch (Exception e1) {
            e1.printStackTrace();
        }

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

其中这一句是必不可少的,其中的UTF-8是必须的。如果没有这个,生成的doc是打不开的,会报xml格式错误,主要是对中文的支持:

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));






你可能感兴趣的:(java开发)