脚本:在Excel中设置打印区域和默认分页预览

时间:2015-07-24 20:31:20

标签: excel powershell

**更新主题...(前者:Powershell - 在Excel中插入分页符)

以下是我的目标:

  1. 根据提取的数据范围在Excel工作表中插入垂直和水平分页符。
  2. 将“分页预览”设置为Excel工作表的默认页面布局。
  3. 这是我到目前为止所做的:

    $Excel = New-Object -ComObject Excel.Application
    $Excel.visible = $True
    $wbpath = 'c:\path\to\excel.xls'
    $wb = $Excel.Workbooks.Open($wbpath)
    $ws = $wb.Sheets.Item(1)
    $rows=$ws.UsedRange.Rows.Count
    $rowcnt = $Excel.WorksheetFunction.CountIf($ws.Range("A1:A"+$rows), "<>")
    $RowRange = $ws.Range("A1:A"+$rowcnt)
    $RowRange = $ws.Range("A1:A"+$rowcnt)
    $ColumnRange = #Set column range
    
    ##Set $Rowrange as VRagebreak
    ##Set $ColumnRange as HPageBreak
    ##Set Page Break view as default Page Layout view
    
    $Workbook.Save()
    $Workbook.Close()
    $Excel.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)|Out-Null
    [gc]::collect() | Out-Null
    [gc]::WaitForPendingFinalizers() | Out-Null
    

    提前致谢!

1 个答案:

答案 0 :(得分:1)

@ Bacon Bits感谢您分享page。这绝对是我第一个问题的答案。

但是经过几个小时的搜索后,我发现不是Powershell,而是一种更优选的语言来解决我的问题就是VBScript。 希望这不会引起我对偏离主题的担忧,但我想分享一下我是如何使用VBscript实现目标的。试着帮助那些可能遇到同样问题的人。

在这里。下面的VBScript将:

  1. 从调用脚本传递的目录中获取所有excel文件(例如batch,powershell等)
  2. 将正确的打印区域(分页符)设置为管道中的当前Excel工作表。
  3. 将当前工作表视图设置为分页预览

    WScript.echo“**格式化摘录... - ”&amp;现在

    '获取当前目录

  4. 设置args = WScript.Arguments

        CurDir = args.Item(0)
        wscript.echo "Current Directory: " & CurDir
    
    'Page break constants
        const xlPageBreakManual = -4135
        const xlPageBreakNone = -4142 
        const xlPageBreakPreview  = &H2 
        const xlNormalView = &H1 
    
    'Initiate Excel variables    
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       Set objFolder = objFSO.GetFolder(CurDir)
       Set colFiles = objFolder.Files
    
    For Each objFile in colFiles
        If UCase(objFSO.GetExtensionName(objFile.name)) = "XLS" Then
            AbsolutePathName = objFSO.GetAbsolutePathName(objFile)
            ExtractName = objFSO.GetFileName(objFile)
            'Wscript.Echo "File path and Name: " & AbsolutePathName
            'Wscript.Echo "Extract Name: " & extractName
    
            'Open Excel
            Set xlApp = CreateObject("Excel.Application")
            set xlBook = xlApp.WorkBooks.Open(AbsolutePathName)
            set xlSht = xlApp.Worksheets.Item(1)
    
            'Set print area
            rows = xlSht.UsedRange.Rows.count
            rowcnt = xlApp.WorksheetFunction.CountIf(xlSht.Range("A1:A"&rows), "<>")
            if InStr(extractName,"DCIS") = 1 then
                wscript.echo extractName & ": Setting print area..."
                xlSht.PageSetup.PrintArea = "$A$1:$N$" & rowcnt
            elseif InStr(extractName,"WCIS") = 1 then
                wscript.echo extractName & ": Setting print area..."
                xlSht.PageSetup.PrintArea = "$A$1:$O$" & rowcnt
            end if
    
            'Set current worksheet view to Page Break Preview
            Wscript.Echo extractName & ": Setting default view to Page break view..."     
            xlApp.ActiveWindow.View = xlPageBreakPreview
    
            'Save formatted excel
            xlBook.Save
            xlBook.Close False
            xlApp.Quit
    
            'Deallocate after use
            Set xlSht = Nothing
            Set xlBook = Nothing
            Set xlApp = Nothing 
        End If
    Next
    
    WScript.echo "**Finished formatting extracts. -- " & Now