zoukankan      html  css  js  c++  java
  • 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} 表示

  • 相关阅读:
    libevent源码学习之event
    游戏寻路A*算法
    游戏地图动态生成
    一个基于protocol buffer的RPC实现
    TCMalloc源码学习(四)(小内存块释放)
    TCMalloc源码学习(三)(小块内存分配)
    TCMalloc源码学习(二)
    第五十四篇 Linux相关——远程连接SSH
    第五十三篇 Linux相关——Web服务器
    第五十二篇 Linux相关——数据库服务MySQL
  • 原文地址:https://www.cnblogs.com/wanghongwei123/p/7098373.html
Copyright © 2011-2022 走看看