Java freemarker 生成word文档
生活随笔
收集整理的這篇文章主要介紹了
Java freemarker 生成word文档
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
工具類
?
package cn.gh.util;import freemarker.template.Configuration; import freemarker.template.Template; import sun.misc.BASE64Encoder;import java.io.*; import java.util.HashMap; import java.util.Map;/*** Created by guo on 2018/3/15.*/ public class WordGenerator {private static Configuration configuration = null;private static Map<String, Template> allTemplates = null;static {configuration = new Configuration();configuration.setDefaultEncoding("utf-8");configuration.setClassForTemplateLoading(WordGenerator.class, "/cn/gh/util");allTemplates = new HashMap<>(); // Java 7 鉆石語法try {allTemplates.put("user", configuration.getTemplate("user.xml"));} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);}}private WordGenerator() {throw new AssertionError();}public static File createDoc(Map<?, ?> dataMap, String type) {String name = "temp" + (int) (Math.random() * 100000) + ".doc";File f = new File(name);Template t = allTemplates.get(type);try {// 這個(gè)地方不能使用FileWriter因?yàn)樾枰付ň幋a類型否則生成的Word文檔會(huì)因?yàn)橛袩o法識(shí)別的編碼而無法打開Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");t.process(dataMap, w);w.close();} catch (Exception ex) {ex.printStackTrace();throw new RuntimeException(ex);}return f;}//放置他圖片public static String getImageStr(String imageurl) {String imgFile = imageurl;InputStream in = null;byte[] data = null;try {in = new FileInputStream(imgFile);data = new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}BASE64Encoder encoder = new BASE64Encoder();//這里會(huì)報(bào)錯(cuò)return encoder.encode(data);}public static void main(String[] args) {String imageStr = getImageStr("G:/jpg/9.jpg");System.out.println(imageStr);} } ??
?
controller 類
?
?
@ResponseBody @RequestMapping("/getDoc") public Object getDoc( HttpServletResponse resp) throws IOException {Map<String,Object> map=new HashMap<String,Object>();map.put("id","1");map.put("name","guohua");map.put("nikename","shuaige");map.put("password","123456");map.put("state","kaixin");String imageStr = WordGenerator.getImageStr("G:/jpg/9.jpg");System.out.println(imageStr+"---------------");map.put("imageurl",imageStr);/* DocUtil docUtil=new DocUtil();docUtil.createDoc(map,"user","G:\\.xml");System.out.println("-----------------------");*/File file = null;InputStream fin = null;ServletOutputStream out = null;try {// 調(diào)用工具類WordGenerator的createDoc方法生成Word文檔file = WordGenerator.createDoc(map, "user");fin = new FileInputStream(file);/* resp.setCharacterEncoding("utf-8");resp.setContentType("application/msword");*/// 設(shè)置瀏覽器以下載的方式處理該文件默認(rèn)名為resume.docresp.addHeader("Content-Disposition", "attachment;filename=resume.doc");out = resp.getOutputStream();byte[] buffer = new byte[512]; // 緩沖區(qū)int bytesToRead = -1;// 通過循環(huán)將讀入的Word文件的內(nèi)容輸出到瀏覽器中while((bytesToRead = fin.read(buffer)) != -1) {out.write(buffer, 0, bytesToRead);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fin != null) fin.close();if(out != null) out.close();if(file != null) file.delete(); // 刪除臨時(shí)文件}return 1; }?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Java freemarker 生成word文档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: google黑客常用搜索语句
- 下一篇: 补丁冷启动模式_试验模式补丁说明