上一篇博客我们分享了数据上传经验《导入Excel》。今天我们来分享一下数据下载经验:导出Excel!
引言
作为一个信息管理类系统,我们不仅需要录入很多数据,同时我们也会收集存储很多数据,并通过条件检索对得到的数据进行整理和分析,这时候我们就可以导出这些数据,如下图所示:
同导入Excel一样,很多系统也同时提供了导出Excle功能,方便把检索到的有用数据导出Excel并打印出来,如此人性化的功能,我们在做类似的系统时也应该借鉴进去,给用户更高的体验度!下面我们来看看具体的实现吧:
思路
1、添加ExcelUtility工具类(完整版链接)
2、实现导出Excel
实现
1、添加ExcelUtility工具类
(1)ExcelUtility工具类中导出Excel代码:
/**
* @MethodName : listToExcel
* @Description : 导出Excel
* @param response:使用response可以导出到浏览器
* @param list:数据源
* @param fieldMap:中英文字段对应Map
* @throws ExcelException
*/
public <T> void listToExcel (
HttpServletResponse response,
List<T> list ,
LinkedHashMap<String,String> fieldMap) throws ExcelException{
//设置默认文件名为当前时间:年月日时分秒
String fileName=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();
//设置response头信息
response.reset();
response.setContentType("application/vnd.ms-excel"); //改成输出excel文件
response.setHeader("Content-disposition","attachment; filename="+fileName+".xls" );
//创建工作簿并发送到浏览器
try {
OutputStream out=response.getOutputStream();
listToExcel(out, list, fieldMap);
} catch (Exception e) {
e.printStackTrace();
//如果是ExcelException,则直接抛出
if(e instanceof ExcelException){
throw (ExcelException)e;
//否则将其它异常包装成ExcelException再抛出
}else{
throw new ExcelException("导出Excel失败");
}
}
}
(2)在web的pom文件里添加相关依赖:
<dependency>
<groupId>com.tgb</groupId>
<artifactId>itoo-assess-tool</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2、实现导出Excel
(1)前台Jsp代码
<div id="dlg" class="easyui-dialog" style="width:90%;height:57%; padding: 10px 20px" closed="true" buttons="#dlg-buttons">
<form id="fm" method="post" >
<div>
<div class="bblock" style="text-align:center" >
<div style="float:left; width:33%;" class="innoblock">
<label id="teaName">教师姓名:</label>
<span id="dialogTeacher" ></span>
<span id="teaId" ></span>
</div>
<div style="float:left; width:33%;" class="innoblock">
<label id="courName">课程名称:</label>
<span id="dialogCourse"></span>
<span id="courId" ></span>
</div>
</div>
</div>
</form>
<br><br>
<table id="detail" class="easyui-datagrid" title="建议详细信息" style="width: 100% x; height: 255px" charset="UTF-8"
toolbar="#detailToolbar" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true" method="post" >
<thead>
<tr >
<th field="suggestionId" hidden="true">建议Id</th>
<th field="teacherId" hidden="true">教师Id</th>
<th field="courseId" hidden="true">课程Id</th>
<th field="suggestion" width="98%" align="center" font-size="30px">学生建议</th>
</tr>
</thead>
</table>
<div id="detailToolbar">
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" οnclick="printSuggestion()">导出Excel</a>
</div>
(
2)前台js代码
// 导出Excel方法
function printSuggestion(){
var teacherId=teaId.innerHTML;
var courseId=courId.innerHTML;
alert("执行到这里");
//调用导出Excel方法
$.ajax({
type : 'post',
url : "exportExcel?teacherId=" + teacherId+"&courseId="+courseId,
// dataType : "text",
})
}
(3)后台Controller代码
/**
* 导出excel
* @param request 请求
* @param resposne 响应
* @throws UnsupportedEncodingException 编码异常
*/
@RequestMapping("/exportExcel")
public void leadToExcelQuestionBank(HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException {
// String courseId = request.getParameter("courseId").trim();
// String teacherId = request.getParameter("teacherId").trim();
String courseId = request.getParameter("courseId");
String teacherId = request.getParameter("teacherId");
courseId = "jisuanjiyingyong123";
teacherId = "wangyajin001";
//Excel要导出的数据
List<StudentAssessSuggestion> suggestions=studentAssessSuggestionBean.findSuggestionByIdExcel(teacherId,courseId);
LinkedHashMap<String, String> fieldMap=new LinkedHashMap<String, String>();
fieldMap.put("teacherName","教师姓名");
fieldMap.put("courseName","课程名称");
fieldMap.put("suggestion","学生建议");
try {
excelUtil=new CommonExcelUtil();
excelUtil.listToExcel(response, suggestions, fieldMap);
} catch (Exception e) {
e.printStackTrace();
}
}
(4)StudentAssessSuggestionBeanImpl代码:
@SuppressWarnings("unchecked")
public List<StudentAssessSuggestion> findSuggestionByIdExcel(String teacherId, String courseId) {
//查询语句的参数map
Map<Serializable,Serializable> map=new HashMap<Serializable,Serializable>();
map.put("teacherId", teacherId);
map.put("courseId", courseId);
String hql=null;
hql="From StudentAssessSuggestion where teacherId=:teacherId and courseId=:courseId ";
List<StudentAssessSuggestion> studentAssessSuggestion = studentAssessSuggestionEao.queryByHql(hql, map);
return studentAssessSuggestion;
}
ok,到这里为止数据下载经验--导出Excel已经完成。
总结
从用户角度出发,以用户为主,越靠近用户的使用习惯,软件亲和力越高,开发的软件越受欢迎!