基于Freemarker和xml實(shí)現(xiàn)Java導(dǎo)出word
前言
最近做了一個調(diào)查問卷導(dǎo)出的功能,需求是將維護(hù)的題目,答案,導(dǎo)出成word,參考了幾種方案之后,選擇功能強(qiáng)大的freemarker+固定格式之后的wordxml實(shí)現(xiàn)導(dǎo)出功能。導(dǎo)出word的代碼是可以直接復(fù)用的,于是在此貼出,并進(jìn)行總結(jié),方便大家拿走。
實(shí)現(xiàn)過程概覽
先在word上,調(diào)整好自己想要的樣子。然后存為xml文件。保存為freemarker模板,以ftl后綴結(jié)尾。將需要替換的變量使用freemarker的語法進(jìn)行替換。最終將數(shù)據(jù)準(zhǔn)備好,和模板進(jìn)行渲染,生成文件并返回給瀏覽器流。
詳細(xì)的實(shí)現(xiàn)過程準(zhǔn)備好word的樣式
我們新建一個word,我們應(yīng)該使用Microsoft office,如果使用wps可能會造成樣式有些不兼容。在新建的office中,設(shè)置好我們的表格樣式。我們的調(diào)查問卷涉及到四種類型,單選,多選,填空,簡答。我們做出四種類型的示例。
樣式?jīng)]有問題后,我們選擇另存為word xml 2003版本。將會生成一個xml文件。
格式化xml,并用freemarker語法替換xml
我們可以先下載一個工具 firstobject xml editor,這個可以幫助我們查看xml,同時方便我們定位我們需要改的位置。復(fù)制過去之后,按f8可以將其進(jìn)行格式化,左側(cè)是標(biāo)簽,右側(cè)是內(nèi)容,我們只需要關(guān)注w:body即可。
像右側(cè)的調(diào)查問卷這個就是個標(biāo)題,我們實(shí)際渲染的時候應(yīng)該將其進(jìn)行替換,比如我們的程序數(shù)據(jù)map中,有title屬性,我們想要這里展示,我們就使用語法${title}即可。
freemarker的具體語法,可以參考freemarker的問題,在這里我給出幾個簡單的例子。
比如我們將所有的數(shù)據(jù)放置在dataList中,所以我們需要判斷,dataList是不是空,是空,我們不應(yīng)該進(jìn)行下面的邏輯,不是空,我們應(yīng)該先循環(huán)題目是必須的,答案是需要根據(jù)類型進(jìn)行再次循環(huán)的。語法參考文檔,這里不再贅述。
程序端引入freemarker
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId></dependency>
將我們的flt文件放在resources下的templates下。
后端代碼實(shí)現(xiàn)
此代碼可以復(fù)用,在此貼出
public class WordUtils { private static Configuration configuration = null; private static final String templateFolder = WordUtils.class.getClassLoader().getResource('').getPath()+'/templates/word'; static { configuration = new Configuration(); configuration.setDefaultEncoding('utf-8'); try { configuration.setDirectoryForTemplateLoading(new File(templateFolder)); } catch (IOException e) { e.printStackTrace(); } } /** * @Description:導(dǎo)出word,傳入request,response,map就是值,title是導(dǎo)出問卷名,ftl是你要使用的模板名 */ public static void exportWord(HttpServletRequest request, HttpServletResponse response, Map map, String title, String ftlFile) throws Exception { Template freemarkerTemplate = configuration.getTemplate(ftlFile); File file = null; InputStream fin = null; ServletOutputStream out = null; try { file = createDocFile(map,freemarkerTemplate); fin = new FileInputStream(file); String fileName = title + '.doc';response.setCharacterEncoding('utf-8');response.setContentType('application/msword');response.setHeader('Content-Disposition', 'attachment;filename=' +fileName);out = response.getOutputStream(); byte[] buffer = new byte[512]; int bytesToRead = -1; while((bytesToRead = fin.read(buffer)) != -1) {out.write(buffer, 0, bytesToRead); } }finally { if(fin != null) fin.close(); if(out != null) out.close(); if(file != null) file.delete(); } } /** * @Description:創(chuàng)建doc文件 */ private static File createDocFile(Map<?, ?> dataMap, Template template) { File file = new File('init.doc'); try { Writer writer = new OutputStreamWriter(new FileOutputStream(file), 'utf-8'); template.process(dataMap, writer); writer.close(); } catch (Exception e) { e.printStackTrace(); } return file; }}
有了工具類后,我們準(zhǔn)備好我們的map數(shù)據(jù)。map里面的數(shù)據(jù)大家可以自行定義。然后調(diào)用utils中的導(dǎo)出方法即可。
WordUtils.exportWord(request, response, dataMap, 'word', 'demo.ftl');
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章: