zoukankan      html  css  js  c++  java
  • Java利用freemaker和(excelXML表格或wordXML表格),导出自己任何想要格式的文档

    做管理系统比较爱用,还在像以前用html化报表,还像以前那样用DIV固定格式,固定填充数据的位置,

    1、写好excel或者word

    像这样:  中间的单元格 你可以随便填一些字母或者中文,这个是方便找它的位置,像我这样是因为我的JAVA代码封装了map,我可以通过这样去取值

    2、将这文件另存为xml表(.xml)的格式,然后将文件扔在你的action包中,或者其他文件夹下。

    3、这时候的文件还是.xml格式的文件,但是我们需要的freemaker的文件格式,所以你需要重命名将它的后缀改成.ftl的。

    改好后,你可以打开它了,还记得图片上的我写了很多那种el表达式吗,找着你模板中写了这些表达式的位置:像这样

    4 这时候  我们的模板算是基本完成了。那么接下来就要去写方法,然后取出你要填写到单元格的数据了。如果是WORD也是类似的,不用再像以前那样用HTML画DIV了。

    5 首先写好实现类的方法

    6 加载数据到模板中。利用freemaker的list循环将数据填充到你需要的位置,如果是单条数据填充word 是不需要循环。(只是我的部分代码)

    @RequestMapping(value="/downloadzcexcel.action")
    public void zcExcelImport(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    String fileName=null;
    DocumentHandler handler=null;
    String tempBasePath=null;
    String tempName=null;
    request.setCharacterEncoding("utf-8");
    Map<String, Object> zcdata = new HashMap<String, Object>();
    String sbzg=request.getParameter("sbzg");
    String bbmc=request.getParameter("bbmc");
    String year=request.getParameter("year");
    String excelType=request.getParameter("excelType");
    String xkzType=request.getParameter("xkzType");
    bbmc=java.net.URLDecoder.decode(bbmc,"UTF-8");//参数解码
    sbzg=java.net.URLDecoder.decode(sbzg,"UTF-8");
    String pid=request.getParameter("pid");
    excelType=java.net.URLDecoder.decode(excelType,"UTF-8"); //报表类型
    if("评委会投票表".equals(excelType)){
    xkzType=java.net.URLDecoder.decode(xkzType,"UTF-8");
    tempBasePath="/com/daqsoft/titlemgmt/Action";
    tempName="zcpwhtp.ftl";
    handler=new DocumentHandler(tempBasePath);
    List<Map> list=zcExcelService.getExcelByPwh(sbzg, xkzType,year);
    zcdata.put("rows", list);
    zcdata.put("BBMC", bbmc);
    zcdata.put("XKZ", xkzType);
    zcdata.put("COUNT", list.size());
    }else{
    if("706".equals(pid)){
    tempBasePath="/com/daqsoft/titlemgmt/Action"; //资格评审、名册报表(转系列)
    tempName="zcExcelByZxl.ftl";
    handler=new DocumentHandler(tempBasePath);
    List<Map> list=zcExcelService.getSbzgByZc(sbzg,year);
    zcdata.put("rows", list);
    zcdata.put("BBMC", bbmc);
    zcdata.put("COUNT", list.size());
    }else{
    tempBasePath="/com/daqsoft/titlemgmt/Action"; //资格评审、名册报表(正常的)
    tempName="zcexcel.ftl";
    handler=new DocumentHandler(tempBasePath);
    List<Map> list=zcExcelService.getSbzgByZc(sbzg,year);
    zcdata.put("rows", list);
    zcdata.put("BBMC", bbmc);
    zcdata.put("COUNT", list.size());
    }
    }
    File file = null;
    InputStream inputStream = null;
    ServletOutputStream outServletOutputStream = null;
    try {
    file=handler.createDoc(zcdata, fileName, tempBasePath, tempName);
    inputStream=new FileInputStream(file);
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/msexcel");
    response.addHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(bbmc,"utf-8")+".xls");
    outServletOutputStream=response.getOutputStream();
    byte[] buffer = new byte[512];
    int bytesToRead = -1;
    while ((bytesToRead = inputStream.read(buffer)) != -1) {
    outServletOutputStream.write(buffer, 0, bytesToRead);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally{

    if (inputStream != null)
    inputStream.close();
    if (outServletOutputStream != null)
    outServletOutputStream.close();
    if (file != null)
    file.delete(); // 删除临时文件
    }
    }

    7 下面是需要用到一个工具类,和数据流的输出是一样的

    public File createDoc(Map<String,Object> dataMap,String fileName,String tempBasePath,String tempName) throws UnsupportedEncodingException {
    //dataMap 要填入模本的数据文件
    //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
    //这里我们的模板是放在template包下面
    //tempBasePath:'/com/daqsoft/hrmanage/action'
    //tempName : 'gbddTemp.ftl'


    fileName=fileName!=null?fileName:"temp" + (int) (Math.random() * 100000);
    Template t=null;
    try {
    //test.ftl为要装载的模板
    t = configuration.getTemplate(tempName);
    } catch (IOException e) {
    e.printStackTrace();
    }
    //输出文档路径及名称
    File outFile = new File(fileName);
    Writer out = null;
    try {
    out=new OutputStreamWriter(new FileOutputStream(outFile), "utf-8");
    t.process(dataMap, out);
    out.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println("------------完成文件封装---------------");
    return outFile;




    }

  • 相关阅读:
    iperf使用
    性能工具--vtune
    Android广告轮播图实现
    自定义控件学习——下拉刷新ListView
    JVM读书笔记
    自定义控件学习——仿qq侧滑栏
    初步使用RecyclerView实现瀑布流
    自定义Toast
    自定义对话框加状态选择器
    自定义组合控件
  • 原文地址:https://www.cnblogs.com/qaz110/p/4936544.html
Copyright © 2011-2022 走看看