更改 PowerPivot 文件中的连接数据

发布于 2024-11-04 18:01:42 字数 128 浏览 4 评论 0原文

所以我遇到了一个有趣的问题。我需要在 PowerPivot 查询中指定自定义 WHERE 子句。我必须根据外部条件来改变它。我想编辑该文件并保存副本。知道如何做到这一点吗?我从二进制文件打开了 PowerPivot 文件,但它似乎已加密...

So I've run into an interesting problem. I need to specify a custom WHERE clause in a PowerPivot query. I must change it based on external conditions. I would like to edit the file and save a copy. Any idea how to do this? I opened the PowerPivot file from binary, but it appears encrypted...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-11-11 18:01:42

您可以转到现有连接,然后在那里进行更新。如果您再次打开相同的数据源(SQL、SSRS 或其他任何数据源)而不是更改现有连接上的参数,则会降低性能,因为 PowerPivot 会将这些数据视为单独的连接。

You can go to existing connections, and then make an update there. If you open the same data source (SQL, SSRS, or anything else) again instead of changing parameters on the existing connection, it will slow your perf as PowerPivot will treat those as separate connections.

何以畏孤独 2024-11-11 18:01:42

解决方案是以 Zip 形式打开 Excel 工作簿(使用 Package 类)。

如果您想修改查询,则可以。 /xl/customData/item1.data 处的文件是一个备份文件,代表用于处理查询的 PowerPivot 数据库(只是在 Vertipaq 模式下运行的 Analysis Services 数据库)。您需要将文件恢复到在 Vertipaq 模式下运行的 SSAS 实例。完成后,将查询编写为 ALTER 脚本。修改脚本(在本例中,将 @projectId 替换为我的实际项目 ID),然后针对数据库运行它们。完成所有这些后,备份数据库并放回到 Excel 工作簿中。这会修改查询。

连接数据存储在 /xl/connections.xml 文件中。打开它,修改并替换。再次重新打包,现在您又拥有了一本工作簿。

