首页 > 解决方案 > Power BI Rest API - 使用 C# 导出到 Excel 当前选择

问题描述

我们使用 Power BI Rest API 将 PowerBI 报告集成到我们的 Web 应用程序中,并使用 ReactJS 库嵌入了该报告。

我们有一个新的要求,即以 PDF 和 PPTX 格式导出报告。

场景 1:如果用户选择页面中的某个区域并且在下载的报告中它应该只突出显示 PDF/PPTX 中的数据。

当我从 app.powerbi.com 门户下载它时,这是有效的,但我们如何以编程方式实现?

问题:如何使用 C# Power BI Rest API 从页面中获取当前选择的区域?

场景 2: 如果报告有多个页面,并且用户希望使用当前选择/默认值下载唯一的特定页面

问题: 如何使用 C# Power BI Rest API 获取和传递当前页面/所有当前选择/默认值

标签: c#powerbi

解决方案


使用 REST API 将 Power BI 报告导出为 PDF、PPTX 或 PNG 文件现在处于预览状态

请注意,您要导出的报表和报表数据集都必须驻留在高级或嵌入式容量上。将报告导出到文件一文中详细介绍了其他预览限制

另请注意,与所有新的 Power BI API 一样,Export-To-File API 仅包含在Power BI APIs .NET SDK v3中。

您可以在官方文档中找到使用Power BI REST API导出到文件所需的所有信息

场景二:使用参数ExportReportPage

场景一:使用 PowerBIReportExportConfiguration 中的 reportLevelFilters,可以在过滤条件下导出报表。要导出过滤的报告,请将要用作过滤器的 URL 查询字符串参数插入到 ExportFilter。输入字符串时,必须删除 URL 查询参数的 ?filter= 部分。

这是导出请求的代码示例: 端到端示例

https://docs.microsoft.com/en-us/power-bi/developer/embedded/export-to#limitations

请参考嵌入式链接

例如,针对特定页面发送导出请求。

private async Task<string> PostExportRequest(
    Guid reportId,
    Guid groupId,
    FileFormat format,
    IList<string> pageNames = null, /* Get the page names from the GetPages REST API */
    string urlFilter = null)
{
    var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
    {
        Settings = new ExportReportSettings
        {
            Locale = "en-us",
        },
        // Note that page names differ from the page display names
        // To get the page names use the GetPages REST API
        Pages = pageNames?.Select(pn => new ExportReportPage(Name = pn)).ToList(),
        // ReportLevelFilters collection needs to be instantiated explicitly
        ReportLevelFilters = !string.IsNullOrEmpty(urlFilter) ? new List<ExportFilter>() { new ExportFilter(urlFilter) } : null,

    };

    var exportRequest = new ExportReportRequest
    {
        Format = format,
        PowerBIReportConfiguration = powerBIReportExportConfiguration,
    };

    // The 'Client' object is an instance of the Power BI .NET SDK
    var export = await Client.Reports.ExportToFileInGroupAsync(groupId, reportId, exportRequest);

    // Save the export ID, you'll need it for polling and getting the exported file
    return export.Id;
}

推荐阅读