例子:
1、在word文档中设计好所需word风格,图片除外,要显示的值用${英文}代替;
2、设计好后,另存为xml格式,保存;
3、打开另存为的xml文件,图片是base64码,先把图片的base64码删除掉;
4、删掉之后在浏览器找一个在线xml格式化,更方面操作,格式化后把后缀改成ftl放到项目中resources下面,最好在resources下面创建一个文件件,我的叫Freemarker,然后放下面;
5、因为我要的数据不止一条可能有很多,那么就需要遍历;
6、下面上java代码;
maven依赖:
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
工具类:
import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Base64; import java.util.Base64.Encoder; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; /** * @Auther: zyx * @Date: 2018/8/7 11:12 */ public final class DocUtils { /** * create one word document and Write data to the freemarker template * * @param: [dataMap, path, fileName, ftlPath, ftlName] * @return: void * @author: zyx * @date: 2018/8/10 14:37 */ public static void createWord(Map<String, Object> dataMap, String path, String fileName, String ftlPath, String ftlName) { if (dataMap == null || dataMap.size() == 0) { return; } try { Configuration configuration = new Configuration(Configuration.VERSION_2_3_23); configuration.setDefaultEncoding("utf-8"); //Fixed physical hard drive location //configuration.setDirectoryForTemplateLoading(new File(path)); //project static resources under folder location configuration.setClassForTemplateLoading(DocUtils.class, ftlPath); //output document path and name File outFile = new File(path+ "/" + fileName + ".doc"); //utf-8 read ftl document Template t = configuration.getTemplate(ftlName, "utf-8"); Writer out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240); t.process(dataMap, out); out.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * get the images base64 code * * @param: [src] * @return: java.lang.String * @author: zyx * @date: 2018/8/10 14:36 */ public static String getImageBase(String src) { 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); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } Encoder encoder = Base64.getEncoder(); return encoder.encodeToString(data); } }
测试类:
import static word.DocUtils.createWord; import static word.DocUtils.getImageBase; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Auther: zyx * @Date: 2018/8/14 18:56 */ public class AlarmTest { public static void main(String[] args) { Map<String, Object> dataMap = handlerMap(); String path = "D:/Export"; String fileName = "test"; String ftlPath = "/Freemarker"; String ftlName = "alarm.ftl"; createWord(dataMap, path, fileName, ftlPath, ftlName); } private static Map<String, Object> handlerMap() { List<Map<String, Object>> list = new ArrayList<>(); for (int i = 0; i < 2; i++) { Map<String, Object> dataMap = new HashMap<>(); dataMap.put("cameraName", "hi看哈圣诞节佛诞节暗示法"); dataMap.put("score", "95.89%"); dataMap.put("inTime", "2018-08-14 19:04:56"); dataMap.put("currentTime", "2018-08-14 19:04:56"); dataMap.put("outTime", "2018-08-14 19:04:56"); dataMap.put("status", "待复核"); dataMap.put("sampleName", "王麻子"); dataMap.put("nation", "汉族"); dataMap.put("idNumber", "410326199408222312"); dataMap.put("personType", "国际总裁"); dataMap.put("libraryName", "好久好久"); dataMap.put("address", "美国纽约"); dataMap.put("detailAddress", "大佛山分公司"); dataMap.put("extractPath", getImageBase("D:/Export/1.png")); dataMap.put("samplePath", getImageBase("D:/Export/2.png")); dataMap.put("snapshotPath", getImageBase("D:/Export/3.png")); list.add(dataMap); } Map<String, Object> map = new HashMap<>(); map.put("recordList", list); return map; } }
运行结果:
作者:yaxin zhang