Controller中
@RequestMapping("/toExcel") //导出为Excel,用url传参数
@ResponseBody
public PjResult toExcel(HttpServletResponse response, RedirectAttributes redirectAttributes,
@RequestParam("keyword") String keyword) {
//第一步,为要导出的类中要导出的字段添加注解
//第二步,根据条件(url传参数)查询出结果列表
//创建一个用作查询条件的对象.
FalseHelpMessageForAdmin falseMessage = new FalseHelpMessageForAdmin();
//把接受的参数存入该用作查询的对象
if (!keyword.equals("undefined")) { //keyWord里可能会有中文,要前台加密,后台解密.
String key = null;
try {
key = URLDecoder.decode(keyword, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//为传入的参数实体类设置属性
falseMessage.setKeyword(key);
}
List<FalseHelpMessageForAdmin> list = service.findListByPage(falseMessage);
//第三步,生成一个ExportExcel对象,把查出的list写进去
try {
new ExportExcel("虚假呼救信息", FalseHelpMessageForAdmin.class, 1).setDataList(list).write(response, "虚假呼救信息.xlsx")
.dispose();
} catch (IOException e) {
PjResult.fail("导出数据失败!失败信息:" + e.getMessage());
e.printStackTrace();
}
return PjResult.success();
}
pojo中
public class FalseHelpMessageForAdmin extends BaseSearchCond implements Serializable {
private static final long serialVersionUID = 1L;
// 表erh_help_invalid
private Long id; /* id 序号*/
//地点信息
private String latitude; /* latitude */
private String longitude; /* longitude */
private String erhUserId; /* erh_user_id */
private String helpDate; /* add_date呼救时间,为避免进行繁琐的转换,使用String类型来传递日期类型 */
//计算得到的属性
private String location; //根据经纬度设置的地点.
// 备用属性
private String keyword; //用于查询时使用的关键字
@ExcelField(title="序号", align=2, sort=1 )
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ExcelField(title="呼救时间", align=2, sort=4 )
public String getHelpDate() {
return helpDate;
}
vue前端中
<Button type="primary" icon="md-share-alt" @click="toExcel" >导出为Excel</Button>
toExcel() { //导出为Excel(按照用户此时的查询条件进行筛选数据)
var _This = this;
var keyword = _This.qObj.keyword;
console.log(keyword);
console.log(keyword != null);
if(keyword != null) {
keyword = encodeURIComponent(encodeURIComponent(keyword)); //防止url传参数中文乱码
}
var path = _This.APP_API_PATH.false_message_to_excel +
"?" + "keyword=" + keyword;
window.open(path);
},