freemarker 的word模板

83961233 2019-12-17

开始

String    path        = "E:/file/";        // 文件地址
String    imagedir    = "E:\\images\\";    // 图片地址

    @SuppressWarnings("deprecation")
    public void printIssueOrderWord(List<Map<String, Object>> list) throws SQLException {

        // 要填入模本的数据文件
        Map<String, Object> dataMap = new HashMap<String, Object>();
        // 填写数据
        dataMap = getData(list);
        String id = String.valueOf(list.get(0).get("PKID"));
        // 输出文档
        File file = new File(path + "word/IssueOrderWord/" + id);
        // 如果文件夹不存在则创建
        if (!file.exists() && !file.isDirectory()) {
            System.out.println("//不存在");
            file.mkdirs();
        } else {
            System.out.println("//目录存在");
        }
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        String wordUrl = path + "word/IssueOrderWord/" + id + "/Word" + id + ".doc";
        try {
            // 加载需要装填的模板
            Template template = null;
            // 加载模板文件
            configuration.setClassForTemplateLoading(this.getClass(), "/freemarker");
            // 设置对象包装器
            configuration.setObjectWrapper(new DefaultObjectWrapper());
            // 设置异常处理器
            configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
            // 以utf-8的编码读取ftl文件
            template = configuration.getTemplate("Order.ftl", "utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wordUrl), "utf-8"), 10240);
            template.process(dataMap, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

设置数据

private Map<String, Object> getData(List<Map<String, Object>> list) throws SQLException {
        List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        int i = 1;
        for (Map<String, Object> obj : list) {// list为数据库查询出来的数据
            String po = "";
            String ship = "";
            String date = "";
            String buyer = "";
            String supplier = "";
            String a4 = "□";
            String a3 = "□";
            String a2 = "□";
            String a1 = "□";
            String phone = "";
            String fax = "";
            String memo = "";

            String type = isNull(obj.get("EXIGENCE_DEGREE"));
            if (type.equals("ROUTINE")) {
                a4 = "■";
            } else if (type.equals("EXPEDITE")) {
                a3 = "■";
            } else if (type.equals("CRITICAL")) {
                a2 = "■";
            } else if (type.equals("AOG")) {
                a1 = "■";
            }
            po = isNull(obj.get("ORDER_NO"));
            date = isNull(obj.get("ORDER_DATEP"));
            supplier = isNull(obj.get("VENDER_NAME")).replaceAll("&", "&amp;");
            ship = isNull(obj.get("RECV_ADDRES"));
            buyer = isNull(obj.get("ORDER_NAME"));
            phone = isNull(obj.get("PHONE"));
            fax = isNull(obj.get("FAX"));
            memo = isNull(obj.get("MEMO"));

            dataMap.put("number", po);
            dataMap.put("ship", ship);
            dataMap.put("date", date);
            dataMap.put("buyer", buyer);
            dataMap.put("phone", phone);
            dataMap.put("fax", fax);
            dataMap.put("memo", memo);

            dataMap.put("supplier", supplier);
            dataMap.put("a4", a4);
            dataMap.put("a3", a3);
            dataMap.put("a2", a2);
            dataMap.put("a1", a1);

            dataMap.put("image4", getImageStr4());
            dataMap.put("image5", getImageStr5());

            Map<String, Object> map = new HashMap<String, Object>();
                map.put("row1", i);
                map.put("row2", isNull(obj.get("PN")));
                String name = null;
                if (obj.get("CHNNAME") == null) {
                    name = isNull(obj.get("ENGNAME"));
                } else {
                    name = isNull(obj.get("CHNNAME"));
                }
                map.put("row3", name);
//            map.put("row4", "FN");
                map.put("row5", isNull(obj.get("ORDER_QTY")));
                map.put("row6", isNull(obj.get("UNIT")));
                String Price = "";
                if (obj.get("ORDER_PRICE") == null) {
                    Price = "0";
                } else {
                    Price = obj.get("ORDER_PRICE").toString() + obj.get("CURRENCY");
                }
                map.put("row7", Price);
                String Amount = "";
                if (obj.get("TOT_PRICE") == null) {
                    Amount = "0";
                } else {
                    Amount = obj.get("TOT_PRICE").toString() + obj.get("CURRENCY");
                }

                map.put("row8", Amount);
                newList.add(map);
                i++;
        }
        dataMap.put("newList", newList);
        return dataMap;
    }

调整参数

public String getImageStr4() {
        String imgFile = imagedir + "mm\\aokai.png";
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    public String getImageStr5() {
        String imgFile = imagedir + "mm\\title.png";
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    private String isNull(Object args) {
        if (args == null) {
            return "";
        } else if (args instanceof Date) {
            return new SimpleDateFormat("yyyy-MM-dd").format(args);
        } else {
            return args.toString();
        }
    }

flt模板可用word另存为xml 直接转flt 

相关推荐