栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 根据已有word模板进行数据下载导出

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 根据已有word模板进行数据下载导出

将已有word模板转化为freemarker模板进行数据导出
  • 一 将现有wrod模板中的数据进行调整并另存为xml格式文件
  • java 代码编写

一 将现有wrod模板中的数据进行调整并另存为xml格式文件

使用freemarker模板 语法进行数据填充的调整

然后将文件另存为 xml 格式文件.
将另存为的文件内容进行在线格式化一下(百度搜索)
再将后缀修改为.flt(freemarker文件)
![在这里插入图片描述](https://img-blog.csdnimg.cn/cd901475f24648c9a051243b424ac965.png

java 代码编写
    public void downloadAuthenticationNotice(HttpServletResponse res, HttpServletRequest request,Long id) throws IOException {
    	// 获取向模板中写入数据对象
        PrtPhProjectDTO projectDTO = prtPhProjectService.get(id);
        String authenticationNotice = projectDTO.getAuthenticationNotice();

        // json 转 TransactionInfoDTO
        TransactionInfoDTO parse = prtPhProjectService.jsonToTransactionInfo(authenticationNotice);
        // 字典数据匹配
        this.manageDictData(parse);
        
		// 模板名称
        String templateName = "test.ftl";
        
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");

        OutputStream os = res.getOutputStream();
        String  fileName = projectDTO.getProjectName() + "测试文件.doc";
        Template t = null;
        ByteArrayOutputStream bos = null;
        Writer out = null;
        try {
            Map dataMap=new HashMap();
            dataMap.put("data",parse); //放入数据
            // 获取模板
            configuration.setClassForTemplateLoading(this.getClass(), "/templates");
            t = configuration.getTemplate(templateName);
            // 向模板中写数据
            bos = new ByteArrayOutputStream();
            out = new BufferedWriter(new OutputStreamWriter(bos));
            t.process(dataMap,out);
            // 根据模板生成 word数据流  获取数据字节数组  
            byte[] byteArray = bos.toByteArray();
			// 下载文件
            res.reset();
            if ("IE".equals(getBrowser(request))) {
                fileName = new String(java.net.URLEncoder.encode(fileName, "UTF-8"));
                res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            }else{
                fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
                res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            }
            res.setContentType("application/octet-stream; charset=utf-8");
            os.write(byteArray);
            os.flush();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
                bos.close();
                out.close();
            }
        }
    }


    private String getBrowser(HttpServletRequest request) {
        String UserAgent = request.getHeader("User-Agent").toLowerCase();
        if (UserAgent.indexOf("firefox") >= 0){
            return "FF";
        }else if(UserAgent.indexOf("safari") >= 0 ){
            return "Chrome";
        }else{
            return "IE";
        }
    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996264.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号