微信公眾號開發之如何一鍵導出微信所有用戶信息到Excel


微信開發交流群:148540125
系列文章參考地址 極速開發微信公眾號歡迎留言、轉發、打賞
項目源碼參考地址 點我點我--歡迎Start

極速開發微信公眾號系列文章之如何一鍵導出微信所有用戶信息到Excel

前方高能警告⚠️:用戶信息導出我們需要使用以下權限以及接口

以上鏈接點擊可以查看相關文檔

本文中用戶導入到excel使用的是jxl,當然大家可以使用poi。如果不會使用jxl可以參考我之前寫的 Java實現Excel導入數據庫,數據庫中的數據導入到Excel

好了,准備工作做好了那就開干吧!!!!

實現的目標:訪問一個地址可以下載一個保存最新所有用戶詳細信息的Excel,最終效果圖如下

最終效果圖.png

將詳細的用戶信息(List)保存到Excel

/**
   * 將詳細的用戶信息保存到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;
  }

獲取所有用戶列表

/**
   * 獲取所有的openid
   * @return
   */
  public List<String> getAllOpenId(){
    List<String> openIds = getOpenIds(null);
    return openIds;
  }

getOpenIds(Stirng next_openid) 方法中迭代(一次拉取調用最多拉取10000個關注者的OpenID)獲取所有的openId並返回一個List集合

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條

/**
   * 根據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

public void index(){
    List<UserInfo> allUserInfo = getAllUserInfo(getAllOpenId());
    
    if (!allUserInfo.isEmpty()) {
      ///下載userInfos
      File file = saveToExcel(allUserInfo);
      renderFile(file);
    }else {
      render("目前暫無用戶...");
    }
  }

以上如何一鍵導出微信所有用戶信息到Excel的全過程。
歡迎留言、轉發、打賞項目源碼參考地址 點我點我--歡迎Start


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM