转:Java生成带有二维码图片的word文档

(一)功能描述:在智能终端的系统中,对所有学员的信息要生成带有二维码图片的报到证与准考证的word文档

(二)实现过程:

                  1.创建报到证与准考证的word模板,在模板中填充相应的变量。

              

                  2.该模板中存在图片,所以在制作模板中应先在相应的位置放一张图片,调整好大小,起到站位的作用
                  1.创建报到证与准考证的word模板,在模板中填充相应的变量。
                  2.在该模板中存在图片,所以在制作模板中应先在相应的位置放一张图片,调整好大小,起到
                  
                  3.将创建好的模板另存为.xml格式的文件
                  3.此时的.xml格式的文件在图片的位置就会出现base64位图片代码,将此代码删掉,换成相应的变量,保存
                  4.在项目中创建template.ftl,将xml中的代码粘贴过去。
                  4.在该功能中需要用到freemarker-2.3.13.jar架包
                  5.编写后台代码。
                  6.模板样式如下图所示:
word模板样式
 
 
 
 
 
 
 
 
(三)后台主要代码实现过程:
1.查询出报到证与准考证上所要展示的信息,将查询的图片调用工具类HandlImage里面的getImageStr方法,将图片转为base64码的代码,将查询到的数据放到list中,下面是部分代码:
List list=this.documentService.printProofByMid();//获取报到证与准考证的信息放到list中
 for(int i=0;i<list.size();i++){//通过遍历list,获取list中图片的路径
    User u=(User) list.get(i);
    User uu=new User();
   filePath=request.getRealPath("image2/" + u.getBak1() + "_" + u.getName() + ".png");//获取图片的路径
    uu.setName(u.getName());
    ..........//其他属性同样
    uu.setImage(HandlImage.getImageStr(filePath));//调用HandlImage中的getImageStr方法,将图片转为base64码
    addList.add(uu);//将对象添加到addList中
  }
Map<String,Object> map=new HashMap<String,Object>();
  if( addList.size()>0){ //对addList进行非空的判断 
  map.put("TED", addList); //将addList放到map中
   DocExportUtils.downLoadDoc(request,map);//调用DocExportUtils下的downLoadDoc方法,生成word文档
  }
2.下面的方法就是生成word文档的方法,下面是此方法的全部代码:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class DocExportUtils {
 private Configuration configuration = null;
 public DocExportUtils() {
  configuration = new Configuration();
  configuration.setDefaultEncoding("utf-8");
 }
 public static void downLoadDoc(HttpServletRequest request,Map map){
  Configuration configuration = new Configuration();
  configuration.setDefaultEncoding("utf-8");
  configuration.setClassForTemplateLoading(DocExportUtils.class, "/com/ecoinfo/terminal/template");//红色部分存放freemarker模板的路径
  Template t=null;
  try {
   t = configuration.getTemplate("template.ftl");// //template.ftl为要装载的模板
  } catch (IOException e) {
   e.printStackTrace();
  }
  String name=(String)map.get("title");  //生成word文档的名称
  File outFile = new File(request.getRealPath("Word/" + name + ".doc"));// 输出文档路径
  Writer out = null;
  try {
    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
  } catch (FileNotFoundException e1) {
   e1.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     try {
   t.process(map, out);
  } catch (TemplateException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

 

(四)在生成word中使用到的工具方法:
描述:在模板中存在图片,所以要将图片转为base64码的图片,只要将图片所存放的路径传到getImageStr中,通过下面方法就可以生成base64码的图片代码,下面就是转为base64码的工具方法的全部代码:
/**
 * 处理图片
 * HandlImage类,用来将图片转为base64码
 */ 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.ecoinfo.commons.security.BASE64Encoder;
public class HandlImage{
 
 public static String getImageStr(String filepath){//filepath图片所存放的路径
 
  InputStream in =null;
  try {
   in=new FileInputStream(filepath);
  } catch (FileNotFoundException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
 if(!"".equals(in)&&in!=null){
  byte[]data=null;
   try{
   data=new byte[in.available()];
   in.read(data);
   in.close();
   }catch(IOException e){
    e.printStackTrace();}
   BASE64Encoder encoder=new BASE64Encoder();
   return encoder.encode(data);
 }else{
  return null;
 }
}

 

(五)注意事项:
1.在模板中,对于图片的list的循环,要将循环图片的位置的代码替换为变量的形式,否则,在word文档中出现的图片都是第一张图片,具体修改如下所示:
修改图片具体方法

 
 
 
 
 
 
2.在freemarker中有些特殊的字符是需要转义一下才可以实现,在下面列出需要转义字符的方法代码:
<#if ted.Name??>${((ted.Name?replace("&","&amp;"))?replace("<","&lt;"))?replace(">","&gt;")}</#if>

转载于:https://www.cnblogs.com/spp0152/p/3969926.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值