VSTO功能越来越丰富,越来越强大,而在国内微信也是如日中天,而且微信也开放了微信企业号,企业号与公众号(服务号及订阅号)是不同的
它主要是针对企业用户使用,对企业来讲,非常有用。而在企业中使用Office(Excel word access ppt等)的办公用户也非常多
如果能够把两者完美结构起来呢,你可以通过Excel VSTO插件及微信的开发接口来将2者连接起来
下面我们就来实现Excel VSTO插件如何在微信企业号中获取指定部门ID的相关信息,及如何用代码 删除指定的部门
部分核心代码如下所示(Office中国出品):
/// <summary>
/// 获取指定部门ID的信息
/// </summary>
/// <param name="departmentId">部门ID</param>
/// <param name="refresh">是否刷新缓存,还是直接获取数据</param>
/// <returns>返回指定部门ID的信息</returns>
/// 备注 Cache√
public ClsDepartment GetDepartment(int departmentId, bool refresh = false)
{
ClsCache clsCache = new ClsCache(this._AccountName);
ClsDepartment clsDepartment;
//获取缓存
if (!refresh)
{
clsDepartment = clsCache.GetPartyCache(departmentId);
if (clsDepartment != null) return clsDepartment;
}
//获取不到缓存,通过接口获取数据
ClsWeb clsWeb = new ClsWeb();
string accessToken = this._AccessToken.GetAccessTokenString();
string strReturn = clsWeb.GetHttp(string.Format(c_urlGetDepartment, accessToken, departmentId), "UTF-8");
if (strReturn.Length == 0) throw (new Exception("(Empty)无法获取内容,请检查网络连接 3"));
JObject json = JObject.Parse(strReturn);
string errCode = json["errcode"].ToString();
if (errCode != "0") throw (new Exception(ClsErrMessage.GetErrorMessage(errCode)));
ClsDepartment[] clsPartys = json["department"].ToObject<ClsDepartment[]>();
clsDepartment = clsPartys.Where(x => x.id == departmentId).ToArray()[0];
//写入缓存
clsCache.ClearPartyCache(clsDepartment.id); //清掉旧缓存
clsCache.WritePartyCache(clsDepartment); //写入新缓存
return clsDepartment;
}
/// <summary>
/// 删除部门
/// </summary>
/// <param name="departmentId">部门ID</param>
/// 备注 Cache√
public void DeleteDepartment(int departmentId)
{
ClsWeb clsWeb = new ClsWeb();
string accessToken = this._AccessToken.GetAccessTokenString();
string strReturn = clsWeb.GetHttp(string.Format(c_urlDeleteDepartment, accessToken, departmentId), "UTF-8");
if (strReturn.Length == 0) throw (new Exception("(Empty)无法获取内容,请检查网络连接 4"));
//把返回结果的JSON格式转成Dictionary格式
Dictionary<string, string> dicReturn = JsonConvert.DeserializeObject<Dictionary<string, string>>(strReturn);
if (dicReturn["errcode"] != "0") throw (new Exception(ClsErrMessage.GetErrorMessage(dicReturn["errcode"])));
//清除缓存
ClsCache clsCache = new ClsCache(this._AccountName);
clsCache.ClearPartyCache(departmentId);
}