vba - 使用 Excel VBA 将 Powerpivot 连接更改为 csv 文件

标签 vba excel csv powerpivot

如何使用 vba 修改 Powerpivot 连接到 csv 文件?我有一个 Excel 工作簿,其 Powerpivot 模型连接到 csv 文件。在 Powerpivot 窗口中,我可以导航到 Home->Existing Connections 并根据需要编辑文件路径。我无法在 vba 中进行编辑。

  • 我找到了this link关于参数化 Powerpivot 连接。
    不幸的是,我不知道如何修改以应用于 csv 文件连接而不是数据库连接。
  • this page 上描述了另一种方法。 .这种方法创建了与 Excel 本身中的数据的连接。然后该连接在 Powerpivot 中可用。关键是不要在 Powerpivot 中创建连接,因为作者说在 Powerpivot 中创建的连接不能被 vba 修改。

  • 我可以通过手动创建此连接Data->Get External Data->From Text menu option .

    我可以使用 "Workbooks("myWorkbook.xlsm").Connections.AddFromFile "data.csv"在 vba 中创建相同的连接。我可以在 Data->Connections->Connections 对话框中手动编辑此连接。但是,当我尝试要使用 WorkbookConnections.TextConnection 属性在 vba 中进行设置,vba 会说“对象不支持此属性或方法”。

    上面的链接侧重于数据库连接而不是 csv 文件连接。但是,似乎可以使用
    Microsoft ACE OLEDB 12.0 连接,但我不能完全掌握它。

    SO上有类似的问题可以解决vba修改数据透视源的问题(例如:Changing pivot table external data source path with Excel macro)。

    但是,我不想将数据放入 Excel 中的表中,我想连接到它以便可以在 Powerpivot 中使用它。

    我可能可以使用 Power Query 执行此操作,但由于我已经拥有所需格式的 csv,我宁愿不必引入此额外步骤。

    我在 Windows 7 上使用 Excel 2013 64 位。感谢您的帮助。

    最佳答案

    我一直在玩类似的东西。还没有成功,但也许我们可以一起解决......

    我注意到的一件事是,这些与 CSV 文件的连接实际上并不是您所期望的 TextConnections,而是 OLEDBConnections。

    编辑:根据 https://goo.gl/x17Nuj ,这是不可能的:

    Once you modify the connection inside PowerPivot, the link between the Excel and the PowerPivot connections is broken. In fact, you can no longer modify the connection properties in Excel and, if you want to load another table, then you have to use the PowerPivot add-in. Needing to use the add-in means that the option of modifying the connection is no longer available in VBA Because (as you have seen) there is no way to modify the PowerPivot connections using VBA.



    (重点是我的)

    关于vba - 使用 Excel VBA 将 Powerpivot 连接更改为 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900967/

    相关文章:

    c# - .NET 的免费商业用途工具可以将 csv 转换为 excel 并将 excel 导出为 pdf?

    java - csv字符集加载infile问题

    macos - 如何将 CSV 转换为多个列表(applescript)?

    excel - 需要帮助在 VBA 中编写循环

    mysql - 直接从 SQL 查询加载 Excel 验证列表

    excel - 使用 .pdf 打印而不提示对话框

    excel - 舍入值未正确插入 Excel

    c# - 尝试使用 EPPlus 读取 .xls 文件时出错

    excel - VBA 另存为方法

    amazon-web-services - 在pyspark(2.2.0)中将CSV文件写入AWS时如何分配访问控制列表(ACL)?