cy
2022-06-23 b83c40548208609d0d6826be13d742c28a784806
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cn.ksource.core.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.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
 
import javax.servlet.http.HttpServletRequest;
 
import cn.ksource.core.util.FreeMarkerUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
 
public class WordUtil {
 
    public String createWord(ContractMsg msg,String path,HttpServletRequest request,int template){  
        String result = new String();
        Writer out = null;
        try {
            Configuration configuration = FreeMarkerUtil.getConfiguration(this.getClass(), "/cn/ksource/core/doc");
            
            String templateName = new String();
            if(template==1) {
                templateName = "fqTemplate.ftl";
            } else {
                templateName = "qeTemplate.ftl";
            }
            Template t = configuration.getTemplate(templateName); //文件名  
            
            String savePath = request.getSession().getServletContext().getRealPath(path) + File.separator;
            //检查目录
            File uploadDir = new File(savePath);
            if(!uploadDir.exists()){
                uploadDir.mkdirs();
            }
            
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String newFileName = df.format(new Date()) + "_" + new Random().nextInt(10000) + ".doc";
            
            String newFilePath = savePath + newFileName;
            
            File outFile = new File(newFilePath);  
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8")); 
            
            
            Map dataMap = getData(msg);
            
            t.process(dataMap, out);  
            
            if (path.endsWith("/")) {
                result = path + newFileName;
            } else {
                result = path + "/" + newFileName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
 
    private Map getData(ContractMsg msg) {
        Map dataMap = new HashMap();
        HeadMsg headMsg = msg.getMsg();
        dataMap.put("msr", headMsg.getMsr());
        dataMap.put("htbh", headMsg.getHtbh());
        dataMap.put("cmr", headMsg.getCmr());
        dataMap.put("htqddd", headMsg.getHtqddd());
        dataMap.put("qdrq", headMsg.getQdrq());
        
        List<GoodsMsg> goods = msg.getGoods();
        dataMap.put("goods", goods);
        
        PayMsg payMsg = msg.getPayMsg();
        dataMap.put("qefkfs", payMsg.getQefkfs());
        dataMap.put("yfkbl", payMsg.getYfkbl());
        dataMap.put("yfkfs", payMsg.getYfkfs());
        dataMap.put("fkkbl", payMsg.getFkkbl());
        dataMap.put("fhlfs", payMsg.getFhlfs());
        dataMap.put("dhkbl", payMsg.getDhkbl());
        dataMap.put("dhkfs", payMsg.getDhkfs());
        dataMap.put("yskbl", payMsg.getYskbl());
        dataMap.put("yskfs", payMsg.getYskfs());
        dataMap.put("zbkbl", payMsg.getZbkbl());
        dataMap.put("zbkfs", payMsg.getZbkfs());
        dataMap.put("total", msg.getTotal());
        return dataMap;
    }  
    
}