使用PowerShell在Excel文档中插入空行

使用PowerShell在Excel文档中插入空行,excel,powershell,Excel,Powershell,我试图以编程方式在Excel工作表中的指定位置插入新行,尽管在web上进行了大量搜索,我还是找不到实现这一点的方法 以下代码成功插入空列: $xlShiftToRight = -4161 #Get list of files $File = Get-ChildItem c:\path\to\file.xlsx #Launch Excel $XL = New-Object -ComObject Excel.Application #Open the file and get the Data

我试图以编程方式在Excel工作表中的指定位置插入新行,尽管在web上进行了大量搜索,我还是找不到实现这一点的方法

以下代码成功插入空列:

$xlShiftToRight = -4161

#Get list of files
$File = Get-ChildItem c:\path\to\file.xlsx

#Launch Excel
$XL = New-Object -ComObject Excel.Application

#Open the file and get the Data sheet
$WB = $XL.Workbooks.Open($File.Fullname)

$XL.Visible = $true
$Sheet = $WB.Worksheets.Item('Naira')

$objRange = $XL.Range("C3").EntireColumn
[void]$objRange.Insert($xlShiftToRight)
但是镜面反射的替代方法-使用
EntireRow
而不是
entireLumn
则不行

问题


如何使用PowerShell在Excel文档中的另一个特定行之后插入一行?

仅更改范围是不够的。您还需要更改移动现有单元格的方向(插入整行时向右移动没有多大意义)

更改此项:

$xlShiftToRight = -4161 

...

$objRange = $XL.Range("C3").EntireColumn 
[void] $objRange.Insert($xlShiftToRight)
为此:

$xlShiftDown = -4121

...

$objRange = $XL.Range("C3").EntireRow
[void]$objRange.Insert($xlShiftDown)

谢谢你的回复。这是可行的——除此之外,我还遇到了另一个我没有意识到的问题,我试图在“表”中插入新行,显然这是不允许的。