【问题标题】:Refreshing Excel Sheets using powershell使用 powershell 刷新 Excel 表格
【发布时间】:2015-10-30 04:30:32
【问题描述】:

我在一个名为 test 的文件夹中有 10 个不同的 excel 表。我想使用 windows power shell 脚本刷新数据透视表中的数据连接和数据。该过程用于每个循环打开每个文件,然后在文件中选择 powerpivot 菜单刷新所有数据,这将刷新 OLEDB 和 OLAP 查询,然后关闭该页面并转到数据选项卡,然后单击全部刷新按钮在每个 excel 表中刷新工作簿 5 张中有 4 张中有 5 张 B4 表示切片器中的月份。我们需要在所有 4 张表中选择上个月,而不是保存 excel.xlsx 文件并转到下一个文件执行相同的过程。如果有人可以帮助修改代码,我有基本代码。 Sheet 1 Sheet 3 Sheet 4 Sheet 5 有切片器。

#Set the file path (can be a network location)
$filePath = "C:\test"


$excelObj = New-Object -ComObject Excel.Application

$excelObj.Visible = $true

#Open the workbook
$workBook = $excelObj.Workbooks.Open($filePath)

#Focus on the top row of the "Data" worksheet
$workSheet = $workBook.Sheets.Item("PowerPivot")
$workSheet.Select()
$workSheet.PowerPivotWindow
foreach ($file in $excelfiles)
{
$workSheet = $workBook.Sheets.Item("Home")
$workSheet.Select()
$workBook.RefreshAll()
$excelworkbook.Close()

$workSheet = $workBook.Sheets.Item("Data")
$workSheet.Select()

#Refresh all data in this workbook
$workBook.RefreshAll()


$workBook.Save()
$excelworkbook.Close()
}

#Uncomment this line if you want Excel to close on its own
$excelObj.Quit()

【问题讨论】:

    标签: excel powershell powershell-2.0 powerpivot powershell-4.0


    【解决方案1】:

    作为建议,不要在脚本中运行它,而是调整您的数据模型以具有您可以使用的属性。 (就像上个月一样——或者你想叫它什么,但它会提供你上个月的切片)你只需要依靠你的 $workBook.RefreshAll() 来更新每张纸。因为上个月或上个月将取决于您的数据并在刷新时更新。

    然后,您可以将此上个月切片器链接到您需要成为上个月的表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多