简体   繁体   中英

Make ajax call from Excel Add-in

Is it possible to make ajax calls from Excel 2013 Add-in? If not then what is the best way to create Task Pane for Excel that can do ajax calls?

Yes I've actually done exactly what you are trying to do. Here are some tips

  1. The excel forms actions are not threadsafe. 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; and renable it after execution is completed.
  2. Use an instance of `HttpClient` to do all of your requests to get json
  3. Learn from my mistake. Bite the bullet and use This json library for all of your json needs. If you need anything to be dynamic the standard .net json provider is insufficient.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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