Java word导出,可导出一份word或多页word内容

一步步按着写,图片看不清的,右键打开新标签页查看图片,谢谢

html

<a href="javascript:void(0)" class="easyui-linkbutton" plain="true" id="btn_customOpen">导出word</a>

前端js(ids为传递参数)

$.messager.confirm('温馨提示','确认打印选中用户信息!',function(r){
   if (r){
       window.location.href='artificialAccount/OpenAccountInfo.do?ids='+ids;
       $('#data_list').datagrid("reload");
     }
});

后台

@RequestMapping("/OpenAccountInfo.do")
@Logs(remark = "打印用户信息", type = LogsType.OTHER)
public void OpenAccountInfo(HttpServletRequest request,HttpServletResponse response,String ids) throws Exception {
    if (ids != null) {
        ids=ids.substring(0,ids.lastIndexOf(",")).replaceAll("\"","\\\"");
        if (ids!=null) {
            //根据ids,查询所有的导出word用户
            List<AccountOpenVo> customlist = customService.getOpenCustomInfo(ids);
            ***WordKit.genWord(customlist);***
        } 
        response.setContentType("application/force-download");// 设置强制下载不打开
        String fileName = java.net.URLEncoder.encode("用户信息", "UTF-8");
        response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName+".doc"));// 设置文件名
        OutputStream out = response.getOutputStream();
        out.write(FileUtils.readFileToByteArray(new File(WordKit.getTemPath())));
        out.flush();
        out.close();
    }

}

另外的引用的word方法(getWord)

package com.ewide.cloud.oc.cm.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ewide.cloud.oc.cm.vo.AccountOpenVo;
import com.google.common.collect.Lists;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class WordKit {

    public static Configuration cfg = null;

    public static void main(String[] args) throws Exception {
        System.out.println(WordKit.class.getClassLoader()
                .getResource("template").getPath());
        genWord();
    }

    //可测试打印试试
    public static void genWord() throws Exception {
        List<Map<String, Object>> lists = Lists.newArrayList();
        Map<String, Object> mybatisMap1 = new HashMap<String, Object>();
        mybatisMap1.put("cname", "测试1");
        Map<String, Object> mybatisMap2 = new HashMap<String, Object>();
        mybatisMap2.put("cname", "测试2");
        mybatisMap2.put("ccode", "测试2");
        mybatisMap2.put("contact", "lianx2");
        mybatisMap2.put("identitype", "as2");
        Map<String, Object> mybatisMap3 = new HashMap<String, Object>();
        mybatisMap3.put("cname", "测试2");
        mybatisMap3.put("ccode", "测试2");
        mybatisMap3.put("contact", "lianx2");
        mybatisMap3.put("identitype", "as2");
        Map<String, Object> mybatisMap4 = new HashMap<String, Object>();
        mybatisMap4.put("cname", "测试2");
        mybatisMap4.put("ccode", "测试2");
        mybatisMap4.put("contact", "lianx2");
        mybatisMap4.put("identitype", "as2");
        Map<String, Object> mybatisMap5 = new HashMap<String, Object>();
        mybatisMap5.put("cname", "测试2");
        mybatisMap5.put("ccode", "测试2");
        mybatisMap5.put("contact", "lianx2");
        mybatisMap5.put("identitype", "as2");
        lists.add(mybatisMap1);
        lists.add(mybatisMap2);
        lists.add(mybatisMap3);
        lists.add(mybatisMap4);
        lists.add(mybatisMap5);
        Map<String, Object> mybatisMap = new HashMap<String, Object>();
        mybatisMap.put("columns", lists);
        Writer mybatisOut = new OutputStreamWriter(
                new FileOutputStream("d://ewideconf//test.doc"), "utf-8");
        Template mybatisTemplate = cfg.getTemplate("account.ftl");
        mybatisTemplate.process(mybatisMap, mybatisOut);
    }

    static {
        cfg = new Configuration(
                Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        cfg.setDefaultEncoding("UTF-8");
        String filePath = WordKit.class.getClassLoader().getResource("template")
                .getPath();
        File templateFile = new File(filePath);
        try {
            cfg.setDirectoryForTemplateLoading(templateFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getTemPath() throws Exception {
        String filePath = WordKit.class.getClassLoader().getResource("template")
                .getPath();
        return filePath + File.separator + "tem.doc";
    }

    public static String getTemPath(String filePath) throws Exception {
        String path = WordKit.class.getClassLoader().getResource("template")
                .getPath() + File.separator + filePath;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return path;
    }

//lists需要导出的数据
    public static void genWord(List<AccountOpenVo> lists) throws Exception {
        Map<String, Object> mybatisMap = new HashMap<String, Object>();
        mybatisMap.put("columns", lists);
        //将word先存入临时文件
        Writer mybatisOut = new OutputStreamWriter(
                new FileOutputStream(WordKit.class.getClassLoader()
                        .getResource("template").getPath() + "/tem.doc"),
                "UTF-8");
        //account.ftl这个是你word的模板
        Template mybatisTemplate = cfg.getTemplate("**account.ftl**", "UTF-8");
        mybatisTemplate.process(mybatisMap, mybatisOut);
    }
}

word模板操作
第一步:
新建你需要打印的模板,且将要填入值的字段写入,如图1:

第二步:
1.文件保存未xml文件
这里写图片描述
2.保存成功后将后缀改为ftl
这里写图片描述
3.完成后打开此文件,找到模板的table开始的地方,就是“《w:tbl》《/w:tbl》”
这里写图片描述
4.在“《w:tbl》《/w:tbl》”加入《#list columns as obj》 《/list》
这里写图片描述
这里写图片描述
5.更改值,将obj加入
这里写图片描述
保存后将此模板放到项目template文件夹下
搞定,收工!!!!

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值