工具实现mysql导出Excel表

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);   
      },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值