// 用你的 CSV 文件路径替换此路径
string csvPath = "D:/powerbi星球/Tabulareditor/批量导入度量值/度量值表.csv";
var lines = System.IO.File.ReadAllLines(csvPath).Skip(1); // 跳过标题行
foreach (var line in lines)
{
var matches = System.Text.RegularExpressions.Regex.Matches(line, @"(?:^|,)(?:""([^""]*(?:""""[^""]*)*)""|([^,]*))");
if (matches.Count < 3) continue;
var tableName = matches[0].Groups[1].Value;
if (string.IsNullOrEmpty(tableName))
{
tableName = matches[0].Groups[2].Value;
}
var measureName = matches[1].Groups[1].Value;
if (string.IsNullOrEmpty(measureName))
{
measureName = matches[1].Groups[2].Value;
}
var formula = matches[2].Groups[1].Value;
if (string.IsNullOrEmpty(formula))
{
formula = matches[2].Groups[2].Value;
}
formula = formula.Replace("\"\"", "\"");
var table = Model.Tables[tableName];
if (table == null) continue;
try
{
table.AddMeasure(measureName, formula);
}
catch{}
}
// 用你的 CSV 文件路径替换此路径
string csvPath = "D:/PowerBI星球/Tabulareditor/批量导入度量值并设置格式/度量值.csv";
var lines = System.IO.File.ReadAllLines(csvPath).Skip(1);
foreach (var line in lines)
{
var matches = System.Text.RegularExpressions.Regex.Matches(line, @"(?:^|,)(?:""([^""]*(?:""""[^""]*)*)""|([^,]*))");
if (matches.Count < 4) continue;
var tableName = matches[0].Groups[1].Value;
if (string.IsNullOrEmpty(tableName))
{
tableName = matches[0].Groups[2].Value;
}
var measureName = matches[1].Groups[1].Value;
if (string.IsNullOrEmpty(measureName))
{
measureName = matches[1].Groups[2].Value;
}
var formula = matches[2].Groups[1].Value;
if (string.IsNullOrEmpty(formula))
{
formula = matches[2].Groups[2].Value;
}
var format = matches[3].Groups[1].Value;
if (string.IsNullOrEmpty(format))
{
format = matches[3].Groups[2].Value;
}
formula = formula.Replace("\"\"", "\"");
var table = Model.Tables[tableName];
if (table == null) continue;
try
{
var newMeasure = table.AddMeasure(measureName, formula);
newMeasure.FormatString = format; // 设置度量值的格式
}
catch
{}
}
关于度量值格式的写法可以参考:
联系客服