冻结工作表的第一行

时间:2015-05-20 18:21:43

标签: excel powershell

PowerShell代码创建Excel。

我正试图冻结顶行:

$excel = New-Object -Com Excel.Application
$excel.Visible = $True
$wb = $Excel.Workbooks.Add()
$ws = $wb.Worksheets.Add()

$ws.Activate()
$ws.Select()

$excel.Rows.Item("1:1").Select()
$excel.ActiveWindow.FreezePanes = $true

它不会冻结顶行,而是冻结行的中心和列的中心,即

enter image description here

更新

重复帖子中的解决方案不起作用,即

$excel.Rows("1:1").Select()
$excel.ActiveWindow.FreezePanes = $true

给出以下错误:

Method invocation failed because [System.__ComObject] does not contain a method named 'Rows'.
At D:\Script\upgrades.ps1:231 char:5
+     $excel.Rows("1:1").Select()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Rows:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

3 个答案:

答案 0 :(得分:7)

要冻结顶行,您需要选择第二个行:

public function exampleEventHandler(Varien_Event_Observer $observer)
{
    /* @var Mage_Sales_Model_Order $order */
    $order = $observer->getOrder();
    $stateProcessing = $order::STATE_PROCESSING;
    // Only trigger when an order enters processing state.
    if ($order->getState() == $stateProcessing && $order->getOrigData('state') != $stateProcessing) {
        ...
    }
    ...
}

答案 1 :(得分:1)

如果您有多张纸,需要冻结每张纸的顶行:

$colSheets = ($Sheet1, $Sheet2, $Sheet3, $Sheet4, $Sheet5)

foreach ($page in $colSheets){
    $page.Select()
    $page.application.activewindow.splitcolumn = 0
    $page.application.activewindow.splitrow = 1
    $page.application.activewindow.freezepanes = $true
}
#After you are done, re-select first sheet (optional)
$Sheet1.Select()

答案 2 :(得分:0)

$excel.Application.ActiveWindow.SplitRow = 1
$excel.Application.ActiveWindow.FreezePanes = $true