poi导出word表格的操作讲解

一、效果如下

poi导出word表格的操作讲解

二、js代码

function export_word(){ //导出word
  var url = "czzsca/exportWord.do";
  this.export(url);
}
function export(url){
  var currentyear = $("#mainYear").val() * 1;
  var key_columns = ['xh',"d_name","d_unit","d_number","d_estimate","d_lastyear","jnjh","d_remarks","d_depart","d_executeunit"];
  var value_columns = ['序号','项目名称','单位','数量','总概算','至'+(currentyear-1)+'年完成',currentyear+'年计划','附注','责任部门','计划执行单位'];
  window.location.href= http://www.cppcns.com/ruanjian/java/url+"?key_columns="+key_columns+"&value_columns="+value_columns+"&title="+currentyear+"年 更新改造计划 正式计划草案";
}

三、controller代码

(controller代码比较长,主要是格式化每一行的数据,主要是往工具类里传List<List<String>>参数,泛型的List<String>是每一行,String是每一列)

 //导出word
 @RequestMapping("/exportWord.do")
 @ResponseBody
 public ReturnMsg exportWord(String[] key_columns,String[] value_columns,String title,HttpServletResponse response){
 ReturnMsg rm = new ReturnMsg();
 try {
  response.setCharacterEncoding("UTF-8");
     response.setContentType("application/msexcle");  
     response.setHeader("content-disposition", "attachment;filename="+new String("正式计划草案".getBytes("gb2312"),"ISO8859-1")+".doc");
     List<List<String>> content = new ArrayList<List<String>>();
     List<List<String>> head = new ArrayList<List<String>>();
     String jnjhzh = new BigDecimal(this.map.get("jnjhzh")==null||"".equals(this.map.get("jnjhzh")+"")?"0":this.map.get("jnjhzh")+"").setScale(1, RoundingMode.UP)+"";
     String trimpro = new BigDecimal(this.map.get("tzjh")==null||"".equals(this.map.get("tzjh")+"")?"0":this.map.get("tzjh")+"").setScale(1, RoundingMode.UP)+"";
     String temp = new BigDecimal(trimpro).subtract(new BigDecimal(jnjhzh))+"";
     if("0".equals(temp)) {
     temp = "";
     }
     head = ExportUtil.getCzzscaList(jnjhzh, trimpro, temp);
     Map dounit = new HashMap();
     dounit.put("dwlx", 0);
     dounit.put("user_type", 1);
      List<Map> d_executeunit = unitDao.selectByPrimaryKey(dounit);
      Map zrbm = new HashMap();
      zrbm.put("dwlx", 1);
      zrbm.put("user_type", 1);
      List<Map> d_zrbm = unitDao.selectByPrimaryKey(zrbm);
      List<Map> zmtype = nd_caController.findProType("2-3-4");
     for (Map map: this.list) {
       List<String> row = new ArrayList<String>();
       for (int i = 0; i < key_columns.length; i++) {
       System.out.print(map.get(key_columns[i])+",");
       Object Otzjh = map.get("tzjh")!=null?map.get("tzjh"):0;
       Object Ojnjh = map.get("jnjh")!=null?map.get("jnjh"):0;
       BigDecimal tzjh = null;
       BigDecimal jnjh = null;
       if ("tzjh".equals(key_columns[i])||"jnjh".equals(key_columns[i])||"d_lnorde".equals(key_columns[i])) {
        if(Otzjh instanceof BigDecimal) {
          tzjh = (BigDecimal)Otzjh;
         }else {
     tzjh = new BigDecimal(Otzjh+"");
     }
         if(Ojnjh instanceof BigDecimal) {
          jnjh = (BigDecimal)Ojnjh;
         }else {
          jnjh = new BigDecimal(Ojnjh+"");
     }
         
       }
       if ("d_lnorde".equals(key_columns[i])) {
        if(map.get("flag")!=null&&"2".equals((map.get("flag")+""))) {
        row.add("");
        }else {
        row.add(tzjh.subtract(jnjh).setScale(1, RoundingMode.UP)+""); //不为整数则四舍五入
     }
   }else if ("xh".equals(key_columns[i])&&map.get("xh")!=null) {
        if(map.get("xh").toString().indexOf("、")>-1) {
        row.add(map.get("xh").toString().replace("、", "").trim());
        }else {
        row.add(map.get("xh")+""); //不为整数则四舍五入
     }
   }else if("tzjh".equals(key_columns[i])&&map.get("tzjh")!=null) { 
   if(ExportUtil.isNumeric(tzjh+"")) { //判断相减是否为整数
    row.add(tzjh+"");
      }else {
       String stzjh = tzjh.setScale(1, RoundingMode.UP)+"";
       row.add("0".equals(stzjh.charAt(stzjh.indexOf(".")+1)+"")?stzjh.replace(".0", ""):stzjh);//不为整数则四舍五入
   }
   }else if("d_estimate".equals(key_columns[i])&&map.get("d_estimate")!=null) { 
   if("0".equals(map.get("d_estimate")+"")) {
    row.add("");
   }else {
    row.add(map.get("d_estimate")+"");
   }
   }else if("d_number".equals(key_columns[i])&&map.get("d_number")!=null) { 
   if("0".equals(map.get("d_number")+"")) {
    row.add("");
   }else {
    row.add(map.get("d_number")+"");
   }
   }else if("jnjh".equals(key_columns[i])&&map.get("jnjh")!=null) { 
   if(ExportUtil.isNumeric(jnjh+"")) { //判断相减是否为整数
    row.add(jnjh+"");
      }else {
       String sjnjh = jnjh.setScale(1, RoundingMode.UP)+"";
       row.add("0".equals(sjnjh.charAt(sjnjh.indexOf(".")+1)+"")?sjnjh.replace(".0", ""):sjnjh); //不为整数则四舍五入
   }
   }else if("d_pro_type".equals(key_columns[i])&&map.get("d_pro_type")!=null) { 
   for (Map m : zmtype) {
    if((map.get("d_pro_type")+"").equals(m.get("TYPE_ID")+"")){
    row.add(m.get("NUM")+"");
    break;
    }
   }
       }else if("d_depart".equals(key_columns[i])&&map.get("d_depart")!=null) { 
        String d_depart = map.get("d_depart")+"";
        String depart = "";
        for (Map m : d_zrbm) {
        if((d_depart).indexOf(",")>-1) {
         for (String string : d_depart.split(",")) {
         if((string).equals(m.get("ID")+"")) {
          depart += ","+m.get("UNITNAME")+"";
          break;
           }
    }
        }else {
         if((d_depart).equals(m.get("ID")+"")) {
         depart = ","+m.get("UNITNAME")+"";
         break;
         }
        }
   }
        if (StringUtils.isNotBlank(depart)) {
        depart = depart.substring(1, depart.length()).replace(",", "\r ");
        row.add(depart);
   }else {
    row.add("");
   }
   }else if("d_executeunit".equals(key_columns[i])&&map.get("d_executeunit")!=null) { 
   boolean flag;
        for (Map m : d_executeunit) {
        flag = false;
        String executeunit = map.get("d_executeunit")+"";
        if((executeunit).indexOf(",")>-1) {
         for (String string : executeunit.split(",")) {
         if((string).equals(m.get("ID")+"")) {
          row.add((m.get("UNITNAME")+"").trim());
           flag = true;
           break;
           }
    }
         if (flag) {
         break;
    }
         
        }else {
         if((executeunit).equals(m.get("ID")+"")) {
         row.add((m.get("UNITNAME")+"").trim());
         break;
         }
        }
   }
       }else {
        if(map.get(key_columns[i])==null) {
        row.add("");
        }else {
        row.add((map.get(key_columns[i])+"").trim());
   }
   }
    }
       content.add(row);
  }
     head.addAll(content);
     int[] colWidths = new int[] { 600, 3000, 500, 500, 800, 800 ,800,3000,800,1500}; 
  XWPFDocument document = ExportUtil.exportWord(title,value_columns, head,colWidths,1,7);
  OutputStream out = response.getOutputStream(); 
     document.write(out);
     out.close();
     rm.setCode("1");
  rm.setMsg("导出成功");
 } catch (Exception e) {
  e.printStackTrace();
  rm.setCode("0");
  rm.setMsg("导出失败,请刷新重试。");
 }
 return rm;
 }

poi导出word表格的操作讲解

扫一扫手机访问