简体   繁体   English

从Excel加载项进行Ajax调用

[英]Make ajax call from Excel Add-in

Is it possible to make ajax calls from Excel 2013 Add-in? 是否可以从Excel 2013加载项进行Ajax调用? If not then what is the best way to create Task Pane for Excel that can do ajax calls? 如果不是,那么为Excel创建可以进行Ajax调用的任务窗格的最佳方法是什么?

Yes I've actually done exactly what you are trying to do. 是的,我实际上已经完全完成了您要尝试做的事情。 Here are some tips 这里有一些提示

  1. The excel forms actions are not threadsafe. Excel表单操作不是线程安全的。 Make sure that after you click the button that will be doing the http request that you disable all the form controls btnDownload.Enabled = false; 确保在单击将要执行http请求的按钮之后,禁用所有表单控件btnDownload.Enabled = false; and renable it after execution is completed. 并在执行完成后将其租借。
  2. Use an instance of `HttpClient` to do all of your requests to get json 使用`HttpClient`的实例来执行所有获取json的请求
  3. Learn from my mistake. 从我的错误中学习。 Bite the bullet and use This json library for all of your json needs. 咬一下子弹,并使用此json库满足您的所有json需求。 If you need anything to be dynamic the standard .net json provider is insufficient. 如果您需要动态的东西,那么标准的.net json提供程序是不够的。

My code usually had the following format when doing stuff. 我的代码在执行工作时通常具有以下格式。

// click handler for vsto excel add-in
private async void btnDownload_Click(object sender, RibbonControlEventArgs e)
{
    btnDownload.Enabled = false;
    using (var client = new HttpClient())
    {
        var resp = await client.GetAsync("http://somepath.com/data.json");
        var statusCode = (int) resp.StatusCode;
        if (statusCode == 200)
        {
            var json = await resp.Content.ReadAsStringAsync();
            var jobj = JObject.Parse(json); // from the library I mention.
            // lookup docs on how to manipulate jobj
            // then make calls to excel api to affect spreadsheet from the json.
        }
    }
    btnDownload.Enabled = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM