基本思路
1. 导入freemaker2.3.jar
2. 需要导出的Word模板
3. 在Word内填入值的标签
4. Word另存为xml(2003版本)
5. Coding
6. 导出Word文件
具体操作
1. Intellij IDEA > FIle > Project Structure > Libraries > "+"(左下角) > OK
2. Word模板有需求方提供
3. 在Word内填入需要值的标签
4. 另存为Word 2003 XML文档
5. 放进项目文件
6. 实现代码
'''
/**
* 获取导出任务书所需要的数据,并封装进dataMap
* @param studWorkProcess
* @param studWork1
* @param request
* @param response
* @return
*/
@RequiresPermissions("studwork:studwork1:studWork1:edit")
@RequestMapping(value = {"exportStudWork1"})
public StringexportStudWork1(StudWorkProcess studWorkProcess, StudWork1 studWork1, HttpServletRequest request, HttpServletResponse response) {
studWork1.setId(studWorkProcess.getId());
List studWork1List =studWork1Service.findList(studWork1);
if(studWork1List.size() ==1) {
Map dataMap =new HashMap();
// set this yaer
dataMap.put("year","2020");
User user =systemService.getUser(studWork1List.get(0).getStudId());
// get student user obj
dataMap.put("user",user);
dataMap.put("studWork1", studWork1List.get(0));
User currentStudent =systemService.getUser(studWork1.getStudId());
// file name
String fileName = currentStudent.getNo() +"_" + currentStudent.getName() +"_" +"任务书.doc";
try {
download(response, fileName, dataMap);
}catch (Exception e) {
e.printStackTrace();
}
}
return "modules/studwork/studwork1/studWork1List";
}
/**
* 导出任务书doc文件
* @param response
* @param newWordName
* @param dataMap
*/
public void download(HttpServletResponse response, String newWordName, Map dataMap) {
Configuration configuration =new Configuration();
// set encoding
configuration.setDefaultEncoding("utf-8");
String path =this.getClass().getResource("/").getPath();
StudWork1 studWork1 =new StudWork1();
// get template path
configuration.setClassForTemplateLoading(studWork1.getClass(), "/");
Template t =null;
try {
// word1.xml is a template(generate by file 01)
User user = UserUtils.getUser();
if(user.getIsTeacher() ==null || user.getIsTeacher() ==""){
t = configuration.getTemplate("word1.xml","utf-8");
}else{
t = configuration.getTemplate("word1_stamp.xml","utf-8");
}
}catch (Exception e) {
e.printStackTrace();
}
File outFile =null;
Writer out =null;
String filename = newWordName;
try {
outFile =new File(newWordName);
out =new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile),"utf-8"));
}catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
}catch (Exception e) {
e.printStackTrace();
}
try {
out.flush();
out.close();
}catch (IOException e) {
e.printStackTrace();
}
InputStream fis =null;
OutputStream toClient =null;
try {
fis =new BufferedInputStream(new FileInputStream(outFile));
byte[] buffer =new byte[fis.available()];
fis.read(buffer);
fis.close();
// clear response
response.reset();
// set header and translate encoding
filename = URLEncoder.encode(filename, "utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + filename+"");
response.addHeader("Content-Length", "" + outFile.length());
toClient =new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(fis!=null){
fis.close();
}
}catch (IOException e) {
e.printStackTrace();
}
try {
if(toClient!=null){
toClient.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
'''
采坑
1. Word导出格式一定要是2003版本的XML,不然生成的文件无法正常打开
2. Word另存为过程中,会将部分标签分开,需要手动调整
'''
'''
调整后
'''
'''