Powershell - 将多列添加到多个 excel 文件
Powershell - add multiple columns to multiple excel files
我有一个文件夹,里面有 50 多个 excel 文件(下面路径中的“项目转储”。)所有这些文件都包含完全相同的数据(其存档的月度数据用于MoM 报告)我需要更新所有这些文件以添加 10 个新列 headers - 这些列中的 none 将包含任何数据,只需将它们添加到 table以匹配将在其中包含数据的最新月份提取。
我一直在使用 Powershell,并且有一个脚本可以一次将一列添加到一个文件,但老实说,我手动打开每个文件并自己添加列会更快。我似乎无法弄清楚如何更改我的脚本以执行它对多个文件(以及多列)的操作,我们将不胜感激任何帮助!
背景;参考是我的项目转储文件夹中的特定文件。第 50 列是第一个空白列,需要添加到 table:
(Get-ChildItem "C:\Downloads\Project dump\ArchiveJAN21.xlsx")|
foreach-object {
$xl=New-Object -ComObject Excel.Application
$wb=$xl.workbooks.open($_)
$ws = $wb.worksheets.Item(1)
$ws.Columns.ListObject.ListColumns.Add(50)
$ws.Cells.Item(1,50) ='Call Type'
$wb.Save()
$xl.Quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject([System.__ComObject]$xl)){'released'| Out-Null}
}
您需要在循环之前定义Excel对象并在之后退出。
此外,使用 Get-ChildItem
从文件夹路径获取 FileInfo 对象,而不是文件的硬编码路径。
尝试:
# an array with the new column names
$newColumns = 'Call Type','NewCol2','NewCol3','NewCol4','NewCol5','NewCol6','NewCol7','NewCol8','NewCol9','NewCol10'
# create the Excel object outside of the loop
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $false
# loop thtrough the files in the folder
(Get-ChildItem -Path 'C:\Downloads\Project dump' -Filter '*.xlsx' -File ) | ForEach-Object {
$wb = $xl.WorkBooks.Open($_.FullName)
$ws = $wb.Worksheets.Item(1)
# get the number of columns in the sheet
$startColumn = $ws.UsedRange.Columns.Count
for ($i = 0; $i -lt $newColumns.Count; $i++) {
$startColumn++ # increment the column counter
$ws.Cells.Item(1, $startColumn) = $newColumns[$i]
}
$wb.Close($true) # $true saves the changes
}
# quit Excel and clean COM objects from memory
$xl.Quit()
# clean up the COM objects used
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ws)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
我有一个文件夹,里面有 50 多个 excel 文件(下面路径中的“项目转储”。)所有这些文件都包含完全相同的数据(其存档的月度数据用于MoM 报告)我需要更新所有这些文件以添加 10 个新列 headers - 这些列中的 none 将包含任何数据,只需将它们添加到 table以匹配将在其中包含数据的最新月份提取。
我一直在使用 Powershell,并且有一个脚本可以一次将一列添加到一个文件,但老实说,我手动打开每个文件并自己添加列会更快。我似乎无法弄清楚如何更改我的脚本以执行它对多个文件(以及多列)的操作,我们将不胜感激任何帮助!
背景;参考是我的项目转储文件夹中的特定文件。第 50 列是第一个空白列,需要添加到 table:
(Get-ChildItem "C:\Downloads\Project dump\ArchiveJAN21.xlsx")|
foreach-object {
$xl=New-Object -ComObject Excel.Application
$wb=$xl.workbooks.open($_)
$ws = $wb.worksheets.Item(1)
$ws.Columns.ListObject.ListColumns.Add(50)
$ws.Cells.Item(1,50) ='Call Type'
$wb.Save()
$xl.Quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject([System.__ComObject]$xl)){'released'| Out-Null}
}
您需要在循环之前定义Excel对象并在之后退出。
此外,使用 Get-ChildItem
从文件夹路径获取 FileInfo 对象,而不是文件的硬编码路径。
尝试:
# an array with the new column names
$newColumns = 'Call Type','NewCol2','NewCol3','NewCol4','NewCol5','NewCol6','NewCol7','NewCol8','NewCol9','NewCol10'
# create the Excel object outside of the loop
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $false
# loop thtrough the files in the folder
(Get-ChildItem -Path 'C:\Downloads\Project dump' -Filter '*.xlsx' -File ) | ForEach-Object {
$wb = $xl.WorkBooks.Open($_.FullName)
$ws = $wb.Worksheets.Item(1)
# get the number of columns in the sheet
$startColumn = $ws.UsedRange.Columns.Count
for ($i = 0; $i -lt $newColumns.Count; $i++) {
$startColumn++ # increment the column counter
$ws.Cells.Item(1, $startColumn) = $newColumns[$i]
}
$wb.Close($true) # $true saves the changes
}
# quit Excel and clean COM objects from memory
$xl.Quit()
# clean up the COM objects used
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ws)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()