前言
最近做了一個調查問卷導出的功能,需求是將維護的題目,答案,導出成word,參考了幾種方案之后,選擇功能強大的freemarker+固定格式之后的wordxml實現導出功能。導出word的代碼是可以直接復用的,於是在此貼出,並進行總結,方便大家拿走。
實現過程概覽
先在word上,調整好自己想要的樣子。然后存為xml文件。保存為freemarker模板,以ftl后綴結尾。將需要替換的變量使用freemarker的語法進行替換。最終將數據准備好,和模板進行渲染,生成文件並返回給瀏覽器流。
詳細的實現過程
准備好word的樣式
我們新建一個word,我們應該使用Microsoft office,如果使用wps可能會造成樣式有些不兼容。在新建的office中,設置好我們的表格樣式。我們的調查問卷涉及到四種類型,單選,多選,填空,簡答。我們做出四種類型的示例。
樣式沒有問題后,我們選擇另存為word xml 2003版本。將會生成一個xml文件。
格式化xml,並用freemarker語法替換xml
我們可以先下載一個工具 firstobject xml editor,這個可以幫助我們查看xml,同時方便我們定位我們需要改的位置。
復制過去之后,按f8可以將其進行格式化,左側是標簽,右側是內容,我們只需要關注w:body即可。
像右側的調查問卷這個就是個標題,我們實際渲染的時候應該將其進行替換,比如我們的程序數據map中,有title屬性,我們想要這里展示,我們就使用語法${title}即可。
freemarker的具體語法,可以參考freemarker的問題,在這里我給出幾個簡單的例子。
比如我們將所有的數據放置在dataList中,所以我們需要判斷,dataList是不是空,是空,我們不應該進行下面的邏輯,不是空,我們應該先循環題目是必須的,答案是需要根據類型進行再次循環的。語法參考文檔,這里不再贅述。
程序端引入freemarker
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
將我們的flt文件放在resources下的templates下。
后端代碼實現
此代碼可以復用,在此貼出
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:導出word,傳入request,response,map就是值,title是導出問卷名,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:創建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;
}
}
有了工具類后,我們准備好我們的map數據。map里面的數據大家可以自行定義。然后調用utils中的導出方法即可。
WordUtils.exportWord(request, response, dataMap, "word", "demo.ftl");
結語
至此已經結束了,十分的好用,有疑問的話,可以評論交流。