freemaker导出word

freemaker导出word首先要新建一个word模板,另存为xml格式,再将扩展名改为.ftl

判断替换的内容是否为空${Year!''}

在w:tr标签加上list循环

<#list clzbjh as ApsMonthAdjustTech>

 </w:tr>

 </w:tr>

</#list>

 @RequestMapping("/build")
    public  RestResult build(HttpServletResponse response, HttpServletRequest request) throws Exception {
        boolean flag=false;
        Object  retObject=null;

        try {
            ApsMonthAdjust apsMonthAdjust=new ApsMonthAdjust();
            Map formMap = this.getParameterMap(request);
            String id = MapUtil.getString(formMap, "id");
            apsMonthAdjust.setId(id);
            HashMap resultMap = new HashMap<>();
           
     resultMap 里存入数据
Configuration configuration = new Configuration(); configuration.setDefaultEncoding( "utf-8"); try { String exportFile = this.getClass().getClassLoader().getResource( "template").getPath(); configuration.setDirectoryForTemplateLoading( new File(exportFile)); // File outFile = new File("F://test112221.doc"); // Template t = configuration.getTemplate("template11.ftl","utf-8"); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240); // Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240); // t.process(resultMap, out); // out.close(); Template t = null; t = configuration.getTemplate( "monthTemplate.ftl"); //文件名 t.setEncoding( "utf-8"); //ActionContext ctx = ActionContext.getContext(); //定义输出类型 response.reset(); response.setHeader( "Content-type", "application/msword;charset=ISO8859-1"); response.addHeader( "Pargam", "no-cache"); response.addHeader( "Cache-Control", "no-cache"); response.setHeader( "Content-disposition", "attachment; filename="+ new String((adYear+ "年"+adMonth+ "月计划.doc").getBytes(), "ISO8859-1")); response.setCharacterEncoding( "utf-8"); PrintWriter out = response.getWriter(); t.process(resultMap, out); out.close(); // Template t=null; // Writer out = null; // File outFile = null; // try { // t = configuration.getTemplate("monthTemplate.ftl"); // String name = "temp" + (int) (Math.random() * 100000) + ".doc"; // outFile = new File(name); // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8")); // t.process(resultMap, out); // } catch (Exception e) { // e.printStackTrace(); // } finally{ //finally下面的代码很重要 否则的话你导不出word 必须flush close 一下才行 // if(out!=null){ // try { // out.flush(); // out.close(); // } catch (Exception e2) { // e2.printStackTrace(); // } // } // } // //下面这些代码是为了你的 导出做准备的 // request.setCharacterEncoding("UTF-8"); // File file = null; // InputStream fin = null; // ServletOutputStream out1 = null; // try { // fin = new FileInputStream(outFile); // response.reset(); // response.setCharacterEncoding("utf-8"); // response.setContentType("application/msword"); // // 设置浏览器以下载的方式处理该文件默认名为Exhibition.doc // response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(adYear+"年"+adMonth+"月计划.doc" + ".doc", "UTF-8")); // out1 = response.getOutputStream(); // byte[] buffer = new byte[1024]; // 缓冲区 // int bytesToRead = -1; // // 通过循环将读入的Word文件的内容输出到浏览器中 // while ((bytesToRead = fin.read(buffer)) != -1) { // out1.write(buffer, 0, bytesToRead); // } // } finally { // if (fin != null){ // fin.close(); // } // if (out1 != null){ // out1.close(); // } // if (outFile != null){ // outFile.delete(); // 删除临时文件 // } // } } catch (Exception e){ e.printStackTrace(); } flag= true; } catch(Exception e){ e.printStackTrace(); retObject = "操作失败:"+e.getMessage(); } return renderResult(flag, flag? Ok: Fail, retObject); }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值