You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:将Power BI文件转为JSON的C#代码实现问题

解决Power BI模型转JSON(TMSL)的代码错误及实现方案

问题背景

我需要将现有Power BI文件转换为JSON格式,制作成可程序化修改(添加表/度量值)的模板,之后按需上传到客户工作区,避免为每个客户手动编辑PBIX文件。我了解可以通过TOM(表格对象模型)在Visual Studio或Tabular Editor中编辑Power BI模型,但认为JSON文件更便于操作。

我在Visual Studio中编写了以下C#代码,编译时提示does not contain a definition "toJson"错误,由于刚接触C#,暂时找不到添加ToJson方法的途径:

namespace ConvertPowerBIFileToTMSL
{
    class Program
    {
        static void Main(string[] args)
        {
            string pbixFilePath = @"C:\Users\USER\Downloads\MyDashboard.pbix";
            string tmslFilePath = @"C:\Users\USER\Downloads\MyTMSLFile.json";

            using (var server = new Server())
            {
                server.Connect("localhost");

                var database = server.Databases.GetByName("MyDatabase");
                var model = database.Model;

                var tmsl = model.ToJson(pbixFilePath);
                File.WriteAllText(tmslFilePath, tmsl);
            }
        }
    }
}

错误原因及解决方法

1. 错误根源

你使用的Model对象并无内置ToJson方法,且代码逻辑存在问题:Server类用于连接SSAS(SQL Server Analysis Services)实例,无法直接读取本地PBIX文件。

2. 正确实现方案

要实现PBIX转TMSL(JSON格式)并支持程序化修改,推荐使用Tabular Editor的API或Power BI REST API,以下是两种可行的代码实现:

方案一:通过Tabular Editor API读取PBIX并导出TMSL

首先安装NuGet包TabularEditor.TOMWrapper和TabularEditor.PowerBI,再使用以下代码:

using System.IO;
using TabularEditor.TOMWrapper;
using TabularEditor.TOMWrapper.Utils;

namespace PBIXtoTMSL
{
    class Program
    {
        static void Main(string[] args)
        {
            var pbixPath = @"C:\Users\USER\Downloads\MyDashboard.pbix";
            var tmslPath = @"C:\Users\USER\Downloads\MyTMSLFile.json";

            // 加载本地PBIX文件
            var model = TabularModelHandler.Load(pbixPath);

            // 将模型序列化为TMSL格式的JSON
            var tmsl = model.SerializeTmsl();

            // 写入JSON文件
            File.WriteAllText(tmslPath, tmsl);

            // 示例:程序化添加度量值
            var salesTable = model.Tables["Sales"];
            salesTable.AddMeasure("Total Sales", "SUM(Sales[Amount])", "Sales Measures");

            // 保存修改后的模型为新PBIX文件
            model.SaveAs(@"C:\Users\USER\Downloads\ModifiedDashboard.pbix");
        }
    }
}

方案二:通过Power BI REST API获取模型TMSL

若已将PBIX发布到Power BI服务,可通过REST API获取模型的TMSL:

using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.IO;

namespace PBITMSLExport
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var workspaceId = "你的工作区ID";
            var datasetId = "你的数据集ID";
            var accessToken = "你的Power BI访问令牌";
            var tmslPath = @"C:\Users\USER\Downloads\MyTMSLFile.json";

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                var response = await client.GetAsync($"https://api.powerbi.com/v1.0/myorg/groups/{workspaceId}/datasets/{datasetId}/executeQueries?queries={{\"query\":\"EVALUATE MODEL()\"}}&serializerSettings={\"includeMetadata\":true}");
                
                var tmsl = await response.Content.ReadAsStringAsync();
                File.WriteAllText(tmslPath, tmsl);
            }
        }
    }
}

3. 注意事项

  • Tabular Editor API是处理本地PBIX最便捷的方式,支持直接修改模型后保存为PBIX或重新导出TMSL
  • 使用Power BI REST API时,需先获取有效的访问令牌(可通过Azure AD应用程序授权)
  • 修改TMSL后,可通过Power BI REST API的Update Dataset接口将修改后的模型部署到客户工作区

内容的提问来源于stack exchange,提问作者NutellaKing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 05:37:17