欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue+springmvc导出excel数据的实现代码

程序员文章站 2022-07-03 21:53:40
vue端处理 this.$http.get(this.service + '/user/excel',{responsetype: 'blob'}).then((...

vue端处理

this.$http.get(this.service + '/user/excel',{responsetype: 'blob'}).then(({data})=> {
   console.info(typeof data)
   var a = document.createelement('a');
   var url = window.url.createobjecturl(data);
   a.href = url;
   a.download = '用户统计信息.xls';
   a.click();
   window.url.revokeobjecturl(url);
  })

web端处理

  int total=userbsservice.getcount(null);
  list<uservo> list=userbsservice.getlist(null, 1, total);
  string filename = new date().gettime() + "";
   xssfworkbook wb=new xssfworkbook();
   sheet sheet=wb.createsheet();
   row row0=sheet.createrow(0);
   string titlename[] = {"用户账号", "充值总金额", "邀请总人数", "社群组"};//列名
   for(int i=0;i<titlename.length;i++){
     sheet.setcolumnwidth(i, 10 * 512); 
     row0.createcell(i).setcellvalue(titlename[i]);
   }
   int i=0;
   for(uservo v:list){
     row row=sheet.createrow(i+1);
     if(!stringutils.isempty(v.getmobile())){
        row.createcell(0).setcellvalue(v.getmobile());
      }else{
        row.createcell(0).setcellvalue(v.getemail());
      }
     row.createcell(1).setcellvalue(bigdecimalutil.outputconvert(v.getamount()));
     row.createcell(2).setcellvalue(v.getcounts());
     row.createcell(3).setcellvalue(v.getgroups());
    i++;
   }
   bytearrayoutputstream os = new bytearrayoutputstream();
  try{   
     try {
      wb.write(os);
      wb.close();
     } catch (ioexception e) {
       e.printstacktrace();
     }
     byte[] content = os.tobytearray();
     inputstream is = new bytearrayinputstream(content);
     response.reset();
     response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
     response.setheader("content-disposition", "attachment;filename="+ new string((filename + ".xls").getbytes(), "iso-8859-1"));
     servletoutputstream out = response.getoutputstream();
     bufferedinputstream bis = null;
     bufferedoutputstream bos = null;
     try {
       bis = new bufferedinputstream(is);
       bos = new bufferedoutputstream(out);
       byte[] buff = new byte[2048];
       int bytesread;
       while (-1 != (bytesread = bis.read(buff, 0, buff.length))) {
         bos.write(buff, 0, bytesread);
       }
     } catch (final ioexception e) {
       throw e;
     } finally {
       if (bis != null)
         bis.close();
       if (bos != null)
         bos.close();
     }
   }catch (exception e){
   }
  return null;

总结

以上所述是小编给大家介绍的vue+springmvc导出excel数据的实现代码,希望对大家有所帮助