(一)功能描述:在智能終端的系統中,對全部學員的信息要生成帶有二維碼圖片的報到證與准考證的word文檔java
(二)實現過程:工具
1.建立報到證與准考證的word模板,在模板中填充相應的變量。this
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文檔 }
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(); } } }
/** * 處理圖片 * 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; } }