使用Azure Devops API,如何从.xlsx文件创建项目?

问题描述

我有一个.xlsx文件,其中包含第一列项目名和第二列描述。 我想创建一个控制台应用程序,用于使用Azure DevOps Rest API从azureProjects.xlsx文件创建项目。我已经实现了下面的代码,但是我不明白如何从.xlsx文件读取并实现代码。您能为我提供解决方案吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

   namespace WorkItemTest
   {
class AzureAdmin
{
    private readonly Uri uri;
    private readonly string personalAccesstoken;

    public AzureAdmin(string orgName,string personalAccesstoken)
    {
        this.uri = new Uri("https://dev.azure.com/" + orgName);
        this.personalAccesstoken = personalAccesstoken;
    }

    public async Task<bool> createProject()
    {

        try
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",Convert.ToBase64String(
                        Encoding.ASCII.GetBytes(
                            string.Format("{0}:{1}","",personalAccesstoken))));

                var req = new Root
                {
                    name = "test3",description = "test about smthng",visibility = 0,capabilities = new Capabilities
                    {
                        versioncontrol = new Versioncontrol {sourceControlType = "Git"},processtemplate = new Processtemplate
                        {
                            templateTypeId = "b8a3a935-7e91-48b8-a94c-606d37c3e9f2"
                        }
                    }
                };

                var result = await client.PostAsJsonAsync($"{uri}/_apis/projects?api-version=5.1",req); //
                Console.WriteLine(result.StatusCode);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        return true;
    }

    public class Versioncontrol
    {
        public string sourceControlType { get; set; }
    }

    public class Processtemplate
    {
        public string templateTypeId { get; set; }
    }

    public class Capabilities
    {
        public Versioncontrol versioncontrol { get; set; }
        public Processtemplate processtemplate { get; set; }
    }

    public class Root
    {
        public string name { get; set; }
        public string description { get; set; }
        public int visibility { get; set; }
        public Capabilities capabilities { get; set; }
    }

 }
}

解决方法

您已经实现了如何在DevOps中创建团队项目的方法,您需要从.xlsx文件中读取项目名称。这不是通过Azure Devops API来实现的,您只需要获得Excel方面的帮助即可获取如何通过api从.xlsx文件读取数据的方法。

检查以下链接以查看它是否对您有帮助:

https://docs.microsoft.com/en-us/troubleshoot/dotnet/csharp/query-excel-data-aspx-page