** 需要jar包 freemarker-2.3.8.jar
1、創建word模板:
首先使用 offic或wps(我用的就是wps) 創建一個 word.docx 文件
在文件中輸入 ${test} 並將文件保存爲 xml 格式,其中 ${test} 就是我們在程序中要替換的目標,重點是 ${} 這個符號,裏面的字內容可以自己定義。
2、保存爲 ftl 格式
將word.xml 直接重命名爲 word.ftl,並放到項目目錄下
3、寫代碼(直接下載版):
package com.soft.out_word;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import freemarker.template.Configuration;
import freemarker.template.Template;
@WebServlet("/OutWord")
public class OutWord extends HttpServlet {
private Configuration configuration = null;
private static final long serialVersionUID = -8933774728508351159L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
configuration = new Configuration(Configuration.VERSION_2_3_28);
configuration.setDefaultEncoding("UTF-8");
Map<String,Object> dataMap=new HashMap<String,Object>();
dataMap.put("test", "測試");
configuration.setClassForTemplateLoading(this.getClass(), "/"); //FTL文件所存在的位置
Template template = null;
try {
template = configuration.getTemplate("word.ftl"); //文件名
response.setHeader("Content-disposition","attachment;filename="+UUID.randomUUID().toString()+".doc");
response.setContentType("application/msword;charset=UTF-8");
PrintWriter writer = response.getWriter();
template.process(dataMap, writer);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
從代碼中可以看出,只需要將目標位置和需要加入的數據存在 dataMap 中即可,程序會將數據替換到目標位置。
4、導出word(寫成文件版)
package com.soft.out_word;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
* Servlet implementation class outWord2
*/
@WebServlet("/outWord2")
public class outWord2 extends HttpServlet {
private static final long serialVersionUID = 1L;
private Configuration configuration = null;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
configuration = new Configuration(Configuration.VERSION_2_3_28);
configuration.setDefaultEncoding("UTF-8");
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("test", "測試");
configuration.setClassForTemplateLoading(this.getClass(), "/"); // FTL文件所存在的位置
Template template = null;
try {
template = configuration.getTemplate("word.ftl"); // 文件名
File outFile = new File("D:\\demo.docx");
FileOutputStream fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
Writer out = new BufferedWriter(oWriter);
template.process(dataMap, out);
out.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
合併單元格:
參考鏈接:https://www.cnblogs.com/10158wsj/p/11211471.html
freemarker 無法實現合併單元格,因爲其本省只是替換數據,但是我們可以通過word本身的標籤做到合併,
使用的標籤是 <w:vMerge w:val='restart'/> 和 <w:vMerge/>
原則一、第一行數據只放"<w:vMerge w:val='restart'/>",從第二行開始,所有要合併的單元格放"<w:vMerge/>"。
public List<Map<String, String>> checkList(List<Map<String, String>> list) {
String start = "<w:vMerge w:val='restart'/>";
String end = "<w:vMerge/>";
list.get(0).put("start", start);
for (int i = 1; i < list.size(); i++) {
list.get(i).put("end", end);
}
return list;
}