jeefast框架:实现导出功能
在主界面中,设置【导出】功能键
<a v-if="hasPermission('platform:student:exportExcel')" class="btn btn-primary btn-sm" @click="exportExcel"><i class="fa fa-trash-o"></i> 导出</a>
xml配置项
<select id="selectBatchIds" resultType="cn.jeefast.modules.platform.entity.Student">
SELECT pn.* , su.`username` createrName,c.cname cname,g.gname gname
FROM pf_student pn
INNER JOIN pf_class c ON pn.cid=c.cid
inner join pf_grade g on c.gid=g.gid
LEFT JOIN sys_user su ON su.`user_id`=pn.`creater`
where id in
<foreach item="ids" collection="list" open="(" separator="," close=")">
#{ids}
</foreach>
</select>
js中【导出】功能选中功能
exportExcel: function () {
var noticeIds=getSelectedRows();
if(noticeIds == null){
var ids=0;
}else{
var ids=noticeIds.join(",");
}
window.top.location.href = baseURL + "platform/student/exportExcel?token="+token+"&ids="+ids;
},
实体类
//@ExcelResources :这个注释是用来导出某个数据的。
//title这个就是在导出的xls文件中某一列中命名一个名字,作为列名
//order 这个是用来将这个属性第几次取出数据
@ExcelResources(title = "学生ID",order=1)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
Dao,Service 层的代码,就省略不写了 🙂,原因是方法是它封装好了的,我们直接调用它就行。
@Log("导出用户")
@RequestMapping("/exportExcel")
@RequiresPermissions("platform:student:exportExcel")
public void exportExcel(HttpServletResponse response,HttpServletRequest request) throws Exception{
Map<String, Object> params = new HashMap<String, Object>();
List<Student> userList = null;
List<Long> ids = new ArrayList<Long>();
String str=request.getParameter("ids");
System.out.println(str);
if (str.equals("0") || str == "0") {
userList = studentService.selectList(new EntityWrapper<Student>());
}else {
String[] noticeIds = str.split(",");
for (int i = 0; i < noticeIds.length; i++) {
ids.add(Long.parseLong(noticeIds[i]));
}
userList = studentService.selectBatchIds(ids);
}
OutputStream os = response.getOutputStream();
Map<String, String> map = new HashMap<String, String>();
map.put("title", "学生信息表");
map.put("total", userList.size()+" 条");
map.put("date", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
//响应信息,弹出文件下载窗口
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("学生信息表.xls", "UTF-8"));
ExcelTemplate et = ExcelUtil.getInstance().handlerObj2Excel("web-info-template.xls", userList, Student.class, true);
et.replaceFinalData(map);
et.wirteToStream(os);
os.flush();
os.close();
}