• freemarker 的word模板


    开始

    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 

        @Override
        public Map<String, Object> printIssueOrderWord(ConsignIssueOrderMaster consignIssueOrderMaster)  throws Exception {
            return issueOrderWord.getData(consignIssueOrderMaster);
    
        }
    View Code
    @Autowired
        UploadFileService uploadFileService;
        @Autowired
        DictionaryService dictionaryService;
        @Autowired
        IssueOrderMasterMapper issueOrderMasterMapper;
        @Autowired
        IssueOrderDetailMapper issueOrderDetailMapper;
        
        String    path        = "E:/file/";        // 文件地址
        String    imagedir    = "E:\images\";    // 图片地址
    
    
    
        public Map<String, Object> getData(ConsignIssueOrderMaster consignIssueOrderMaster) throws Exception {
            List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
            Map<String, Object> dataMap = new HashMap<String, Object>();
            HashMap<String, Object> params = new HashMap<>();
            params.put("master", consignIssueOrderMaster);
            List<ConsignIssueOrderMaster> masterList = issueOrderMasterMapper.selectIssueOrderList(params);
            List<ConsignIssueOrderDetail> detailList = issueOrderDetailMapper.selectDetail(consignIssueOrderMaster);
    
            int i = 1;
                String po = "";
                String ship = "";
                String date = "";
                String buyer = "";
                String supplier = "";
                String a4 = "";
                String a3 = "";
                String a2 = "";
                String a1 = "";
                String phone = "";
                String fax = "";
                String memo = "";
                for (ConsignIssueOrderMaster obj : masterList) {// list为数据库查询出来的数据
    
                String type = isNull(obj.getExigenceDegree());
                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.getOrderNo());
                date = isNull(obj.getOrderDate());//"ORDER_DATEP"
                supplier = "11";//isNull(obj.getVenderChnname().replaceAll("&", "&amp;"));//get("VENDER_NAME")).replaceAll("&", "&amp;");
                ship = isNull(obj.getName());//get("RECV_ADDRES"));
                buyer = isNull(obj.getOrderMan());//get("ORDER_NAME"));
                phone = isNull(obj.getPhone());//("PHONE"));
                fax = isNull(obj.getFax());//("FAX"));
                memo = isNull(obj.getMemo());//("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());
                for (ConsignIssueOrderDetail obj : detailList) {// list为数据库查询出来的数据
                Map<String, Object> map = new HashMap<String, Object>();
                    map.put("row1", i);
                    map.put("row2", isNull(obj.getPn()));//("PN")));
                    String name = null;
                    if (obj.getChnname() == null) {//("CHNNAME") == null) {
                        name = isNull(obj.getEngname());//("ENGNAME"));
                    } else {
                        name = isNull(obj.getChnname());//("CHNNAME"));
                    }
                    map.put("row3", name);
    //            map.put("row4", "FN");
                    map.put("row5", isNull(obj.getOrderQty()));//("ORDER_QTY")));
                    map.put("row6", isNull(obj.getUnit()));//("UNIT")));
                    String Price = "";
                    if (obj.getOrderPrice() == null) {//("ORDER_PRICE") == null) {
                        Price = "0";
                    } else {
                        Price = obj.getOrderPrice().toString() + obj.getCurrency();//("ORDER_PRICE").toString() + obj.get("CURRENCY");
                    }
                    map.put("row7", Price);
                    String Amount = "";
                    if (obj.getTotPrice() == null) {//("TOT_PRICE") == null) {
                        Amount = "0";
                    } else {
                        Amount = obj.getTotPrice().toString() + obj.getCurrency();//("TOT_PRICE").toString() + obj.get("CURRENCY");
                    }
    
                    map.put("row8", Amount);
                    newList.add(map);
                    i++;
            }
            dataMap.put("newList", newList);
            return dataMap;
        }
    View Code
        @RequestMapping("/printIssueOrderWord")
        public void printIssueOrderWord(ConsignIssueOrderMaster consignIssueOrderMaster, HttpServletResponse response)throws Exception  {
            String templateName = null;
    //        Integer pkid = consignIssueOrderMaster.getPkid();
            // 1、获取模版中所需要的送修合同数据
            Map<String, Object> issueDataMap = null;
            issueDataMap = issueOrderService.printIssueOrderWord(consignIssueOrderMaster);
            templateName = "order.ftl";
    
            // 2、模版配制及加载
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
            cfg.setDefaultEncoding("utf-8");
            cfg.setClassForTemplateLoading(this.getClass(), "/template/consign/");
            Template template = cfg.getTemplate(templateName, "utf-8");
    
            // 3、利用http协议下载
            response.setContentType("application/msword");
            response.addHeader("Content-Disposition", "attachment;filename=" + consignIssueOrderMaster.getOrderNo() + ".doc");
            ServletOutputStream outputStream = response.getOutputStream();
            Writer out = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
            template.process(issueDataMap, out);
            out.close();
        }
    View Code
     
  • 相关阅读:
    2017《Java技术》预备作业 计科1501 杨柳
    Java技术预备作业02 计科1501杨柳
    H2O.ai初步使用
    Vue.Js加入bootstrap及jquery,或加入其他插件vueresource,vuex等
    初次使用git上传代码(转)
    svg绘图工具raphael.js的使用
    EF6添加mysql的edmx实体时报错:无法生成模型:“System.Data.StrongTypingException: 表“TableDetails”中列“IsPrimaryKey”的值为 DBNull
    在window下搭建Vue.Js开发环境
    SQL Server: 索引碎片产生及修复
    Windows注册表(regedit.exe)
  • 原文地址:https://www.cnblogs.com/lingcheng7777/p/12055254.html
Copyright © 2020-2023  润新知