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

问题描述 投票:0回答:1

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

这是我的目标:

  1. 根据提取的数据范围在 Excel 工作表中插入垂直和水平分页符。
  2. 将“分页预览”设置为 Excel 工作表的默认页面布局。

这是我到目前为止所拥有的:

$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

提前致谢!

excel powershell vbscript
1个回答
1
投票

@培根块感谢分享页面。这绝对为我的第一个问题提供了答案。

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

就这样吧。下面的 VBScript 将:

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

    WScript.echo“**格式化提取...--”&现在

    '获取当前目录

设置参数= 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
© www.soinside.com 2019 - 2024. All rights reserved.