當前位置:編程學習大全網 - 網絡軟體 - 誰知道 用freemarker 導出word時,如何使導出word的格式,默認為doc格式

誰知道 用freemarker 導出word時,如何使導出word的格式,默認為doc格式

將要導出的Word另存為xml格式的文件,打開xml 在其中添加freemarker標簽,然後另存為outChairDoc.ftl文件.

第壹步要加入Freemarker.jar包。

Servlet代碼如下:在outChairDoc.ftl放在包cn.skyclass.jiaowu.freemarker.template下

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

configuration = new Configuration();

configuration.setDefaultEncoding("utf-8");

try {

createDoc(response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void createDoc(HttpServletResponse response) throws Exception {

// 要填入模本的數據文件

Map dataMap = new HashMap();

dataMap.put("docTitle", "fdfdfdfdfdfdf用戶信息");

List<ChairInfo> list = new ArrayList<ChairInfo>();

ChairInfo chairInfo = new ChairInfo();

chairInfo.setChairTitle("dfdfd");

chairInfo.setTeacherName("tea");

chairInfo.setStartTime(new Date());

chairInfo.setPlace("dfdfd");

list.add(chairInfo);

dataMap.put("chairInfoList", list);

// 設置模本裝置方法和路徑,FreeMarker支持多種模板裝載方法。可以重servlet,classpath,數據庫裝載,

// 這裏我們的模板是放在com.havenliu.document.template包下面

configuration.setClassForTemplateLoading(this.getClass(),

"/cn/skyclass/jiaowu/freemarker/template");

Template t = null;

try {

// test.ftl為要裝載的模板

t = configuration.getTemplate("outChairDoc.ftl");

t.setEncoding("utf-8");

} catch (IOException e) {

e.printStackTrace();

}

// 輸出文檔路徑及名稱

String fileName="講座列表.doc";

response.setContentType("application/msword");

response.addHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));//可防止導出的文件亂碼

response.setCharacterEncoding("utf-8");

PrintWriter out = response.getWriter();

t.process(dataMap, out);

out.close();

}

  • 上一篇:馬六明主要經歷
  • 下一篇:人臉識別是什麽?
  • copyright 2024編程學習大全網