这是我制作的代码。您必须根据需要调用这些方法。基本的想法已经有了,但是......

    const string DBName = "Testing";
    const string OriginalBackupPath = @"\\MyLocation\BKUP.abf";
    const string ModifiedBackupPath = @"\\MyLocation\BKUPAfter.abf";
    const string ServerPath = @"machineName\powerpivot";

    private static readonly Server srv = new Server();
    private static readonly Scripter scripter = new Scripter();
    private static Database db;

    private static byte[] GetPackagePartContents(string packagePath, string partPath)
    {
        var pack = Package.Open(packagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        var part = pack.GetPart(new Uri(partPath, UriKind.Relative));
        var stream = part.GetStream();
        var b = new byte[stream.Length];
        stream.Read(b, 0, b.Length);
        stream.Flush();
        stream.Close();
        pack.Flush();
        pack.Close();
        return b;
    }

    private static void WritePackagePartContents(string packagePath, string partPath, byte[] contents)
    {
        var uri = new Uri(partPath, UriKind.Relative);
        var pack = Package.Open(packagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        var part = pack.GetPart(uri);
        var type = part.ContentType;
        pack.DeletePart(uri);
        pack.CreatePart(uri, type);
        part = pack.GetPart(uri);
        var stream = part.GetStream();
        stream.Write(contents, 0, contents.Length);
        stream.Flush();
        stream.Close();
        pack.Flush();
        pack.Close();
    }

    private static void RestoreBackup(string server, string dbName, string backupPath)
    {
        srv.Connect(server);
        if (srv.Databases.FindByName(dbName) != null) { srv.Databases.FindByName(dbName).Drop(); srv.Update(); }
        srv.Restore(backupPath, dbName, true);
        srv.Update();
        srv.Refresh();
    }

    private static void WriteContentsToFile(byte[] contents, string filePath)
    {
        var fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
        fileStream.Write(contents, 0, contents.Length);
        fileStream.Flush();
        fileStream.Close();
    }

    private static byte[] ReadContentsFromFile(string filePath)
    {
        var fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
        var b = new byte[fileStream.Length];
        fileStream.Read(b, 0, b.Length);
        fileStream.Flush();
        fileStream.Close();
        return b;
    }

    private static XDocument GetAlterScript(MajorObject obj)
    {
        var stream = new MemoryStream();
        var streamWriter = XmlWriter.Create(stream);
        scripter.ScriptAlter(new[] { obj }, streamWriter, false);
        streamWriter.Flush();
        streamWriter.Close();
        stream.Flush();
        stream.Position = 0;
        var b = new byte[stream.Length];
        stream.Read(b, 0, b.Length);
        var alterString = new string(Encoding.UTF8.GetString(b).ToCharArray().Where(w => w != 65279).ToArray());
        var alter = XDocument.Parse(alterString);
        stream.Close();
        return alter;
    }

    private static void ExecuteScript(string script)
    {
        srv.Execute(script);
        srv.Update();
        db.Process();
        db.Refresh();
    }



    private static void ProcessPowerpointQueries(string bookUrl, string projectId)
    {
        byte[] b = GetPackagePartContents(bookUrl, "/xl/customData/item1.data");
        WriteContentsToFile(b, OriginalBackupPath);
        RestoreBackup(ServerPath, DBName, OriginalBackupPath);
        var db = srv.Databases.GetByName(DBName);
        var databaseView = db.DataSourceViews.FindByName("Sandbox");
        var databaseViewAlter = GetAlterScript(databaseView);
        var cube = db.Cubes.FindByName("Sandbox");
        var measureGroup = cube.MeasureGroups.FindByName("Query");
        var partition = measureGroup.Partitions.FindByName("Query");
        var partitionAlter = GetAlterScript(partition);
        var regex = new Regex(@"\s@projectid=\w*[ ,]");
        var newDatabaseViewAlter = databaseViewAlter.ToString().Replace(regex.Match(databaseViewAlter.ToString()).Value.Trim(',',' '), @"@projectid=" + projectId);
        ExecuteScript(newDatabaseViewAlter);
        var newPartitionAlter = partitionAlter.ToString().Replace(regex.Match(partitionAlter.ToString()).Value.Trim(',', ' '), @"@projectid=" + projectId);
        ExecuteScript(newPartitionAlter);
        db.Backup(ModifiedBackupPath, true);
        WritePackagePartContents(bookUrl, @"/xl/customData/item1.data", ReadContentsFromFile(ModifiedBackupPath));
        db.Drop();
        srv.Disconnect();
    }

    private static void ProcessWorkbookLinks(string bookUrl, string newCoreUrl)
    {
        var connectionsFile = GetPackagePartContents(bookUrl, @"/xl/connections.xml");
        var connectionsXml = Encoding.UTF8.GetString(connectionsFile);
        connectionsXml = connectionsXml.Replace(
            new Regex(@"Data Source=\S*;").Match(connectionsXml).Value.Trim(';'), @"Data Source=" + newCoreUrl);
        WritePackagePartContents(bookUrl, @"/xl/connections.xml", connectionsXml.Replace(@"https://server/site/", newCoreUrl).ToCharArray().Select(Convert.ToByte).ToArray());
    }

Solution was to open the Excel workbook up as a Zip (using the Package class).

If you are looking to modify the queries, you can. The file at /xl/customData/item1.data is a backup file that represents the PowerPivot database (which is just an Analysis Services database running in Vertipaq mode) used to process queries. You need to restore the file to a SSAS instance running in Vertipaq mode. Once that's done, script the queries as an ALTER script. Modify the scripts (in this case, replacing @projectId with my actual projectID), then run them against the database. Once all this is done, back the database up and put back into the Excel workbook. That modifies the queries.

The connection data is stored in the /xl/connections.xml file. Open that up, modify, and replace. Repack it all up again, and now you have a workbook again.

Here's the code I made. You will have to call the methods as you need. Basic idea is there, though...

    const string DBName = "Testing";
    const string OriginalBackupPath = @"\\MyLocation\BKUP.abf";
    const string ModifiedBackupPath = @"\\MyLocation\BKUPAfter.abf";
    const string ServerPath = @"machineName\powerpivot";

    private static readonly Server srv = new Server();
    private static readonly Scripter scripter = new Scripter();
    private static Database db;

    private static byte[] GetPackagePartContents(string packagePath, string partPath)
    {
        var pack = Package.Open(packagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        var part = pack.GetPart(new Uri(partPath, UriKind.Relative));
        var stream = part.GetStream();
        var b = new byte[stream.Length];
        stream.Read(b, 0, b.Length);
        stream.Flush();
        stream.Close();
        pack.Flush();
        pack.Close();
        return b;
    }

    private static void WritePackagePartContents(string packagePath, string partPath, byte[] contents)
    {
        var uri = new Uri(partPath, UriKind.Relative);
        var pack = Package.Open(packagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        var part = pack.GetPart(uri);
        var type = part.ContentType;
        pack.DeletePart(uri);
        pack.CreatePart(uri, type);
        part = pack.GetPart(uri);
        var stream = part.GetStream();
        stream.Write(contents, 0, contents.Length);
        stream.Flush();
        stream.Close();
        pack.Flush();
        pack.Close();
    }

    private static void RestoreBackup(string server, string dbName, string backupPath)
    {
        srv.Connect(server);
        if (srv.Databases.FindByName(dbName) != null) { srv.Databases.FindByName(dbName).Drop(); srv.Update(); }
        srv.Restore(backupPath, dbName, true);
        srv.Update();
        srv.Refresh();
    }

    private static void WriteContentsToFile(byte[] contents, string filePath)
    {
        var fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
        fileStream.Write(contents, 0, contents.Length);
        fileStream.Flush();
        fileStream.Close();
    }

    private static byte[] ReadContentsFromFile(string filePath)
    {
        var fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
        var b = new byte[fileStream.Length];
        fileStream.Read(b, 0, b.Length);
        fileStream.Flush();
        fileStream.Close();
        return b;
    }

    private static XDocument GetAlterScript(MajorObject obj)
    {
        var stream = new MemoryStream();
        var streamWriter = XmlWriter.Create(stream);
        scripter.ScriptAlter(new[] { obj }, streamWriter, false);
        streamWriter.Flush();
        streamWriter.Close();
        stream.Flush();
        stream.Position = 0;
        var b = new byte[stream.Length];
        stream.Read(b, 0, b.Length);
        var alterString = new string(Encoding.UTF8.GetString(b).ToCharArray().Where(w => w != 65279).ToArray());
        var alter = XDocument.Parse(alterString);
        stream.Close();
        return alter;
    }

    private static void ExecuteScript(string script)
    {
        srv.Execute(script);
        srv.Update();
        db.Process();
        db.Refresh();
    }



    private static void ProcessPowerpointQueries(string bookUrl, string projectId)
    {
        byte[] b = GetPackagePartContents(bookUrl, "/xl/customData/item1.data");
        WriteContentsToFile(b, OriginalBackupPath);
        RestoreBackup(ServerPath, DBName, OriginalBackupPath);
        var db = srv.Databases.GetByName(DBName);
        var databaseView = db.DataSourceViews.FindByName("Sandbox");
        var databaseViewAlter = GetAlterScript(databaseView);
        var cube = db.Cubes.FindByName("Sandbox");
        var measureGroup = cube.MeasureGroups.FindByName("Query");
        var partition = measureGroup.Partitions.FindByName("Query");
        var partitionAlter = GetAlterScript(partition);
        var regex = new Regex(@"\s@projectid=\w*[ ,]");
        var newDatabaseViewAlter = databaseViewAlter.ToString().Replace(regex.Match(databaseViewAlter.ToString()).Value.Trim(',',' '), @"@projectid=" + projectId);
        ExecuteScript(newDatabaseViewAlter);
        var newPartitionAlter = partitionAlter.ToString().Replace(regex.Match(partitionAlter.ToString()).Value.Trim(',', ' '), @"@projectid=" + projectId);
        ExecuteScript(newPartitionAlter);
        db.Backup(ModifiedBackupPath, true);
        WritePackagePartContents(bookUrl, @"/xl/customData/item1.data", ReadContentsFromFile(ModifiedBackupPath));
        db.Drop();
        srv.Disconnect();
    }

    private static void ProcessWorkbookLinks(string bookUrl, string newCoreUrl)
    {
        var connectionsFile = GetPackagePartContents(bookUrl, @"/xl/connections.xml");
        var connectionsXml = Encoding.UTF8.GetString(connectionsFile);
        connectionsXml = connectionsXml.Replace(
            new Regex(@"Data Source=\S*;").Match(connectionsXml).Value.Trim(';'), @"Data Source=" + newCoreUrl);
        WritePackagePartContents(bookUrl, @"/xl/connections.xml", connectionsXml.Replace(@"https://server/site/", newCoreUrl).ToCharArray().Select(Convert.ToByte).ToArray());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文