当用户尝试在 C# 中刷新 Web 查询时如何询问凭据

问题描述 投票:0回答:0

我目前正在使用 DocumentFormat.OpenXml.Spreadsheet 来生成一个 excel 文件。我还向其中添加了一个网络查询。

我现在想对用户进行身份验证。因此,在 excel 中单击刷新时,我想请求用户名和密码,然后在 API 中对其进行身份验证。

这是我目前使用 webquery 生成 excel 的代码。

ConnectionsPart connPart = workbook.WorkbookPart.AddNewPart\<ConnectionsPart\>();
connPart.Connections = new Connections();
var connection1 = new Connection()
{
    Id = 1,
    Name = "Connection1",
    Type = 4, //Web Query
    WebQueryProperties = new WebQueryProperties {
        Url = "myurlfordatarefresh"
    }
};

connPart.Connections.Append(connection1);
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = "mysheet" };
QueryTablePart qt = sheetPart.AddNewPart\<QueryTablePart\>();

qt.QueryTable = new QueryTable() { Name = "Connection", ConnectionId = connection1.Id, RefreshOnLoad = true };

DefinedNames definedNames = new DefinedNames();
definedNames.Append(new DefinedName()
{
    Name = "Connection",
    Text = "mysheet!$A$2:$A$2"
});

上面的代码添加了一个网络查询,在下载 excel 文件时,我能够刷新并从 url 获取数据,文件中的数据被最新数据替换。

这里是否有其他属性或任何方式可以帮助我实现身份验证(询问用户名和密码)。有人可以在这里帮助我吗?

c# .net-core openxml basic-authentication excel-web-query
© www.soinside.com 2019 - 2024. All rights reserved.