我在一个名为 test 的文件夹中有 10 个不同的 Excel 工作表。我想使用 Windows Power shell 脚本刷新数据连接和数据透视表中的数据。该过程使用 for 每个循环打开每个文件,然后在文件中选择 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()
最佳答案
作为建议,不要在脚本中运行它,而是调整您的数据模型以具有可以使用的属性。 (比如上个月 - 或者你想调用它,但它会提供你的上个月切片)你只需要依靠你的 $workBook.RefreshAll() 来更新每张工作表。上个月或上个月将取决于您的数据并在刷新时更新。
然后,您可以将此上个月切片器链接到您需要成为上个月的表。
关于excel - 使用 powershell 刷新 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31865691/