• java利用模板生成嵌套循环word


    //创建考场名单

    private void createKcmdWord(){
    List<OsipOstaExamarrange> list=examarrangeService.getByJdjhid(vo.getJdjhid());
    Map<String, Object> dataMap = new HashMap<String, Object>();
    List<Map<String, Object>> newsList=new ArrayList<Map<String,Object>>();
    OsipOstaJdjh jdjhbo=jdJhService.getOsipOstaJdjhAll(vo.getJdjhid());
    for (int i = 0; i < list.size(); i++) {
    Map<String, Object> map=new HashMap<String, Object>();
    map.put("fullname",list.get(i).getTestcentername());
    map.put("zygzname",list.get(i).getProfessionalworkname());
    map.put("jdrank",numToUpper(Integer.valueOf(list.get(i).getAppraisalrank())));
    map.put("kcbh",list.get(i).getExaminationnum());
    map.put("kcbh",list.get(i).getExaminationnum());

    List<Map<String, Object>> newsList1=new ArrayList<Map<String,Object>>();

    List<KsScoreListVO> ksScoreList=ksScoreService.getKsscoreByKcid(vo.getJdjhid(),list.get(i).getId(),list.get(i).getExaminationtype());

    for (KsScoreListVO ksScoreListVO : ksScoreList) {
    Map<String, Object> map1=new HashMap<String, Object>();
    if(list.get(i).getExaminationtype().equals("1")){
    map1.put("zwh", ksScoreListVO.getLlkczwh());
    }else if(list.get(i).getExaminationtype().equals("2")){
    map1.put("zwh", ksScoreListVO.getZhkczwh());
    }else if(list.get(i).getExaminationtype().equals("3")){
    map1.put("zwh", ksScoreListVO.getSckczwh());
    }else if(list.get(i).getExaminationtype().equals("4")){
    map1.put("zwh", ksScoreListVO.getWykczwh());
    }
    map1.put("zkznum", ksScoreListVO.getZkznum());
    map1.put("stuname", ksScoreListVO.getStuname());
    map1.put("idno", ksScoreListVO.getCredennum());

    if(!jdjhbo.getJdrank().equals("1")&&!jdjhbo.getJdrank().equals("2")){
    map1.put("zh","/");
    }else{
    map1.put("zh"," ");
    }

    if(!jdjhbo.getZygznum().equals("3-02")){
    map1.put("wy","/");
    }else{
    map1.put("wy"," ");
    }

    newsList1.add(map1);

    }
    map.put("dataList", newsList1);

    newsList.add(map);


    }

    dataMap.put("newsList",newsList);

    /** 组装数据 */

    //文件名称
    try {
    fileName= new String(new String(("考场名单_"+vo.getJdjh()).getBytes("GBK"),"ISO-8859-1"))+".doc";
    InputStream fis=WordUtil.createWord1(dataMap, "kcmd.ftl", "222.doc");
    this.inputStream=fis;
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    try {
    inputStream.close();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    }

    public static InputStream createWord1(Map dataMap,String templateName,String fileName){
    Writer out=null;
    try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //创建配置实例
    Configuration configuration = new Configuration();

    //设置编码
    configuration.setDefaultEncoding("UTF-8");

    //ftl模板文件统一放下面
    configuration.setClassForTemplateLoading(WordUtil.class, "/com/wondersgroup/pub/util/");
    //获取模板
    Template template = configuration.getTemplate(templateName);

    //将模板和数据模型合并生成文件
    out = new BufferedWriter(new OutputStreamWriter(baos,"UTF-8"));
    //生成文件
    template.process(dataMap, out);

    byte[] ba = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(ba);
    //关闭流
    out.flush();
    out.close();
    baos.close();
    return bais;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }finally{
    try {
    out.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }

    注意word模板的制作,需要通过另存为.xml文件 需要装填数据的地方用 ${name}、 ${gender} 表示

  • 相关阅读:
    ReactiveCocoa RACObserve subscribeNext 时,只有值不一样时才响应
    ReactiveCocoa 监听Enabled和添加Command出错的处理方法
    Masonry + UIView Animations 注意事项
    addObserver forKeyPath options 注意事项
    ios中tabbar得title和navigationbar的title如何修改
    tableview 分组显示返回footerviewt和headerView的高度不能为0的问题
    UITableViewCell的选中时的颜色设置
    ios 枚举 位移操作
    设置UIButton 字体 颜色
    jsoup 源码分析
  • 原文地址:https://www.cnblogs.com/wanghongwei123/p/7098373.html
Copyright © 2020-2023  润新知