excel - 使用 powershell 刷新 Excel 工作表

标签 excel powershell powershell-2.0 powerpivot powershell-4.0

我在一个名为 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/

相关文章:

vba - 使宏在特定工作簿中运行

windows - 检查注册表值是否等于 1 无法正常工作

powershell - 自定义表输出清除-格式化数组值的属性

c# - 大型目录/子目录数据传输

powershell - 写输出在回调内不起作用

java - 在java中将xls转换为xlsx

arrays - 数组类型不匹配

vba - 无法将 CopyFromRecordset 结果设置为日期或数字

powershell - 显示消息框以使用 Powershell 脚本从中复制代码部分

powershell - 使用 powershell 从 XML 文件中提取部分