freemarker导出word

    工作中遇到需要导出word的要求,特此记录下来,以便以后查看。这次导出word,是采用的freemarker模板的方式进行导出的,个人感觉比较方便。

    具体步骤

1.新建word根据所需要的格式,进行编辑

freemarker导出word_第1张图片

2.把word另存为xml。

3.用firstobject打开xml文件,或者任意编辑器。这里用firstobject是因为他有自动格式化功能

用notepad打开是这样的

freemarker导出word_第2张图片

用firstobject打开之后按F8之后就能变成这样

freemarker导出word_第3张图片

然后保存,再用notepad打开就好了。notepad有高亮显示很方便

4.然后对需要获取值的地方进行修改比如序号换成${index},题目位置改为${stem},如果是集合的话可以进行遍历,具体用法参考freemarker标签用法






ivviw
ivviw
28
47
2018-05-24T03:08:00Z
2018-05-24T11:50:00Z
1
11
69
Microsoft
1
1
79
15












































































































































































































































































































































































































































































































































































































<#list stuanswerList as stuanswer>























${stuanswer.paperName}


































班级:



























${stuanswer.className}


















学号:



























${stuanswer.stuNo}



















姓名:


























${stuanswer.studentName}





















开始时间:



























${stuanswer.answerStime?datetime}



















结束时间:



























${stuanswer.answerEtime?datetime}



















总分:


























${stuanswer.totalScore}





















考试用时:


























${stuanswer.usedMinutes}



















得分:


























${stuanswer.score}


























































<#list questionsList as questions>	
<#if questions.type =='2' || questions.type =='4'>

















${questions_index+1}.

















<#if (questions.stem)??>
${questions.stem}








<#list questions.eduExamSubquestionsList as subquestions>


















<#if questions.type =='2' || questions.type =='4'>
(${subquestions_index+1})

<#if questions.type =='1' || questions.type =='3'>
${questions_index+1}.


















${subquestions.stem}





<#list subquestions.answerList as answer>

















答案${answer.sorts}:















${answer.answerInfo}






















正确答案:
















${subquestions.answerExp}





















学生作答







:















${subquestions.stuanswerExp}

















5.后台代码的实现

 public void exportPaper(EduExamStuanswer eduExamStuanswer, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		
		//试题集合
		List questionsList = eduExamStuanswerinfoService.findEduExamStuanswerinfoList(eduExamStuanswerinfo);
		//学生答题信息集合
		List stuanswerList = new ArrayList();
		stuanswerList.add(eduExamStuanswer);
		//
		Map dataMap = new HashMap();
		//
		dataMap.put("questionsList", questionsList);
		dataMap.put("stuanswerList", stuanswerList);
		//
		String fileName =eduExamStuanswer.getPaperName()+ "试卷详情" +".doc";
		//
		Configuration configuration = new Configuration();  
        configuration.setDefaultEncoding("utf-8");  
        PrintWriter out = null;
        try {
        	//指定ftl所在目录,根据自己的改  
			configuration.setDirectoryForTemplateLoading(new File(request.getSession().getServletContext().getRealPath(File.separator)+"/static/freemarker/"));
			response.setContentType("application/msword");     
			response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes("GBK"), "iso8859-1") + "\"");  
			response.setCharacterEncoding("utf-8");//此句非常关键,不然word文档全是乱码  
			out = response.getWriter();  
			Template t =  configuration.getTemplate("paper.ftl","utf-8");//以utf-8的编码读取ftl文件  
			t.process(dataMap, out);  
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        out.close();  
    }
因为我做的是点击导出之后弹出下载窗口,所以用的
response.setHeader()

6.功能基本实现

freemarker导出word_第4张图片



你可能感兴趣的:(开发经验)