您的位置:首页 > 编程语言 > Java开发

Java +EasyUI+SpringMvc实现Excle导入导出(下)

2015-01-26 16:29 489 查看

前言

接上篇,在上篇文章我们介绍了要实现Excle导入做的一些配置和Excel导入的前端EasyUI代码的书写和后台controller的具体书写,这篇我们我们主要来学习Excle导出的实现和ExcelUtil类的编写。

正题

Excel导出就是根据前台条件将参数传到controller,根据参数去数据库中进行查询,查询出list集合,调用ExcelUtil工具类,将list集合转为成excle数据,输出到浏览器。

导出实现

首先我们先来看下前台代码,前台获取参数,将参数传到对于的controller中:

//导出题库
		function toDownLoadExcel(){
			 
			  //获取题型
			  var id= $('#questionType').combobox('getValue')
			  var questionTypes=encodeURI(id);
			   
			  //获取课程
			  var courseTypeId =$('#courseTypeId').combobox('getValue')
			  var courseType=encodeURI(courseTypeId);
			  
			  if(questionTypes !=""){ 
				  document.getElementById("downLoadExcel").href ="${pageContext.request.contextPath}/leadtoQuestionTypes/leadToExcelQuestionBank?questionType="+questionTypes+"&courseType="+courseType;
		          $.messager.alert('提示','操作成功!','info');  
			  }else{  
		          $.messager.alert('提示','请选择课程题型!','info'); 
			  }

		}


后台controller获取前台传来的参数,根据参数去数据库查询list集合,将list集合通过工具类进行转化,将Excle输出到浏览器

/**
 * 导出excel题型题库
 *
 * @param request 请求
 *            
 * @param resposne 响应
 *            
 * @throws UnsupportedEncodingException   编码异常
 *            
 */
@RequestMapping("/leadToExcelQuestionBank")
public void leadToExcelQuestionBank(HttpServletRequest request,
		HttpServletResponse response) throws UnsupportedEncodingException {
	try {
		// 获取前台传来的题型和课程
		String questionType = request.getParameter("questionType").trim();
		String courseType = request.getParameter("courseType").trim();
		String questionTypeNameId = new String(
				questionType.getBytes("iso-8859-1"), "utf-8");
		String courseTypeId = new String(courseType.getBytes("iso-8859-1"),
				"utf-8");
		// excel表格的表头,map
		LinkedHashMap<String, String> fieldMap = leadToInQuestionTypesManageBean.getMapLeadToExcelQuestionBank(questionTypeNameId);
		// excel的sheetName
		String sheetName = "题库";
		// excel要导出的数据
		List list = leadToInQuestionTypesManageBean.leadToExcelQuestionBank(questionTypeNameId, courseTypeId);
		// 导出
		if (list == null || list.size() == 0) {
			System.out.println("题库为空");
		}else {
			//将list集合转化为excle
			ExcelUtil.listToExcel(list, fieldMap, sheetName, response);
			System.out.println("导出成功~~~~");
		}
	} catch (ExcelException e) {
		e.printStackTrace();
	}
}


其中list集合转化为excle时候的参数fieldMap,是导出的数据库字段和excle中的中文表头map,方法如下:

/**
 * 得到导出Excle时题型的英中文map
 *
 * @return 返回题型的属性map
 */
public LinkedHashMap<String, String> getLeadToFiledPublicQuestionBank() {

	LinkedHashMap<String, String> superClassMap = new LinkedHashMap<String, String>();

	superClassMap.put("stemContent", "题干内容");
	superClassMap.put("difficulty", "难度等级");
	superClassMap.put("scoreCoefficient", "分值系数");
	superClassMap.put("chapter", "章节");
	superClassMap.put("availability", "是否使用");

	return superClassMap;
}


在上一篇文章中的excle数据转为list集合时,也用到了一个map,那个map时excle中文表头和数据库字段的map集合,方法如下:

/**
 * 得到导入Excle时,题型类的中英文map
 *
 * @return 返回题型类的属性map
 */
public LinkedHashMap<String, String> getLeadInFiledPublicQuestionBank() {
	// excel的表头与文字对应
	LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
	map.put("题干内容", "stemContent");
	map.put("难度等级", "difficulty");
	map.put("分值系数", "scoreCoefficient");
	map.put("章节", "chapter");
	map.put("是否使用", "availability");

	return map; 
}


ExcelUtil工具类

在介绍ExcelUtil工具类之前,我们应该先了解下Jxl,Jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作Excel文档。我们实现的Excle导入导出就是利用了Jxl,当然了除了Jxl,Apache的一个POI也可以操纵Excle,只不过两者相比,Jxl使用简单比较容易上手,POI稍微复杂点。而Jxl的缺点就是目前Jxl还不能操作Excle2000以上的版本。

我们本次使用的Jxl是2.6.12版本的,在maven的pom.xml文件中添加Jxl的坐标依赖:

<dependency>
	<groupId>net.sourceforge.jexcelapi</groupId>
	<artifactId>jxl</artifactId>
	<version>2.6.12</version>
</dependency>


而我们使用的ExcelUtil工具类,可以完成的功能是将list转化为Excle输出到浏览器,同时也可以完成将Excle转化为list集合,还可以设置excle列宽等。


关于ExcelUtil类代码比较多,感兴趣的同学可以自行下载


小结

Excle的导入和导出是我们在开发中经常遇到的问题,通过这两篇文章,我们就利用Jxl简单完成了excle的操作,实现了excle的导入和导出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: