java用freemarker实现导出word----包含图片

分为以下三个步骤:

1.先制作word模板

2.将该文档另存为 xml 文件

3.打开xml 文件

 

将对应的字段替换,比如

4.将xml文件保存成ftl格式的文档

5.相应的代码:

 1 package org.lq.ssm.gp.controller;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 import java.io.InputStream;
10 import java.io.OutputStreamWriter;
11 import java.io.Writer;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 
17 import sun.misc.BASE64Encoder;
18 
19 import freemarker.template.Configuration;
20 import freemarker.template.Template;
21 import freemarker.template.TemplateException;
22 
23 public class wordController {
24     
25      private Configuration configuration = null;  
26      
27         public wordController(){  
28             configuration = new Configuration();  
29             configuration.setDefaultEncoding("UTF-8");  
30         }  
31           
32         public static void main(String[] args) {  
33             wordController test = new wordController();  
34             test.createWord();  
35         }  
36           
37         public void createWord(){  
38             Map<String,Object> dataMap=new HashMap<String,Object>();  
39             getData(dataMap);  
40           
41             System.out.println(this.getClass().getResource("/util"));
42             configuration.setClassForTemplateLoading(this.getClass(), "/controller");  //FTL文件所存在的位置  
43             Template t=null;  
44             try {
45 
46                 t = configuration.getTemplate("baodan2.ftl"); //文件名
47             } catch (IOException e) {
48                 e.printStackTrace();  
49             }
50             File outFile = new File("H:/baodan/baodan.doc");  
51             Writer out = null;  
52             try {  
53                 out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));  
54             } catch (FileNotFoundException e1) {  
55                 e1.printStackTrace();  
56             }  
57                
58             try {  
59                 t.process(dataMap, out);  
60             } catch (TemplateException e) {  
61                 e.printStackTrace();  
62             } catch (IOException e) {  
63                 e.printStackTrace();  
64             }  
65         }  
66       
67         private void getData(Map<String, Object> dataMap) {  
68             
69               List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
70             dataMap.put("name", "");  
71             dataMap.put("username", "");  
72             dataMap.put("userzjh", "");  
73             dataMap.put("userQygs", "");  
74             dataMap.put("image", getImageStr());
75       
76         }  
77         private String getImageStr() {
78              String imgFile = "H:\\JT1.jpg";
79              
80              InputStream in = null;
81              byte[] data = null;
82              try {
83                  in = new FileInputStream(imgFile);
84                  data = new byte[in.available()];
85                  in.read(data);
86                  in.close();
87              } catch (IOException e) {
88                  e.printStackTrace();
89              }
90              BASE64Encoder encoder = new BASE64Encoder();        
91              return encoder.encode(data);
92          }
93      
94 }

 

posted @ 2017-06-15 14:35  xiaotian_小天  Views(2559)  Comments(0Edit  收藏  举报