欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

记录开发一个简历导出为Word格式的流程

程序员文章站 2024-01-14 19:50:46
...

1. 编写Word模板

记录开发一个简历导出为Word格式的流程

2. 另存为word2003xml格式

记录开发一个简历导出为Word格式的流程
记录开发一个简历导出为Word格式的流程

3. 打开xml文件并将要填入的内容替换为${xuehao}格式

文字替换记录开发一个简历导出为Word格式的流程
图片替换:先删掉图片很长一段字符码内容,然后替换为
记录开发一个简历导出为Word格式的流程
需要循环的地方,在循环开始时加<#list aaa_list as l> 结尾加</#list> 循环内容表示为${l.xuehao},修改后改文件为.ftl

4. 具体导出代码

package temp;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
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 WordTest {  

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

    public static void main(String[] args) {  
        WordTest test = new WordTest();  
        test.createWord();  
    }   
    public void createWord(){  
        Map<String,Object> dataMap=new HashMap<String,Object>();  
        getData(dataMap);  
        configuration.setClassForTemplateLoading(this.getClass(), "");//模板文件所在路径
        Template t=null;  
        try {  
            t = configuration.getTemplate("简历模板.ftl"); //获取模板文件
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        File outFile = new File("E:/2017工作/人力资源/简历"+Math.random()*10000+".doc"); //导出文件
        Writer out = null;  
        try {  
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));  
        } catch (FileNotFoundException e1) {  
            e1.printStackTrace();  
        } catch (Exception e){
            e.printStackTrace();
        }

        try {  
            t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件 
        } catch (TemplateException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  

    private void getData(Map<String, Object> dataMap) {  
        dataMap.put("name", "标题");  
        dataMap.put("age", "25");
        dataMap.put("imgdata", getImageStr("C:/Users/Public/Pictures/Sample Pictures/111.jpg"));


       List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();  
        for (int i = 0; i < 10; i++) {  
            Map<String,Object> map = new HashMap<String,Object>();  
            map.put("xuehao", i);  
            map.put("neirong", "内容"+i);  
            list.add(map);  
        }  


        dataMap.put("mylist", list);  
    } 
    //后台获取图片的base64编码方法:
    private String getImageStr(String imgFile) {

        InputStream in = null;
        byte[] data = null;
        try {
            if(imgFile.startsWith("http")){          //获取在线图片
                URL url = new URL(imgFile);
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5 * 1000);
                in = conn.getInputStream();
            }else{      //获取线下图片
                in = new FileInputStream(imgFile);
            }

            int count = 0;
            while (count == 0) {
                count = in.available();
            }
            data = new byte[count];
            int c;
            ByteArrayOutputStream buff = new ByteArrayOutputStream();
            while((c = in.read()) >= 0){
                buff.write(c);
            }
            data = buff.toByteArray();
            buff.close();
            in.read(data);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        if(data!=null && data.length>0){
            return encoder.encode(data);
        }
        return null;
    }
}

如果模板文件通过文件路径加载,而不是类路径,加载代码替换成以下
TemplateLoader templateLoader = null;
templateLoader = new FileTemplateLoader(new File(tPath));
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateLoader(templateLoader);
Template t = null;
try{
t = configuration.getTemplate("简历模板.ftl");//获取模板文件
}catch(IOException e){
e.printStackTrace();
}
File outFile = new File(myFileName);
Writer outW = null;
try{
outW = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
}catch(FileNotFoundException e1){
e1.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}

相关jar包:freemark.jar,rt.jar

相关标签: 导出word