前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信公众号开发之如何一键导出微信所有用户信息到Excel

微信公众号开发之如何一键导出微信所有用户信息到Excel

作者头像
Javen
发布2018-08-21 11:03:07
4.6K0
发布2018-08-21 11:03:07
举报
文章被收录于专栏:酷玩时刻酷玩时刻

极速开发微信公众号系列文章之如何一键导出微信所有用户信息到Excel

前方高能警告⚠️:用户信息导出我们需要使用以下权限以及接口

以上链接点击可以查看相关文档

本文中用户导入到excel使用的是jxl,当然大家可以使用poi。如果不会使用jxl可以参考我之前写的 Java实现Excel导入数据库,数据库中的数据导入到Excel

好了,准备工作做好了那就开干吧!!!!

实现的目标:访问一个地址可以下载一个保存最新所有用户详细信息的Excel,最终效果图如下

最终效果图.png

将详细的用户信息(List)保存到Excel

代码语言:javascript
复制
/**
   * 将详细的用户信息保存到Excel
   * @param userInfos
   * @return
   */
  private File saveToExcel(List<UserInfo> userInfos){
    File file=null;
    try {
      WritableWorkbook wwb = null;
      
      // 创建可写入的Excel工作簿
      String fileName = "用户详细信息.xls";
      file=new File(fileName);
      //以fileName为文件名来创建一个Workbook
      wwb = Workbook.createWorkbook(file);

      // 创建工作表
      WritableSheet ws = wwb.createSheet("用户详细信息", 0);
      ws.setColumnView(0,8);
      ws.setColumnView(1,15);
      ws.setColumnView(2,50);
      ws.setColumnView(3,8);
      ws.setColumnView(4,10);
      ws.setColumnView(5,10);
      ws.setColumnView(6,10);
      ws.setColumnView(7,20);
      ws.setColumnView(8,50);
      ws.setColumnView(9,10);
      ws.setColumnView(10,30);
      ws.setColumnView(11,20);
      ws.setColumnView(12,20);
      
      ws.mergeCells(0,0,12,0);//合并第一列第一行到第七列第一行的所有单元格 
      WritableFont font1= new WritableFont(WritableFont.TIMES,16,WritableFont.BOLD); 
      WritableCellFormat format1=new WritableCellFormat(font1); 
      format1.setAlignment(jxl.format.Alignment.CENTRE);
      Label top= new Label(0, 0, "所有用户详细信息",format1);
      ws.addCell(top);
      
      //要插入到的Excel表格的行号,默认从0开始
      Label labelId= new Label(0, 1, "编号");
      Label labelnickname= new Label(1, 1, "用户的昵称");
      Label labelopenid= new Label(2, 1, "用户的标识");
      Label labelsex= new Label(3, 1, "性别");
      Label labelcountry= new Label(4,1, "所在国家");
      Label labelprovince= new Label(5,1, "所在省份");
      Label labelcity= new Label(6, 1, "所在城市");
      Label labellanguage= new Label(7,1, "用户的语言");
      Label labelheadimgurl= new Label(8,1, "用户头像");
      Label labelsubscribe= new Label(9, 1, "是否订阅");
      Label labelsubscribetime= new Label(10, 1, "关注时间");
      Label labelgroupid= new Label(11, 1, "所在的分组ID");
      Label labelremark= new Label(12, 1, "备注");
      ws.addCell(labelId);
      ws.addCell(labelnickname);
      ws.addCell(labelopenid);
      ws.addCell(labelsex);
      ws.addCell(labelcountry);
      ws.addCell(labelprovince);
      ws.addCell(labelcity);
      ws.addCell(labellanguage);
      ws.addCell(labelheadimgurl);
      ws.addCell(labelsubscribe);
      ws.addCell(labelsubscribetime);
      ws.addCell(labelgroupid);
      ws.addCell(labelremark);
      for (int i = 0; i < userInfos.size(); i++) {
          
          Label labelId_i= new Label(0, i+2, i+1+"");
          Label nickName= new Label(1, i+2, userInfos.get(i).getNickname());
          Label openid= new Label(2, i+2, userInfos.get(i).getOpenid());

        String sexStr=userInfos.get(i).getSex();
        //用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
        if (StrKit.notBlank(sexStr)) {
          int sexInt=Integer.parseInt(sexStr);
          if (sexInt==1) {
            sexStr="男";
          }else if (sexInt==2) {
            sexStr="女";
          }
        }else {
          sexStr="未知";
        }

          Label sex= new Label(3, i+2, sexStr);
          Label country= new Label(4, i+2, userInfos.get(i).getCountry());
          Label province= new Label(5, i+2, userInfos.get(i).getProvince());
          Label city= new Label(6, i+2, userInfos.get(i).getCity());
          Label language= new Label(7, i+2, userInfos.get(i).getLanguage());
          Label headimgaeurl= new Label(8, i+2, userInfos.get(i).getHeadimgurl());

          Label subscribe= new Label(9, i+2, userInfos.get(i).getSubscribe().equals("1")?"已关注":"未关注");
        //获取关注时间
        String subscribe_time = userInfos.get(i).getSubscribe_time();

        if (StrKit.notBlank(subscribe_time)) {
          subscribe_time=sfg.format(new Date(Long.parseLong(subscribe_time) * 1000L));
        }
          Label subscribetime= new Label(10, i+2, subscribe_time);
          Label groupid= new Label(11, i+2, userInfos.get(i).getGroupid());
          Label remark= new Label(12, i+2, userInfos.get(i).getRemark());
          ws.addCell(labelId_i);
          ws.addCell(openid);
          ws.addCell(nickName);
          ws.addCell(sex);
          ws.addCell(country);
          ws.addCell(province);
          ws.addCell(city);
          ws.addCell(language);
          ws.addCell(headimgaeurl);
          ws.addCell(subscribe);
          ws.addCell(subscribetime);
          ws.addCell(groupid);
          ws.addCell(remark);
      }
     
      //写进文档
      wwb.write();
      // 关闭Excel工作簿对象
      wwb.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return file;
  }

获取所有用户列表

代码语言:javascript
复制
/**
   * 获取所有的openid
   * @return
   */
  public List<String> getAllOpenId(){
    List<String> openIds = getOpenIds(null);
    return openIds;
  }

getOpenIds(Stirng next_openid) 方法中迭代(一次拉取调用最多拉取10000个关注者的OpenID)获取所有的openId并返回一个List集合

代码语言:javascript
复制
private List<String> getOpenIds(String next_openid){
    List<String> openIdList=new ArrayList<String>();
    ApiResult apiResult=UserApi.getFollowers(next_openid);
    String json=apiResult.getJson();
log.error("json:"+json);
    if (apiResult.isSucceed()) {
      JSONObject result = JSON.parseObject(json);
      next_openid = apiResult.getStr("next_openid");
      int count = apiResult.getInt("count");
      JSONObject openIdObject = result.getJSONObject("data");
      if (count>0) {
        JSONArray openids=openIdObject.getJSONArray("openid");
        for (int i = 0; i < openids.size(); i++) {
          openIdList.add(openids.getString(i));
        }
      }
      //下一页
      if (next_openid!=null&& !next_openid.equals("")) {
        List<String> list = getOpenIds(next_openid);
        openIdList.addAll(list);
      }
    }
    return openIdList;
  }

批量获取用户基本信息

注意批量接口最多支持一次拉取100条

代码语言:javascript
复制
/**
   * 根据openId列表获取用户信息
   * @param allOpenId  
   * @return
   */
  private List<UserInfo> getAllUserInfo(List<String> allOpenId){
    List<UserInfo> userInfos = new ArrayList<UserInfo>();
    int total=allOpenId.size();
    UserConfig[] user_list=null;
    //开发者可通过该接口来批量获取用户基本信息。最多支持一次拉取100条。
    int temp=100;//一次获取100
    if (total>temp) {
      int page=0;//当前页面
      int count=total/100+(total%100>0?1:0);//总共获取多少次
      int index=0;
      while (page<count) {
        index=(temp*(page+1))>total?total:(temp*(page+1));
        System.out.println("/////////"+page*temp+" "+index);
        user_list=new UserConfig[index-(page*temp)];
        for (int i = page*temp; i <index; i++) {
          UserConfig config=new UserConfig();
          config.setLang(LangType.zh_CN);
          config.setOpenid(allOpenId.get(i));
          user_list[i-(page*temp)]=config;
        }
        GetUserInfo getUserInfo = new GetUserInfo();
        getUserInfo.setUser_list(user_list);
        String jsonGetUserInfo = JsonKit.toJson(getUserInfo);
        System.out.println("jsonGetUserInfo:"+jsonGetUserInfo);
        
        ApiResult apiResult = UserApi.batchGetUserInfo(jsonGetUserInfo);
        
        String jsonResult = apiResult.getJson();
        //将json转化为对象
        List<UserInfo> userInfo = parseJsonToUserInfo(jsonResult);
        userInfos.addAll(userInfo);
        page++;
      }
    }else {
      user_list=new UserConfig[total];
      for (int i = 0; i < user_list.length; i++) {
        System.out.println(allOpenId.get(i));
        UserConfig config=new UserConfig();
        config.setLang(LangType.zh_CN);
        config.setOpenid(allOpenId.get(i));
        user_list[i]=config;
      }
      GetUserInfo getUserInfo = new GetUserInfo();
      getUserInfo.setUser_list(user_list);
      String jsonGetUserInfo = JsonKit.toJson(getUserInfo);
      
      
      ApiResult batchGetUserInfo = UserApi.batchGetUserInfo(jsonGetUserInfo);
      List<UserInfo> userInfo = parseJsonToUserInfo(batchGetUserInfo.getJson());
      userInfos.addAll(userInfo);
    }
    return userInfos;
  }

大功告成---测试

开源项目weixin_guide 中添加路由 com.javen.common.APPConfig 类的 configRoute(Routes me) 的方法中添加 me.add("/wxuser", UserController.class,"/front");

在浏览器中输入http://localhost:8080/wxuser 即可下载Excel

代码语言:javascript
复制
public void index(){
    List<UserInfo> allUserInfo = getAllUserInfo(getAllOpenId());
    
    if (!allUserInfo.isEmpty()) {
      ///下载userInfos
      File file = saveToExcel(allUserInfo);
      renderFile(file);
    }else {
      render("目前暂无用户...");
    }
  }

以上如何一键导出微信所有用户信息到Excel的全过程。 欢迎留言、转发、打赏项目源码参考地址 点我点我--欢迎Start

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.09.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 将详细的用户信息(List)保存到Excel
  • 获取所有用户列表
  • 批量获取用户基本信息
  • 大功告成---测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档