【发布时间】: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