日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java freemarker 生成word文档

發布時間:2024/3/13 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java freemarker 生成word文档 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工具類

?

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 {// 這個地方不能使用FileWriter因為需要指定編碼類型否則生成的Word文檔會因為有無法識別的編碼而無法打開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();//這里會報錯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 {// 調用工具類WordGenerator的createDoc方法生成Word文檔file = WordGenerator.createDoc(map, "user");fin = new FileInputStream(file);/* resp.setCharacterEncoding("utf-8");resp.setContentType("application/msword");*/// 設置瀏覽器以下載的方式處理該文件默認名為resume.docresp.addHeader("Content-Disposition", "attachment;filename=resume.doc");out = resp.getOutputStream();byte[] buffer = new byte[512]; // 緩沖區int bytesToRead = -1;// 通過循環將讀入的Word文件的內容輸出到瀏覽器中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(); // 刪除臨時文件}return 1; }

?

?

?

?

總結

以上是生活随笔為你收集整理的Java freemarker 生成word文档的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。